public override void PrepareEffectPermutations(RenderDrawContext context)
        {
            base.PrepareEffectPermutations(context);

            var rootEffectRenderFeature = ((RootEffectRenderFeature)RootRenderFeature);
            var renderEffects           = RootRenderFeature.RenderData.GetData(((RootEffectRenderFeature)RootRenderFeature).RenderEffectKey);
            int effectSlotCount         = ((RootEffectRenderFeature)RootRenderFeature).EffectPermutationSlotCount;

            Dispatcher.ForEach(RootRenderFeature.ObjectNodeReferences, objectNodeReference =>
            {
                var objectNode       = RootRenderFeature.GetObjectNode(objectNodeReference);
                var renderMesh       = (RenderMesh)objectNode.RenderObject;
                var staticObjectNode = renderMesh.StaticObjectNode;

                renderMesh.ActiveMeshDraw = renderMesh.Mesh.Draw;

                foreach (var stage in RenderSystem.RenderStages)
                {
                    if (stage == null)
                    {
                        continue;
                    }
                    var effectSlot             = rootEffectRenderFeature.GetEffectPermutationSlot(stage);
                    var staticEffectObjectNode = staticObjectNode * effectSlotCount + effectSlot.Index;
                    var renderEffect           = renderEffects[staticEffectObjectNode];

                    if (renderEffect != null)
                    {
                        renderEffect.EffectValidator.ValidateParameter(StrideEffectBaseKeys.ComputeVelocityShader, new ShaderClassSource("MeshVelocity"));
                    }
                }
            });
        }
        /// <inheritdoc/>
        public override void Extract()
        {
            if (!Context.VisibilityGroup.Tags.TryGetValue(ModelToInstancingMap, out var modelToInstancingMap))
            {
                return;
            }

            var renderObjectInstancingData = RootRenderFeature.RenderData.GetData(renderObjectInstancingDataInfoKey);

            bufferUploaded.Clear();

            foreach (var objectNodeReference in RootRenderFeature.ObjectNodeReferences)
            {
                var objectNode = RootRenderFeature.GetObjectNode(objectNodeReference);
                var renderMesh = objectNode.RenderObject as RenderMesh;
                if (renderMesh == null)
                {
                    continue;
                }

                var renderModel = renderMesh.RenderModel;
                if (renderModel == null)
                {
                    continue;
                }

                if (!modelToInstancingMap.TryGetValue(renderModel, out var renderInstancing))
                {
                    renderMesh.InstanceCount = 0;
                    continue;
                }

                ref var instancingData = ref renderObjectInstancingData[renderMesh.StaticObjectNode];

                // Instancing data
                if (renderInstancing.InstanceCount > 0)
                {
                    instancingData.InstanceCount              = renderInstancing.InstanceCount;
                    instancingData.ModelTransformUsage        = renderInstancing.ModelTransformUsage;
                    instancingData.BuffersManagedByUser       = renderInstancing.BuffersManagedByUser;
                    instancingData.WorldMatrices              = renderInstancing.WorldMatrices;
                    instancingData.WorldInverseMatrices       = renderInstancing.WorldInverseMatrices;
                    instancingData.InstanceWorldBuffer        = renderInstancing.InstanceWorldBuffer;
                    instancingData.InstanceWorldInverseBuffer = renderInstancing.InstanceWorldInverseBuffer;

                    bufferUploaded[renderInstancing.InstanceWorldBuffer] = renderInstancing.BuffersManagedByUser;
                }
                else
                {
                    instancingData.InstanceCount = 0;
                }

                // Update instance count on mesh
                renderMesh.InstanceCount = instancingData.InstanceCount;
            }
Exemple #3
0
        /// <inheritdoc/>
        public override void Extract()
        {
            var renderModelObjectInfo = RootRenderFeature.RenderData.GetData(RenderModelObjectInfoKey);

            //for (int index = 0; index < RootRenderFeature.ObjectNodeReferences.Count; index++)
            Dispatcher.For(0, RootRenderFeature.ObjectNodeReferences.Count, index =>
            {
                var objectNodeReference = RootRenderFeature.ObjectNodeReferences[index];
                var objectNode          = RootRenderFeature.GetObjectNode(objectNodeReference);
                var renderMesh          = objectNode.RenderObject as RenderMesh;

                // TODO: Extract world
                renderModelObjectInfo[objectNodeReference].World = renderMesh != null ? renderMesh.World : Matrix.Identity;
            });
        }
Exemple #4
0
        /// <param name="context"></param>
        /// <inheritdoc/>
        public override void PrepareEffectPermutations(RenderDrawContext context)
        {
            var skinningInfos = RootRenderFeature.RenderData.GetData(skinningInfoKey);

            var renderEffects   = RootRenderFeature.RenderData.GetData(renderEffectKey);
            int effectSlotCount = ((RootEffectRenderFeature)RootRenderFeature).EffectPermutationSlotCount;

            //foreach (var objectNodeReference in RootRenderFeature.ObjectNodeReferences)
            Dispatcher.ForEach(((RootEffectRenderFeature)RootRenderFeature).ObjectNodeReferences, objectNodeReference =>
            {
                var objectNode       = RootRenderFeature.GetObjectNode(objectNodeReference);
                var renderMesh       = (RenderMesh)objectNode.RenderObject;
                var staticObjectNode = renderMesh.StaticObjectNode;

                ref var skinningInfo = ref skinningInfos[staticObjectNode];
                var parameters       = renderMesh.Mesh.Parameters;
                if (parameters != skinningInfo.Parameters || parameters.PermutationCounter != skinningInfo.PermutationCounter)
                {
                    skinningInfo.Parameters         = parameters;
                    skinningInfo.PermutationCounter = parameters.PermutationCounter;

                    skinningInfo.HasSkinningPosition = parameters.Get(MaterialKeys.HasSkinningPosition);
                    skinningInfo.HasSkinningNormal   = parameters.Get(MaterialKeys.HasSkinningNormal);
                    skinningInfo.HasSkinningTangent  = parameters.Get(MaterialKeys.HasSkinningTangent);
                }

                for (int i = 0; i < effectSlotCount; ++i)
                {
                    var staticEffectObjectNode = staticObjectNode * effectSlotCount + i;
                    var renderEffect           = renderEffects[staticEffectObjectNode];

                    // Skip effects not used during this frame
                    if (renderEffect == null || !renderEffect.IsUsedDuringThisFrame(RenderSystem))
                    {
                        continue;
                    }

                    if (renderMesh.Mesh.Skinning != null)
                    {
                        renderEffect.EffectValidator.ValidateParameter(MaterialKeys.HasSkinningPosition, skinningInfo.HasSkinningPosition);
                        renderEffect.EffectValidator.ValidateParameter(MaterialKeys.HasSkinningNormal, skinningInfo.HasSkinningNormal);
                        renderEffect.EffectValidator.ValidateParameter(MaterialKeys.HasSkinningTangent, skinningInfo.HasSkinningTangent);

                        var skinningBones = Math.Max(MaxBones, renderMesh.Mesh.Skinning.Bones.Length);
                        renderEffect.EffectValidator.ValidateParameter(MaterialKeys.SkinningMaxBones, skinningBones);
                    }
                }
            });
Exemple #5
0
        /// <param name="context"></param>
        /// <inheritdoc/>
        public override unsafe void Prepare(RenderDrawContext context)
        {
            // Compute WorldView, WorldViewProj
            var renderModelObjectInfoData = RootRenderFeature.RenderData.GetData(RenderModelObjectInfoKey);
            var renderModelViewInfoData   = RootRenderFeature.RenderData.GetData(RenderModelViewInfoKey);

            // Update PerFrame (time)
            // TODO Move that to RootEffectRenderFeature?
            foreach (var frameLayout in ((RootEffectRenderFeature)RootRenderFeature).FrameLayouts)
            {
                var timeOffset = frameLayout.GetConstantBufferOffset(time);
                if (timeOffset == -1)
                {
                    continue;
                }

                var resourceGroup = frameLayout.Entry.Resources;
                var mappedCB      = resourceGroup.ConstantBuffer.Data;

                var perFrameTime = (PerFrameTime *)((byte *)mappedCB + timeOffset);
                perFrameTime->Time     = (float)Context.Time.Total.TotalSeconds;
                perFrameTime->TimeStep = (float)Context.Time.Elapsed.TotalSeconds;
            }

            // Update PerView (View, Proj, etc...)
            for (int index = 0; index < RenderSystem.Views.Count; index++)
            {
                var view        = RenderSystem.Views[index];
                var viewFeature = view.Features[RootRenderFeature.Index];

                // Compute WorldView and WorldViewProjection
                Dispatcher.ForEach(viewFeature.ViewObjectNodes, renderPerViewNodeReference =>
                {
                    var renderPerViewNode        = RootRenderFeature.GetViewObjectNode(renderPerViewNodeReference);
                    ref var renderModelFrameInfo = ref renderModelObjectInfoData[renderPerViewNode.ObjectNode];
                    ref var renderModelViewInfo  = ref renderModelViewInfoData[renderPerViewNodeReference];

                    Matrix.Multiply(ref renderModelFrameInfo.World, ref view.View, out renderModelViewInfo.WorldView);
                    Matrix.Multiply(ref renderModelFrameInfo.World, ref view.ViewProjection, out renderModelViewInfo.WorldViewProjection);
                });
        public override unsafe void Prepare(RenderDrawContext context)
        {
            var previousTransformationInfoData     = RootRenderFeature.RenderData.GetData(previousTransformationInfoKey);
            var previousTransformationViewInfoData = RootRenderFeature.RenderData.GetData(previousTransformationViewInfoKey);
            var renderModelObjectInfoData          = RootRenderFeature.RenderData.GetData(renderModelObjectInfoKey);

            // Calculate previous WVP matrix per view and object
            usageCounter++;
            for (int index = 0; index < RenderSystem.Views.Count; index++)
            {
                var view        = RenderSystem.Views[index];
                var viewFeature = view.Features[RootRenderFeature.Index];

                bool useView = false;
                foreach (var stage in RenderSystem.RenderStages)
                {
                    foreach (var renderViewStage in view.RenderStages)
                    {
                        if (renderViewStage.Index == stage.Index && stage.OutputValidator?.Find <VelocityTargetSemantic>() >= 0)
                        {
                            useView = true;
                            break;
                        }
                    }
                }
                if (!useView)
                {
                    continue;
                }

                // Cache per-view data locally
                RenderViewData viewData;
                if (!renderViewDatas.TryGetValue(view, out viewData))
                {
                    viewData = new RenderViewData();
                    renderViewDatas.Add(view, viewData);
                }

                Dispatcher.ForEach(viewFeature.ViewObjectNodes, renderPerViewNodeReference =>
                {
                    var renderPerViewNode    = RootRenderFeature.GetViewObjectNode(renderPerViewNodeReference);
                    var renderModelFrameInfo = renderModelObjectInfoData[renderPerViewNode.ObjectNode];

                    Matrix previousViewProjection = viewData.PreviousViewProjection;

                    Matrix previousWorldViewProjection;
                    Matrix.Multiply(ref renderModelFrameInfo.World, ref previousViewProjection, out previousWorldViewProjection);

                    previousTransformationViewInfoData[renderPerViewNodeReference] = new PreviousObjectViewInfo
                    {
                        WorldViewProjection = previousWorldViewProjection,
                    };
                });

                // Shift current view projection transform into previous
                viewData.PreviousViewProjection = view.ViewProjection;
                viewData.UsageCounter           = usageCounter;
                updatedViews.Add(view);
            }

            foreach (var view in renderViewDatas.Keys.ToArray())
            {
                if (!updatedViews.Contains(view))
                {
                    renderViewDatas.Remove(view);
                }
            }
            updatedViews.Clear();

            // Update cbuffer for previous WVP matrix
            Dispatcher.ForEach(((RootEffectRenderFeature)RootRenderFeature).RenderNodes, (ref RenderNode renderNode) =>
            {
                var perDrawLayout = renderNode.RenderEffect.Reflection?.PerDrawLayout;
                if (perDrawLayout == null)
                {
                    return;
                }

                var previousWvpOffset = perDrawLayout.GetConstantBufferOffset(previousWorldViewProjection);
                if (previousWvpOffset == -1)
                {
                    return;
                }

                var mappedCB        = renderNode.Resources.ConstantBuffer.Data;
                var previousPerDraw = (PreviousPerDraw *)((byte *)mappedCB + previousWvpOffset);

                var renderModelFrameInfo         = renderModelObjectInfoData[renderNode.RenderObject.ObjectNode];
                var renderModelPreviousFrameInfo = previousTransformationViewInfoData[renderNode.ViewObjectNode];

                // Shift current world transform into previous transform
                previousTransformationInfoData[renderNode.RenderObject.StaticObjectNode] = new StaticObjectInfo
                {
                    World = renderModelFrameInfo.World,
                };

                previousPerDraw->PreviousWorldViewProjection = renderModelPreviousFrameInfo.WorldViewProjection;
            });
        }
 /// <summary>
 ///   Attaches this <see cref="SubRenderFeature"/> to a <see cref="Rendering.RootRenderFeature"/>.
 /// </summary>
 /// <param name="rootRenderFeature">The root render feature.</param>
 internal void AttachRootRenderFeature(RootRenderFeature rootRenderFeature)
 {
     RootRenderFeature = rootRenderFeature;
     RenderSystem      = rootRenderFeature.RenderSystem;
 }