Exemple #1
0
 private void SetPassComponents(Entity ePass, CameraMask cameraMask, Entity eCam, bool updateClear = false)
 {
     EntityManager.AddComponent <RenderPassAutoSizeToNode>(ePass);
     EntityManager.AddComponentData <CameraMask>(ePass, cameraMask);
     EntityManager.AddComponentData(ePass, new RenderPassUpdateFromCamera {
         camera      = eCam,
         updateClear = updateClear
     });
 }
 private void SetPassComponents(Entity ePass, CameraMask cameraMask, Entity eCam)
 {
     EntityManager.AddComponent <RenderPassAutoSizeToNode>(ePass);
     EntityManager.AddComponent <Frustum>(ePass);
     EntityManager.AddComponentData <CameraMask>(ePass, cameraMask);
     EntityManager.AddComponentData(ePass, new RenderPassUpdateFromCamera
     {
         camera = eCam
     });
 }
Exemple #3
0
        private void FinalizePass(Entity e, bool isTransparent)
        {
            CameraMask cameraMask = new CameraMask {
                mask = ulong.MaxValue
            };

            if (EntityManager.HasComponent <CameraMask>(e))
            {
                cameraMask = EntityManager.GetComponentData <CameraMask>(e);
            }
            ShadowMask shadowMask = new ShadowMask {
                mask = ulong.MaxValue
            };

            if (EntityManager.HasComponent <ShadowMask>(e))
            {
                shadowMask = EntityManager.GetComponentData <ShadowMask>(e);
            }
            Entity eGroup;

            if (isTransparent)
            {
                eGroup = FindOrCreateRenderGroup(new BuildGroup {
                    passTypes = RenderPassType.Transparent, cameraMask = cameraMask, shadowMask = shadowMask
                });
            }
            else
            {
                eGroup = FindOrCreateRenderGroup(new BuildGroup {
                    passTypes = RenderPassType.Opaque | RenderPassType.ZOnly | RenderPassType.ShadowMap, cameraMask = cameraMask, shadowMask = shadowMask
                });
            }
            OptionalSetSharedComponent(e, new RenderToPasses {
                e = eGroup
            });
        }
Exemple #4
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 ePassClear = EntityManager.CreateEntity();

            EntityManager.AddComponentData(ePassClear, new RenderPass {
                inNode              = eNode,
                sorting             = RenderPassSort.Unsorted,
                projectionTransform = float4x4.identity,
                viewTransform       = float4x4.identity,
                passType            = RenderPassType.Clear,
                viewId              = 0xffff,
                scissor             = new RenderPassRect(),
                viewport            = new RenderPassRect {
                    x = 0, y = 0, w = (ushort)w, h = (ushort)h
                },
                clearFlags   = RenderPassClear.Depth | RenderPassClear.Color, // copied from camera
                clearRGBA    = 0xff00ffff,
                clearDepth   = 1.0f,
                clearStencil = 0
            });
            SetPassComponents(ePassClear, cameraMask, eCam, true);

            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   = 0,
                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,
                clearDepth   = 1.0f,
                clearStencil = 0
            });
            SetPassComponents(ePassTransparent, cameraMask, eCam);

            Entity ePassSprites = EntityManager.CreateEntity();

            EntityManager.AddComponentData(ePassSprites, new RenderPass {
                inNode              = eNode,
                sorting             = RenderPassSort.SortZGreater,
                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,
                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,
                clearDepth   = 1.0f,
                clearStencil = 0
            });
            SetPassComponents(ePassUI, cameraMask, eCam);

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

            if (ePassClear != Entity.Null)
            {
                passRefs.Add(new RenderPassRef {
                    e = ePassClear
                });
            }
            passRefs.Add(new RenderPassRef {
                e = ePassOpaque
            });
            passRefs.Add(new RenderPassRef {
                e = ePassTransparent
            });
            passRefs.Add(new RenderPassRef {
                e = ePassSprites
            });
            passRefs.Add(new RenderPassRef {
                e = ePassUI
            });
        }
Exemple #5
0
        protected override void OnUpdate()
        {
            // go through all known render types, and assign groups to them
            // assign groups to all renderers
            Entities.WithAny <SimpleMeshRenderer, SimpleParticleRenderer>().WithoutBurst().WithStructuralChanges().WithNone <RenderToPasses>().ForEach((Entity e, ref MeshRenderer rlmr) => {
                bool isTransparent = false;
                if (EntityManager.HasComponent <SimpleMaterial>(rlmr.material))
                {
                    isTransparent = EntityManager.GetComponentData <SimpleMaterial>(rlmr.material).transparent;
                }
                FinalizePass(e, isTransparent);
            }).Run();

            Entities.WithAny <LitMeshRenderer, LitParticleRenderer>().WithoutBurst().WithStructuralChanges()
            .WithNone <RenderToPasses>().ForEach((Entity e, ref MeshRenderer rlmr) =>
            {
                bool isTransparent = false;
                if (EntityManager.HasComponent <LitMaterial>(rlmr.material))
                {
                    isTransparent = EntityManager.GetComponentData <LitMaterial>(rlmr.material).transparent;
                }
                FinalizePass(e, isTransparent);
            }).Run();

            Entities.WithAny <LitMeshRenderer>().WithoutBurst().WithStructuralChanges().WithNone <RenderToPasses>().ForEach((Entity e, ref SkinnedMeshRenderer rlsmr) => {
                bool isTransparent = false;
                if (EntityManager.HasComponent <LitMaterial>(rlsmr.material))
                {
                    isTransparent = EntityManager.GetComponentData <LitMaterial>(rlsmr.material).transparent;
                }
                FinalizePass(e, isTransparent);
            }).Run();

            // those are things that do not render anywhere naturally, so add a to passes for gizmos
            // TODO add a GizmoRenderer tag for these
            Entities.WithoutBurst().WithStructuralChanges().WithNone <RenderToPasses>().WithAny <GizmoLight, GizmoCamera, GizmoAutoMovingDirectionalLight>().ForEach((Entity e) => {
                ShadowMask shadowMask = new ShadowMask {
                    mask = ulong.MaxValue
                };
                CameraMask cameraMask = new CameraMask {
                    mask = 0
                };
                Entity eGroup = FindOrCreateRenderGroup(new BuildGroup {
                    passTypes = RenderPassType.Transparent, cameraMask = cameraMask, shadowMask = shadowMask
                });
                OptionalSetSharedComponent(e, new RenderToPasses {
                    e = eGroup
                });
            }).Run();

            Entities.WithoutBurst().WithStructuralChanges().WithNone <RenderToPasses>().WithAny <GizmoDebugOverlayTexture>().ForEach((Entity e) => {
                ShadowMask shadowMask = new ShadowMask {
                    mask = ulong.MaxValue
                };
                CameraMask cameraMask = new CameraMask {
                    mask = 0
                };
                Entity eGroup = FindOrCreateRenderGroup(new BuildGroup {
                    passTypes = RenderPassType.DebugOverlay, cameraMask = cameraMask, shadowMask = shadowMask
                });
                OptionalSetSharedComponent(e, new RenderToPasses {
                    e = eGroup
                });
            }).Run();
        }
        protected override void OnUpdate()
        {
            if (!SceneService.AreStartupScenesLoaded(World))
            {
                return;
            }
#if DEBUG
            var countEntsStart = GetNumEntities();
#endif
            // gather all passes
            var q = GetEntityQuery(ComponentType.ReadOnly <RenderPass>());
            m_allPasses = q.ToEntityArray(Allocator.TempJob);

            // go through lit renderers and assign lighting setup to them
            // create light
            // TEMP HACK
            var    q2        = GetEntityQuery(ComponentType.ReadOnly <LightingBGFX>());
            Entity eLighting = Entity.Null;
            if (q2.CalculateEntityCount() == 0)
            {
                eLighting = EntityManager.CreateEntity();
                EntityManager.AddComponentData(eLighting, new LightingBGFX());
                // add both ways lookup lighting setup <-> light
                var b1 = EntityManager.AddBuffer <LightToBGFXLightingSetup>(eLighting);
                Entities.WithAll <Light>().ForEach((Entity e) =>
                {
                    b1.Add(new LightToBGFXLightingSetup {
                        e = e
                    });
                });
                Entities.WithAll <Light>().ForEach((Entity e) =>
                {
                    var b = EntityManager.AddBuffer <LightToBGFXLightingSetup>(e);
                    b.Add(new LightToBGFXLightingSetup {
                        e = eLighting
                    });
                });
            }
            else
            {
                var a = q2.ToEntityArray(Allocator.TempJob);
                Assert.IsTrue(a.Length == 1);
                eLighting = a[0];
                a.Dispose();
            }
            Entities.WithNone <LightingRef>().ForEach((Entity e, ref MeshRenderer rlmr, ref LitMeshReference meshRef) =>
            {
                OptionalSetSharedComponent(e, new LightingRef {
                    e = eLighting
                });
            });
            // EOH

            m_buildGroups = new NativeHashMap <BuildGroup, Entity>(256, Allocator.TempJob);

            // find existing groups and add them to builder
            Entities.WithAll <RenderGroup>().ForEach((Entity e, ref BuildGroup bg) =>
            {
                m_buildGroups.Add(bg, e);
            });

            // go through all known render types, and assign groups to them
            // assign groups to all renderers
            Entities.WithNone <RenderToPasses>().ForEach((Entity e, ref MeshRenderer rlmr) =>
            {
                bool isTransparent = false;
                if (EntityManager.HasComponent <SimpleMeshReference>(e))
                {
                    if (EntityManager.HasComponent <SimpleMaterial>(rlmr.material))
                    {
                        isTransparent = EntityManager.GetComponentData <SimpleMaterial>(rlmr.material).transparent;
                    }
                    else if (EntityManager.HasComponent <SimpleMaterialBGFX>(rlmr.material))
                    {
                        var matBGFX   = EntityManager.GetComponentData <SimpleMaterialBGFX>(rlmr.material);
                        isTransparent = (matBGFX.state & (ulong)bgfx.StateFlags.WriteZ) != (ulong)bgfx.StateFlags.WriteZ;
                    }
                }
                else if (EntityManager.HasComponent <LitMeshReference>(e))
                {
                    if (EntityManager.HasComponent <LitMaterial>(rlmr.material))
                    {
                        isTransparent = EntityManager.GetComponentData <LitMaterial>(rlmr.material).transparent;
                    }
                    else if (EntityManager.HasComponent <LitMaterialBGFX>(rlmr.material))
                    {
                        var matBGFX   = EntityManager.GetComponentData <LitMaterialBGFX>(rlmr.material);
                        isTransparent = (matBGFX.state & (ulong)bgfx.StateFlags.WriteZ) != (ulong)bgfx.StateFlags.WriteZ;
                    }
                }

                CameraMask cameraMask = new CameraMask {
                    mask = ulong.MaxValue
                };
                if (EntityManager.HasComponent <CameraMask>(e))
                {
                    cameraMask = EntityManager.GetComponentData <CameraMask>(e);
                }
                ShadowMask shadowMask = new ShadowMask {
                    mask = ulong.MaxValue
                };
                if (EntityManager.HasComponent <ShadowMask>(e))
                {
                    shadowMask = EntityManager.GetComponentData <ShadowMask>(e);
                }
                Entity eGroup;
                if (isTransparent)
                {
                    eGroup = FindOrCreateRenderGroup(new BuildGroup {
                        passTypes = RenderPassType.Transparent, cameraMask = cameraMask, shadowMask = shadowMask
                    });
                }
                else
                {
                    eGroup = FindOrCreateRenderGroup(new BuildGroup {
                        passTypes = RenderPassType.Opaque | RenderPassType.ZOnly | RenderPassType.ShadowMap, cameraMask = cameraMask, shadowMask = shadowMask
                    });
                }

                OptionalSetSharedComponent(e, new RenderToPasses {
                    e = eGroup
                });
            });

            Entities.WithNone <RenderToPasses>().ForEach((Entity e, ref GizmoLight rlgmr) =>
            {
                ShadowMask shadowMask = new ShadowMask {
                    mask = ulong.MaxValue
                };
                CameraMask cameraMask = new CameraMask {
                    mask = 0
                };
                Entity eGroup = FindOrCreateRenderGroup(new BuildGroup {
                    passTypes = RenderPassType.Transparent, cameraMask = cameraMask, shadowMask = shadowMask
                });
                OptionalSetSharedComponent(e, new RenderToPasses {
                    e = eGroup
                });
            });


            m_buildGroups.Dispose();
            m_allPasses.Dispose();

            // TODO: remove any passes that are not referenced by anything

#if DEBUG
            var countEntsEnd = GetNumEntities();
            if (countEntsEnd != countEntsStart)
            {
                RenderDebug.LogFormatAlways("Render graph builder added entities (was {0}, now {1})", countEntsStart, countEntsEnd);
            }
#endif
        }