private void AddCascadeMappedLight(ref LightingBGFX r, ref ShadowmappedLight sml, ref Light l, ref float4x4 tx, ref LightMatrices txCache,
                                           ref CascadeShadowmappedLight csm, ref CascadeShadowmappedLightCache csmData, RendererBGFXInstance *sys, bool srgbColors)
        {
            if (r.numCsmLights >= LightingBGFX.maxCsmLights)
            {
                throw new InvalidOperationException("Too many cascade mapped lights");
            }
            bgfx.TextureHandle texShadowMap = sys->m_noShadow;
            int shadowMapSize = 1;

            if (sml.shadowMap != Entity.Null && EntityManager.HasComponent <TextureBGFX>(sml.shadowMap))
            {
                var imShadowMap = EntityManager.GetComponentData <Image2D>(sml.shadowMap);
                Assert.IsTrue(imShadowMap.imagePixelWidth == imShadowMap.imagePixelHeight);
                shadowMapSize = imShadowMap.imagePixelHeight;
                texShadowMap  = EntityManager.GetComponentData <TextureBGFX>(sml.shadowMap).handle;
            }
            float4 worldPosOrDir = new float4(math.normalize(-tx.c2.xyz), 0.0f);
            float4 mask          = new float4(0);
            float3 c             = srgbColors?Color.LinearToSRGB(l.color):l.color;

            r.SetMappedLight(LightingBGFX.maxMappedLights + r.numCsmLights, txCache.mvp, c * l.intensity, worldPosOrDir, l.clipZFar, mask, texShadowMap, shadowMapSize);
            unsafe {
                r.csmOffsetScale[0]  = csmData.c0.offset.x; r.csmOffsetScale[1] = csmData.c0.offset.y; r.csmOffsetScale[2] = 0; r.csmOffsetScale[3] = csmData.c0.scale;
                r.csmOffsetScale[4]  = csmData.c1.offset.x; r.csmOffsetScale[5] = csmData.c1.offset.y; r.csmOffsetScale[6] = 0; r.csmOffsetScale[7] = csmData.c1.scale;
                r.csmOffsetScale[8]  = csmData.c2.offset.x; r.csmOffsetScale[9] = csmData.c2.offset.y; r.csmOffsetScale[10] = 0; r.csmOffsetScale[11] = csmData.c2.scale;
                r.csmOffsetScale[12] = csmData.c3.offset.x; r.csmOffsetScale[13] = csmData.c3.offset.y; r.csmOffsetScale[14] = 0; r.csmOffsetScale[15] = csmData.c3.scale;
            }
            // r.csmLightsis set in SetMappedLight
            r.numCsmLights++;
        }
 public void Set(float4x4 m, float3 color, float4 _worldPosOrDir, float _range, float4 _mask, bgfx.TextureHandle _shadowMap)
 {
     projection        = m;
     color_invrangesqr = new float4(color, LightingBGFX.InverseSquare(_range) * _worldPosOrDir.w);
     worldPosOrDir     = _worldPosOrDir;
     mask      = _mask;
     shadowMap = _shadowMap;
 }
        protected override unsafe void OnUpdate()
        {
            CompleteDependency();

            // TODO -- most all of this should be rewritten with EntityQuery-based AddComponent/RemoveComponent instead of
            // through Entities.ForEach()

            // do this during initialization
            var sys = World.GetExistingSystem <RendererBGFXSystem>().InstancePointer();

            if (!sys->m_initialized)
            {
                return;
            }

            m_WhiteTextureHandle = sys->m_whiteTexture;

            // non-SDF

            // add bgfx version of materials, system states
            Entities.WithStructuralChanges().WithNone <TextMaterialBGFX>().WithAll <BitmapFontMaterial>().ForEach((Entity e) =>
            {
                EntityManager.AddComponent <TextMaterialBGFX>(e);
            }).Run();

            // upload materials
            Entities.WithoutBurst().ForEach((Entity e, ref BitmapFontMaterial mat, ref TextMaterialBGFX matBGFX) =>
            {
                matBGFX.Update(EntityManager, sys, ref mat);
            }).Run();

            // system state cleanup - can reuse the same ecb, it does not matter if there is a bit of delay
            Entities.WithStructuralChanges().WithAll <TextMaterialBGFX>().WithNone <BitmapFontMaterial>().ForEach((Entity e) =>
            {
                EntityManager.RemoveComponent <TextMaterialBGFX>(e);
            }).Run();

            // SDF

            // add bgfx version of materials, system states
            Entities.WithStructuralChanges().WithNone <TextSDFMaterialBGFX>().WithAll <SDFFontMaterial>().ForEach((Entity e) =>
            {
                EntityManager.AddComponent <TextSDFMaterialBGFX>(e);
            }).Run();

            // upload materials
            Entities.WithoutBurst().ForEach((Entity e, ref SDFFontMaterial mat, ref TextSDFMaterialBGFX matBGFX) =>
            {
                matBGFX.Update(EntityManager, sys, ref mat);
            }).Run();

            // system state cleanup - can reuse the same ecb, it does not matter if there is a bit of delay
            Entities.WithStructuralChanges().WithAll <TextSDFMaterialBGFX>().WithNone <SDFFontMaterial>().ForEach((Entity e) =>
            {
                EntityManager.RemoveComponent <TextSDFMaterialBGFX>(e);
            }).Run();
        }
Exemple #4
0
        // true if still loading
        private bool InitTexture(ref bgfx.TextureHandle dest, Entity src, bgfx.TextureHandle defValue)
        {
            dest = defValue;
            if (src == Entity.Null)
            {
                return(false);
            }
            Image2D im = EntityManager.GetComponentData <Image2D>(src); // must have that one, no check?

            if (im.status == ImageStatus.Loaded && EntityManager.HasComponent <TextureBGFX>(src))
            {
                TextureBGFX tex = EntityManager.GetComponentData <TextureBGFX>(src);
                dest = tex.handle;
                return(false);
            }
            return(true);
        }
        private void AddMappedLight(ref LightingBGFX r, ref ShadowmappedLight sml, ref Light l, ref float4x4 tx, ref LightMatrices txCache, RendererBGFXInstance *sys, bool isSpot, float4 spotmask, bool srgbColors)
        {
            if (r.numMappedLights >= LightingBGFX.maxMappedLights)
            {
                throw new InvalidOperationException("Too many mapped lights");
            }
            bgfx.TextureHandle texShadowMap = sys->m_noShadow;
            int shadowMapSize = 1;

            if (sml.shadowMap != Entity.Null && EntityManager.HasComponent <TextureBGFX>(sml.shadowMap))
            {
                var imShadowMap = EntityManager.GetComponentData <Image2D>(sml.shadowMap);
                Assert.IsTrue(imShadowMap.imagePixelWidth == imShadowMap.imagePixelHeight);
                shadowMapSize = imShadowMap.imagePixelHeight;
                texShadowMap  = EntityManager.GetComponentData <TextureBGFX>(sml.shadowMap).handle;
            }
            float4 mask          = isSpot ? spotmask : new float4(0.0f, 0.0f, 0.0f, 1.0f);
            float4 worldPosOrDir = isSpot? new float4(tx.c3.xyz, 1.0f) : new float4(-tx.c2.xyz, 0.0f);
            float3 c             = srgbColors?Color.LinearToSRGB(l.color):l.color;

            r.SetMappedLight(r.numMappedLights, txCache.mvp, c * l.intensity, worldPosOrDir, l.clipZFar, mask, texShadowMap, shadowMapSize);
            r.numMappedLights++;
        }
        public void SetMappedLight(int idx, float4x4 m, float3 color, float4 worldPosOrDir, float range, float4 mask, bgfx.TextureHandle shadowMap, int shadowMapSize)
        {
            switch (idx)
            {
            case 0:
                mappedLight0.Set(m, color, worldPosOrDir, range, mask, shadowMap);
                mappedLight01sis.x = (float)shadowMapSize;
                mappedLight01sis.y = 1.0f / (float)shadowMapSize;
                break;

            case 1:
                mappedLight1.Set(m, color, worldPosOrDir, range, mask, shadowMap);
                mappedLight01sis.z = (float)shadowMapSize;
                mappedLight01sis.w = 1.0f / (float)shadowMapSize;
                break;

            case 2:
                csmLight.Set(m, color, worldPosOrDir, range, mask, shadowMap);
                csmLightsis.x = (float)shadowMapSize;        // full size, not cascade size
                csmLightsis.y = 1.0f / (float)shadowMapSize;
                csmLightsis.z = 1.0f - 3.0f * csmLightsis.y; // border around cascades, in normalized [-1..1]
                break;

            default: throw new IndexOutOfRangeException();
            }
            ;
        }
        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);
            }
        }
Exemple #9
0
        public void SetMappedLight(int idx, float4x4 m, float3 color, float4 worldPosOrDir, float range, float4 mask, bgfx.TextureHandle shadowMap, int shadowMapSize)
        {
            switch (idx)
            {
            case 0:
                mappedLight0.Set(m, color, worldPosOrDir, range, mask, shadowMap);
                mappedLight01sis.x = (float)shadowMapSize;
                mappedLight01sis.y = 1.0f / (float)shadowMapSize;
                break;

            case 1:
                mappedLight1.Set(m, color, worldPosOrDir, range, mask, shadowMap);
                mappedLight01sis.z = (float)shadowMapSize;
                mappedLight01sis.w = 1.0f / (float)shadowMapSize;
                break;

            default: throw new IndexOutOfRangeException();
            }
            ;
        }
Exemple #10
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);
        }
Exemple #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);
        }
Exemple #12
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 #13
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);
        }
Exemple #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);
        }