internal void SetAnimationBones(Matrix[] simulatedBones)
        {
            if (m_skeletonHierarchy == null)
            {
                return;
            }
            var skeletonCount = m_skeletonHierarchy.Length;

            for (int i = 0; i < skeletonCount; i++)
            {
                m_absoluteTransforms[i] = simulatedBones[i];
            }

            for (int i = 0; i < skeletonCount; i++)
            {
                if (m_skeletonHierarchy[i].Parent != -1)
                {
                    m_absoluteTransforms[i] = m_absoluteTransforms[i] * m_absoluteTransforms[m_skeletonHierarchy[i].Parent];
                }
            }

            int bonesCount = m_skeletonIndices.Length;

            for (int i = 0; i < bonesCount; i++)
            {
                m_skinTransforms[i] = Matrix.Transpose(m_skeletonHierarchy[m_skeletonIndices[i]].SkinTransform * m_absoluteTransforms[m_skeletonIndices[i]]);
            }
        }
Exemplo n.º 2
0
        static unsafe IConstantBuffer GetObjectCB(MyRenderContext RC, MyInstanceComponent instance, float stateData)
        {
            Vector4 col0, col1, col2;

            instance.GetMatrixCols(0, out col0, out col1, out col2);
            Matrix matrix = Matrix.Identity;

            matrix.SetRow(0, col0);
            matrix.SetRow(1, col1);
            matrix.SetRow(2, col2);
            matrix = Matrix.Transpose(matrix);

            int cbSize = sizeof(MyObjectDataCommon);

            cbSize += sizeof(MyObjectDataNonVoxel);

            IConstantBuffer      cb           = MyCommon.GetObjectCB(cbSize);
            var                  mapping      = MyMapping.MapDiscard(RC, cb);
            MyObjectDataNonVoxel nonVoxelData = new MyObjectDataNonVoxel();

            mapping.WriteAndPosition(ref nonVoxelData);
            MyObjectDataCommon commonData = new MyObjectDataCommon();

            commonData.LocalMatrix = matrix;
            commonData.ColorMul    = Vector3.One;
            commonData.KeyColor    = new Vector3(0, -1f, 0f);
            commonData.CustomAlpha = stateData;

            mapping.WriteAndPosition(ref commonData);
            mapping.Unmap();
            return(cb);
        }
Exemplo n.º 3
0
        internal static void WriteSpotlightConstants(LightId lid, ref SpotlightConstants data)
        {
            data.ApertureCos  = Spotlights[lid.Index].ApertureCos;
            data.Range        = Spotlights[lid.Index].Range;
            data.Color        = Spotlights[lid.Index].Color;
            data.Direction    = Spotlights[lid.Index].Direction;
            data.Up           = Spotlights[lid.Index].Up;
            data.GlossFactor  = Spotlights[lid.Index].GlossFactor;
            data.ShadowsRange = Lights.Data[lid.Index].CastsShadowsThisFrame ? Lights.Data[lid.Index].ShadowsDistance : 0;
            data.Position     = Lights.Data[lid.Index].SpotPosition - MyRender11.Environment.CameraPosition;

            float ratio = (float)Math.Sqrt(1 - data.ApertureCos * data.ApertureCos) / data.ApertureCos;
            float h     = ratio * data.Range;

            Matrix viewProjAt0 = MyRender11.Environment.ViewProjectionAt0;

            if (MyStereoRender.Enable)
            {
                if (MyStereoRender.RenderRegion == MyStereoRegion.LEFT)
                {
                    viewProjAt0 = MyStereoRender.EnvMatricesLeftEye.ViewProjectionAt0;
                }
                if (MyStereoRender.RenderRegion == MyStereoRegion.RIGHT)
                {
                    viewProjAt0 = MyStereoRender.EnvMatricesRightEye.ViewProjectionAt0;
                }
            }
            data.ProxyWorldViewProj = Matrix.Transpose(Matrix.CreateScale(2 * h, 2 * h, data.Range) *
                                                       Matrix.CreateWorld(data.Position, data.Direction, data.Up) *
                                                       viewProjAt0);

            data.ShadowMatrix = CreateShadowMatrix(lid);
        }
Exemplo n.º 4
0
        internal static Matrix CreateShadowMatrix(LightId id)
        {
            var     worldMatrix         = MatrixD.CreateTranslation(MyEnvironment.CameraPosition);
            MatrixD worldViewProjection = worldMatrix * GetSpotlightViewProjection(id);

            return(Matrix.Transpose(worldViewProjection * MyMatrixHelpers.ClipspaceToTexture));
        }
Exemplo n.º 5
0
        static void TransferDataCustomProjections()
        {
            var mapping = MyMapping.MapDiscard(RC, m_cbCustomProjections);

            for (int i = 0; i < MyRenderProxy.BillboardsViewProjectionRead.Count; i++)
            {
                MyBillboardViewProjection viewprojection = MyRenderProxy.BillboardsViewProjectionRead[i];

                var scaleX  = viewprojection.Viewport.Width / (float)MyRender11.ViewportResolution.X;
                var scaleY  = viewprojection.Viewport.Height / (float)MyRender11.ViewportResolution.Y;
                var offsetX = viewprojection.Viewport.OffsetX / (float)MyRender11.ViewportResolution.X;
                var offsetY = (MyRender11.ViewportResolution.Y - viewprojection.Viewport.OffsetY - viewprojection.Viewport.Height)
                              / (float)MyRender11.ViewportResolution.Y;

                var viewportTransformation = new Matrix(
                    scaleX, 0, 0, 0,
                    0, scaleY, 0, 0,
                    0, 0, 1, 0,
                    offsetX, offsetY, 0, 1
                    );

                var transpose = Matrix.Transpose(viewprojection.ViewAtZero * viewprojection.Projection * viewportTransformation);
                mapping.WriteAndPosition(ref transpose);
            }

            for (int i = MyRenderProxy.BillboardsViewProjectionRead.Count; i < MAX_CUSTOM_PROJECTIONS_SIZE; i++)
            {
                mapping.WriteAndPosition(ref Matrix.Identity);
            }

            mapping.Unmap();
        }
Exemplo n.º 6
0
        internal override void Draw()
        {
            var RC = MyImmediateRC.RC;


            RC.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            var mapping = MyMapping.MapDiscard(RC.Context, MyCommon.ProjectionConstants.Buffer);

            mapping.stream.Write(Matrix.Transpose(MyEnvironment.ViewProjection));
            mapping.Unmap();

            RC.Context.Rasterizer.SetViewport(0, 0, MyRender.ViewportResolution.X, MyRender.ViewportResolution.Y);

            RC.Context.PixelShader.SetSamplers(0, MyRender.StandardSamplers);

            RC.SetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants.Buffer);
            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants.Buffer);

            RC.SetVS(m_vs);
            RC.SetPS(m_ps);

            RC.BindRawSRV(0, m_indirectionBuffer.m_SRV, m_instanceBuffer.m_SRV, m_indicesBuffer.m_SRV, m_vertexPositionBuffer.m_SRV, m_vertexNormalBuffer.m_SRV);

            RC.BindGBufferForWrite(MyGBuffer.Main);

            RC.Context.Draw(m_pageSize * m_pageTable.Count, 0);
        }
Exemplo n.º 7
0
        internal unsafe void SetProxyConstants(MyRenderableProxy proxy)
        {
            RC.SetCB(MyCommon.OBJECT_SLOT, proxy.ObjectBuffer);

            MyMapping mapping;

            bool constantsChange = true;

            fixed(void *ptr0 = &Locals.objectData)
            {
                fixed(void *ptr1 = &proxy.ObjectData)
                {
                    constantsChange = !SharpDX.Utilities.CompareMemory(new IntPtr(ptr0), new IntPtr(ptr1), sizeof(MyObjectData));
                }
            }

            if (!constantsChange &&
                proxy.SkinningMatrices == null &&
                Locals.objectBuffer == proxy.ObjectBuffer)
            {
            }
            else
            {
                Locals.objectData   = proxy.ObjectData;
                Locals.objectBuffer = proxy.ObjectBuffer;

                MyObjectData objectData = proxy.ObjectData;
                //objectData.Translate(-MyEnvironment.CameraPosition);

                mapping = MyMapping.MapDiscard(RC.Context, proxy.ObjectBuffer);
                void *ptr = &objectData;
                mapping.stream.Write(new IntPtr(ptr), 0, sizeof(MyObjectData));

                if (proxy.SkinningMatrices != null)
                {
                    if (proxy.DrawSubmesh.BonesMapping == null)
                    {
                        for (int j = 0; j < Math.Min(MyRender11Constants.SHADER_MAX_BONES, proxy.SkinningMatrices.Length); j++)
                        {
                            mapping.stream.Write(Matrix.Transpose(proxy.SkinningMatrices[j]));
                        }
                    }
                    else
                    {
                        for (int j = 0; j < proxy.DrawSubmesh.BonesMapping.Length; j++)
                        {
                            mapping.stream.Write(Matrix.Transpose(proxy.SkinningMatrices[proxy.DrawSubmesh.BonesMapping[j]]));
                        }
                    }
                }

                mapping.Unmap();

                Stats.ObjectConstantsChanges++;
            }
        }
Exemplo n.º 8
0
        private static void SetupCameraMatrices(MyRenderMessageSetCameraViewMatrix message)
        {
            var viewMatrixAt0 = message.ViewMatrix;

            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            var originalProjection = message.ProjectionMatrix;
            //var invOriginalProjection = Matrix.CreatePerspectiveFovRhInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane, message.FarPlane);
            var renderProjection = Matrix.CreatePerspectiveFieldOfView(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.FarPlane, message.NearPlane);
            var invProj          = Matrix.CreatePerspectiveFovRhInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.FarPlane, message.NearPlane);

            renderProjection = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane);
            invProj          = Matrix.CreatePerspectiveFovRhInfiniteComplementaryInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane);

            var invView = Matrix.Transpose(viewMatrixAt0);

            invView.M41 = (float)message.CameraPosition.X;
            invView.M42 = (float)message.CameraPosition.Y;
            invView.M43 = (float)message.CameraPosition.Z;

            MyEnvironment.ViewAt0              = viewMatrixAt0;
            MyEnvironment.InvViewAt0           = Matrix.Transpose(viewMatrixAt0);
            MyEnvironment.ViewProjectionAt0    = viewMatrixAt0 * renderProjection;
            MyEnvironment.InvViewProjectionAt0 = invProj * Matrix.Transpose(viewMatrixAt0);
            message.CameraPosition.AssertIsValid();
            MyEnvironment.CameraPosition      = message.CameraPosition;
            MyEnvironment.View                = message.ViewMatrix;
            MyEnvironment.ViewD               = message.ViewMatrix;
            MyEnvironment.OriginalProjectionD = originalProjection;
            MyEnvironment.InvView             = invView;
            MyEnvironment.ViewProjection      = message.ViewMatrix * renderProjection;
            MyEnvironment.InvViewProjection   = invProj * invView;
            MyEnvironment.Projection          = renderProjection;
            MyEnvironment.InvProjection       = invProj;

            MyEnvironment.ViewProjectionD = MyEnvironment.ViewD * (MatrixD)renderProjection;

            MyEnvironment.NearClipping             = message.NearPlane;
            MyEnvironment.FarClipping              = message.FarPlane;
            MyEnvironment.LargeDistanceFarClipping = message.FarPlane * 500.0f;
            MyEnvironment.FovY = message.FOV;

            MyUtils.Init(ref MyEnvironment.ViewFrustumD);
            MyEnvironment.ViewFrustumD.Matrix = MyEnvironment.ViewProjectionD;

            MyUtils.Init(ref MyEnvironment.ViewFrustumClippedD);
            MyEnvironment.ViewFrustumClippedD.Matrix = MyEnvironment.ViewD * MyEnvironment.OriginalProjectionD;
        }
Exemplo n.º 9
0
        private static void BeginDrawGBufferPass(MyRenderContext rc)
        {
            SetViewport(rc, MyStereoRegion.LEFT);

            var viewProjTranspose = Matrix.Transpose(EnvMatricesLeftEye.ViewProjectionAt0);
            var mapping           = MyMapping.MapDiscard(rc, MyCommon.ProjectionConstants);

            mapping.WriteAndPosition(ref viewProjTranspose);
            mapping.Unmap();
            rc.AllShaderStages.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);
            //RC.AllShaderStages.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.VRFrameConstantsLeftEye);
        }
Exemplo n.º 10
0
        private static void EndDrawGBufferPass(MyRenderContext RC)
        {
            SetViewport(RC, MyStereoRegion.FULLSCREEN);

            var viewProjTranspose = Matrix.Transpose(MyRender11.Environment.ViewProjectionAt0);
            var mapping           = MyMapping.MapDiscard(RC.DeviceContext, MyCommon.ProjectionConstants);

            mapping.WriteAndPosition(ref viewProjTranspose);
            mapping.Unmap();
            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);
            //RC.SetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
        }
Exemplo n.º 11
0
        private static void SwitchDrawGBufferPass(MyRenderContext RC)
        {
            SetViewport(RC, MyStereoRegion.RIGHT);

            var viewProjTranspose = Matrix.Transpose(EnvMatricesRightEye.ViewProjectionAt0);
            var mapping           = MyMapping.MapDiscard(RC.DeviceContext, MyCommon.ProjectionConstants);

            mapping.WriteAndPosition(ref viewProjTranspose);
            mapping.Unmap();
            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);
            //RC.SetCB(MyCommon.FRAME_SLOT, MyCommon.VRFrameConstantsRightEye);
        }
Exemplo n.º 12
0
        private static Matrix GetMatrixEyeTranslation(bool isLeftEye, Matrix view)
        {
            float Ipd_2 = 0.2f;

            if (MyOpenVR.Static != null)
            {
                Ipd_2 = MyOpenVR.Ipd_2;
            }

            var invViewMatrix = Matrix.Transpose(view);
            var eyePosition   = (!isLeftEye ? invViewMatrix.Left : invViewMatrix.Right) * Ipd_2;

            return(Matrix.CreateTranslation(eyePosition));
        }
Exemplo n.º 13
0
        internal static void Draw(MyBindableResource depth)
        {
            RC.SetupScreenViewport();
            RC.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            RC.SetIL(m_inputLayout);

            RC.SetRS(MyRender11.m_nocullRasterizerState);
            RC.SetDS(MyDepthStencilState.DefaultDepthState);

            RC.SetVS(m_vs);
            RC.SetPS(m_ps);

            RC.BindDepthRT(depth, DepthStencilAccess.ReadOnly, MyRender11.Backbuffer);

            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            RC.SetBS(MyRender11.BlendTransparent);

            SortTransparent();
            var mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants);

            mapping.stream.Write(Matrix.Transpose(MyEnvironment.ViewProjectionAt0));
            mapping.Unmap();

            CheckBufferSize(m_vertexList.Count);

            RC.SetVB(0, m_VB.Buffer, m_VB.Stride);

            DataStream stream;

            RC.Context.MapSubresource(m_VB.Buffer, MapMode.WriteDiscard, MapFlags.None, out stream);
            for (int i = 0; i < m_vertexList.Count; i++)
            {
                stream.Write(m_vertexList[i]);
            }
            RC.Context.UnmapSubresource(m_VB.Buffer, 0);
            stream.Dispose();

            RC.Context.Draw(m_vertexList.Count, 0);

            RC.SetBS(null);

            m_vertexList.Clear();
            m_postSortVertexList.Clear();
            m_triangleSortDistance.Clear();
            m_sortedIndices.Clear();
        }
Exemplo n.º 14
0
        static unsafe void TransferData()
        {
            var mapping = MyMapping.MapDiscard(RC.Context, MyCommon.GetObjectCB(sizeof(Matrix) * MaxCustomProjections));

            for (int i = 0; i < MyRenderProxy.BillboardsViewProjectionRead.Count; i++)
            {
                var view       = MyRenderProxy.BillboardsViewProjectionRead[i].View;
                var projection = MyRenderProxy.BillboardsViewProjectionRead[i].Projection;

                var scaleX  = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Width / (float)MyRender11.ViewportResolution.X;
                var scaleY  = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height / (float)MyRender11.ViewportResolution.Y;
                var offsetX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetX / (float)MyRender11.ViewportResolution.X;
                var offsetY = (MyRender11.ViewportResolution.Y - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetY - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height)
                              / (float)MyRender11.ViewportResolution.Y;

                var viewportTransformation = new Matrix(
                    scaleX, 0, 0, 0,
                    0, scaleY, 0, 0,
                    0, 0, 1, 0,
                    offsetX, offsetY, 0, 1
                    );

                mapping.stream.Write(Matrix.Transpose(view * projection * viewportTransformation));
            }
            for (int i = MyRenderProxy.BillboardsViewProjectionRead.Count; i < MaxCustomProjections; i++)
            {
                mapping.stream.Write(Matrix.Identity);
            }
            mapping.Unmap();

            mapping = MyMapping.MapDiscard(RC.Context, m_VB.Buffer);
            fixed(void *ptr = m_vertexData)
            {
                mapping.stream.Write(new IntPtr(ptr), 0, (sizeof(MyVertexFormatPosition) * MaxBillboards * 4));
            }

            mapping.Unmap();

            mapping = MyMapping.MapDiscard(RC.Context, m_SB.Buffer);
            fixed(void *ptr = m_billboardData)
            {
                mapping.stream.Write(new IntPtr(ptr), 0, (sizeof(MyBillboardData) * MaxBillboards));
            }

            mapping.Unmap();
        }
Exemplo n.º 15
0
        internal static void WriteSpotlightConstants(LightId lid, ref SpotlightConstants data)
        {
            data.ApertureCos  = Spotlights[lid.Index].ApertureCos;
            data.Range        = Spotlights[lid.Index].Range;
            data.Color        = Spotlights[lid.Index].Color;
            data.Direction    = Spotlights[lid.Index].Direction;
            data.Up           = Spotlights[lid.Index].Up;
            data.ShadowsRange = Lights.Data[lid.Index].CastsShadows ? Lights.Data[lid.Index].ShadowsDistance : 0;
            data.Position     = Lights.Data[lid.Index].Position - MyEnvironment.CameraPosition;

            float ratio = (float)Math.Sqrt(1 - data.ApertureCos * data.ApertureCos) / data.ApertureCos;
            float h     = ratio * data.Range;

            data.ProxyWorldViewProj = Matrix.Transpose(Matrix.CreateScale(2 * h, 2 * h, data.Range) *
                                                       Matrix.CreateWorld(data.Position, data.Direction, data.Up) *
                                                       MyEnvironment.ViewProjectionAt0);
        }
Exemplo n.º 16
0
        internal void SetAnimationBones(Matrix[] boneAbsoluteTransforms, IReadOnlyList <MyBoneDecalUpdate> boneDecals)
        {
            if (m_skeletonHierarchy == null)
            {
                return;
            }

            var skeletonCount = m_skeletonHierarchy.Length;

            for (int i = 0; i < skeletonCount; i++)
            {
                m_absoluteTransforms[i] = boneAbsoluteTransforms[i];
            }

            int bonesCount = m_skeletonIndices.Length;

            for (int i = 0; i < bonesCount; i++)
            {
                m_skinTransforms[i] = Matrix.Transpose(m_skeletonHierarchy[m_skeletonIndices[i]].SkinTransform * m_absoluteTransforms[m_skeletonIndices[i]]);
            }

            m_decalUpdateCache.Clear();
            for (int it = 0; it < boneDecals.Count; it++)
            {
                MyBoneDecalUpdate pair = boneDecals[it];

                MyDecalTopoData data;
                bool            found = MyScreenDecals.GetDecalTopoData(pair.DecalID, out data);
                if (!found)
                {
                    continue;
                }

                Matrix skinningTrans = ComputeSkinning(data.BoneIndices, ref data.BoneWeights);
                Matrix transform     = data.MatrixBinding * skinningTrans;
                m_decalUpdateCache.Add(new MyDecalPositionUpdate()
                {
                    ID = pair.DecalID, Transform = transform
                });
            }

            MyScreenDecals.UpdateDecals(m_decalUpdateCache);
        }
        static unsafe void TransferData()
        {
            var mapping = MyMapping.MapDiscard(RC.DeviceContext, MyCommon.GetObjectCB(sizeof(Matrix) * MaxCustomProjections));

            for (int i = 0; i < MyRenderProxy.BillboardsViewProjectionRead.Count; i++)
            {
                var view       = MyRenderProxy.BillboardsViewProjectionRead[i].View;
                var projection = MyRenderProxy.BillboardsViewProjectionRead[i].Projection;

                var scaleX  = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Width / (float)MyRender11.ViewportResolution.X;
                var scaleY  = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height / (float)MyRender11.ViewportResolution.Y;
                var offsetX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetX / (float)MyRender11.ViewportResolution.X;
                var offsetY = (MyRender11.ViewportResolution.Y - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetY - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height)
                              / (float)MyRender11.ViewportResolution.Y;

                var viewportTransformation = new Matrix(
                    scaleX, 0, 0, 0,
                    0, scaleY, 0, 0,
                    0, 0, 1, 0,
                    offsetX, offsetY, 0, 1
                    );

                var transpose = Matrix.Transpose(view * projection * viewportTransformation);
                mapping.WriteAndPosition(ref transpose);
            }
            for (int i = MyRenderProxy.BillboardsViewProjectionRead.Count; i < MaxCustomProjections; i++)
            {
                mapping.WriteAndPosition(ref Matrix.Identity);
            }
            mapping.Unmap();

            int billboardCount = m_sorted + m_unsorted;

            mapping = MyMapping.MapDiscard(RC.DeviceContext, m_VB.Buffer);
            mapping.WriteAndPosition(m_vertexData, 0, billboardCount * 4);
            mapping.Unmap();

            mapping = MyMapping.MapDiscard(RC.DeviceContext, m_SB.Buffer);
            mapping.WriteAndPosition(m_billboardData, 0, billboardCount);
            mapping.Unmap();
        }
Exemplo n.º 18
0
        private static void SetupCameraMatrices(MyRenderMessageSetCameraViewMatrix message)
        {
            var viewMatrixAt0 = message.ViewMatrix;

            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            var originalProjection      = message.ProjectionMatrix;
            var invOriginalProjection   = Matrix.CreatePerspectiveFovInv(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane, message.FarPlane);
            var complementaryProjection = Matrix.CreatePerspectiveFieldOfView(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.FarPlane, message.NearPlane);
            var invProj = Matrix.CreatePerspectiveFovInv(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.FarPlane, message.NearPlane);

            var invView = Matrix.Transpose(viewMatrixAt0);

            invView.M41 = (float)message.CameraPosition.X;
            invView.M42 = (float)message.CameraPosition.Y;
            invView.M43 = (float)message.CameraPosition.Z;

            MyEnvironment.ViewAt0              = viewMatrixAt0;
            MyEnvironment.InvViewAt0           = Matrix.Transpose(viewMatrixAt0);
            MyEnvironment.ViewProjectionAt0    = viewMatrixAt0 * complementaryProjection;
            MyEnvironment.InvViewProjectionAt0 = invProj * Matrix.Transpose(viewMatrixAt0);
            message.CameraPosition.AssertIsValid();
            MyEnvironment.CameraPosition    = message.CameraPosition;
            MyEnvironment.View              = message.ViewMatrix;
            MyEnvironment.InvView           = invView;
            MyEnvironment.ViewProjection    = message.ViewMatrix * complementaryProjection;
            MyEnvironment.InvViewProjection = invProj * invView;
            MyEnvironment.Projection        = complementaryProjection;
            MyEnvironment.InvProjection     = invProj;

            MyEnvironment.NearClipping = message.NearPlane;
            MyEnvironment.FarClipping  = message.FarPlane;
            MyEnvironment.FovY         = message.FOV;
            MyEnvironment.ViewFrustum  = new BoundingFrustum(MyEnvironment.ViewProjection);
        }
Exemplo n.º 19
0
        internal virtual void Begin()
        {
            if (Locals == null)
            {
                Locals = new MyPassLocals();
            }
            Locals.Clear();

            //if (!m_isImmediate)
            //{
            //    //Debug.Assert(m_RC == null);
            //    //m_RC = MyRenderContextPool.AcquireRC();
            //}

            var mapping = MyMapping.MapDiscard(RC.Context, MyCommon.ProjectionConstants);

            mapping.stream.Write(Matrix.Transpose(ViewProjection));
            mapping.Unmap();

            // common settings
            Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            Context.Rasterizer.SetViewport(Viewport.OffsetX, Viewport.OffsetY, Viewport.Width, Viewport.Height);

            Context.PixelShader.SetSamplers(0, MyRender11.StandardSamplers);

            RC.SetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);
            RC.SetCB(MyCommon.ALPHAMASK_VIEWS_SLOT, MyCommon.AlphamaskViewsConstants);

            Context.PixelShader.SetShaderResource(MyCommon.DITHER_8X8_SLOT, MyTextures.Views[MyTextures.Dithering8x8TexId.Index]);

            if (MyBigMeshTable.Table.m_IB != null)
            {
                RC.VSBindSRV(MyCommon.BIG_TABLE_INDICES,
                             MyBigMeshTable.Table.m_IB.Srv,
                             MyBigMeshTable.Table.m_VB_positions.Srv,
                             MyBigMeshTable.Table.m_VB_rest.Srv);
            }
        }
Exemplo n.º 20
0
        // http://web.stanford.edu/class/cs248/pdf/class_13_skinning.pdf
        private Matrix ComputeSkinning(Vector4UByte indices, ref VRageMath.Vector4 weights)
        {
            // TODO: Optmize
            Matrix ret = new Matrix();

            for (int it = 0; it < 4; it++)
            {
                float weight = weights[it];
                if (weight == 0)
                {
                    break;
                }

                // NOTE: m_skinTransforms are already transposed
                Matrix transform;
                Matrix.Transpose(ref m_skinTransforms[m_skeletonIndices[indices[it]]], out transform);

                transform *= weight;
                ret       += transform;
            }

            return(ret);
        }
Exemplo n.º 21
0
        internal override void Draw()
        {
            var RC = MyImmediateRC.RC;

            RC.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            var mapping = MyMapping.MapDiscard(RC.Context, MyCommon.ProjectionConstants.Buffer);

            mapping.stream.Write(Matrix.Transpose(MyEnvironment.ViewProjection));
            mapping.Unmap();

            RC.Context.Rasterizer.SetViewport(0, 0, MyRender.ViewportResolution.X, MyRender.ViewportResolution.Y);

            RC.Context.PixelShader.SetSamplers(0, MyRender.StandardSamplers);

            RC.SetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants.Buffer);
            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants.Buffer);

            RC.SetVS(m_vs);
            RC.SetPS(m_ps);
            RC.SetIL(m_inputLayout);

            RC.BindGBufferForWrite(MyGBuffer.Main);

            foreach (var m in m_meshes)
            {
                RC.BindRawSRV(0, m.Value.m_instanceBuffer.m_SRV);

                var meshInfo = MyAssetsLoader.GetModel(m.Key).LODs[0].m_meshInfo;

                RC.SetVB(meshInfo.VB.Select(x => x.Buffer).ToArray(), meshInfo.VB.Select(x => x.Stride).ToArray());
                RC.SetIB(meshInfo.IB.Buffer, meshInfo.IB.Format);

                RC.Context.DrawIndexedInstanced(meshInfo.IndicesNum, m.Value.instancesCounter, 0, 0, 0);
            }
        }
Exemplo n.º 22
0
        internal static unsafe void Draw(MyBindableResource renderTarget, MyBindableResource depth)
        {
            RC.SetupScreenViewport();
            RC.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
            RC.SetIL(m_inputLayout);

            RC.SetRS(MyRender11.m_linesRasterizerState);

            RC.SetVS(m_vs);
            RC.SetPS(m_ps);

            RC.SetDS(MyDepthStencilState.DefaultDepthState);

            CheckBufferSize(m_vertices.Count);
            RC.SetVB(0, m_VB.Buffer, m_VB.Stride);

            RC.BindDepthRT(depth, DepthStencilAccess.ReadOnly, renderTarget);

            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            if (m_batches.Count > 0)
            {
                var mapping = MyMapping.MapDiscard(m_VB.Buffer);
                mapping.WriteAndPosition(m_vertices.GetInternalArray(), 0, m_vertices.Count);
                mapping.Unmap();

                Matrix prevMatrix = Matrix.Zero;
                foreach (var batch in m_batches)
                {
                    Matrix matrix;
                    if (batch.CustomViewProjection.HasValue)
                    {
                        matrix = batch.CustomViewProjection.Value;
                    }
                    else
                    {
                        matrix = MyEnvironment.ViewProjectionAt0;
                    }

                    if (prevMatrix != matrix)
                    {
                        prevMatrix = matrix;
                        var transpose = Matrix.Transpose(matrix);

                        mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants);
                        mapping.WriteAndPosition(ref transpose);
                        mapping.Unmap();
                    }

                    if (batch.IgnoreDepth)
                    {
                        RC.SetDS(MyDepthStencilState.IgnoreDepthStencil);
                    }
                    else
                    {
                        RC.SetDS(MyDepthStencilState.DefaultDepthState);
                    }

                    RC.DeviceContext.Draw(batch.VertexCount, batch.StartVertex);
                }
            }

            RC.SetDS(null);
            RC.SetRS(null);

            m_vertices.Clear();

            foreach (var batch in m_batches)
            {
                m_batchesPool.Deallocate(batch);
            }
            m_batches.Clear();
        }
Exemplo n.º 23
0
        internal unsafe override sealed void RecordCommands(MyRenderableProxy proxy)
        {
            if (proxy.Mesh.Buffers.IB == IndexBufferId.NULL || proxy.Draw.IndexCount == 0 || (proxy.flags & MyRenderableProxyFlags.SkipInMainView) > 0)
            {
                return;
            }

            Stats.Meshes++;

            SetProxyConstants(proxy);
            BindProxyGeometry(proxy);

            Debug.Assert(proxy.Shaders.VS != null);

            RC.BindShaders(proxy.Shaders);

            if ((proxy.flags & MyRenderableProxyFlags.DisableFaceCulling) > 0)
            {
                RC.SetRS(MyRender11.m_nocullRasterizerState);
            }
            else
            {
                RC.SetRS(null);
            }

//#if DEBUG
            if (MyRender11.Settings.Wireframe)
            {
                if ((proxy.flags & MyRenderableProxyFlags.DisableFaceCulling) > 0)
                {
                    RC.SetRS(MyRender11.m_nocullWireframeRasterizerState);
                }
                else
                {
                    RC.SetRS(MyRender11.m_wireframeRasterizerState);
                }
            }
//#endif

            //for (int i = 0; i < proxy.submeshes.Length; i++)
            //{
            Stats.Submeshes++;
            var submesh = proxy.Draw;

            //if (submesh.Material != null && submesh.Material.TexturesHash != Locals.matTexturesID)
            //{
            //    Locals.matTexturesID = submesh.Material.TexturesHash;
            //    RC.BindRawSRV(submesh.Material.SRVs);
            //}

            if (submesh.MaterialId != Locals.matTexturesID)
            {
                Locals.matTexturesID = submesh.MaterialId;
                var material = MyMaterials1.ProxyPool.Data[submesh.MaterialId.Index];
                RC.MoveConstants(ref material.MaterialConstants);
                RC.SetConstants(ref material.MaterialConstants, MyCommon.MATERIAL_SLOT);
                RC.SetSRVs(ref material.MaterialSRVs);
            }

            //if (submesh.Material != null && submesh.Material.ConstantsHash != Locals.matConstantsID && submesh.Material.ConstantsBuffer != null)
            //{
            //    Stats.MaterialConstantsChanges++;
            //    Locals.matConstantsID = submesh.Material.ConstantsHash;

            //    RC.SetCB(MyCommon.MATERIAL_SLOT, submesh.Material.ConstantsBuffer);

            //    var mapping = MyMapping.MapDiscard(RC.Context, submesh.Material.ConstantsBuffer);
            //    mapping.stream.WriteRange(submesh.Material.Constants);
            //    mapping.Unmap();
            //}

            if (proxy.skinningMatrices != null)
            {
                Stats.ObjectConstantsChanges++;

                MyObjectData objectData = proxy.ObjectData;
                //objectData.Translate(-MyEnvironment.CameraPosition);

                MyMapping mapping;
                mapping = MyMapping.MapDiscard(RC.Context, proxy.objectBuffer);
                void *ptr = &objectData;
                mapping.stream.Write(new IntPtr(ptr), 0, sizeof(MyObjectData));

                if (proxy.skinningMatrices != null)
                {
                    if (submesh.BonesMapping == null)
                    {
                        for (int j = 0; j < Math.Min(MyRender11Constants.SHADER_MAX_BONES, proxy.skinningMatrices.Length); j++)
                        {
                            mapping.stream.Write(Matrix.Transpose(proxy.skinningMatrices[j]));
                        }
                    }
                    else
                    {
                        for (int j = 0; j < submesh.BonesMapping.Length; j++)
                        {
                            mapping.stream.Write(Matrix.Transpose(proxy.skinningMatrices[submesh.BonesMapping[j]]));
                        }
                    }
                }

                mapping.Unmap();
            }

            if (proxy.instanceCount == 0 && submesh.IndexCount > 0)
            {
                RC.Context.DrawIndexed(submesh.IndexCount, submesh.StartIndex, submesh.BaseVertex);
                RC.Stats.DrawIndexed++;
                Stats.Instances++;
                Stats.Triangles += submesh.IndexCount / 3;
            }
            else if (submesh.IndexCount > 0)
            {
                RC.Context.DrawIndexedInstanced(submesh.IndexCount, proxy.instanceCount, submesh.StartIndex, submesh.BaseVertex, proxy.startInstance);
                RC.Stats.DrawIndexedInstanced++;
                Stats.Instances += proxy.instanceCount;
                Stats.Triangles += proxy.instanceCount * submesh.IndexCount / 3;
            }
            //}
        }
Exemplo n.º 24
0
        private static void SetupCameraMatricesInternal(MyRenderMessageSetCameraViewMatrix message, MyEnvironmentMatrices envMatrices, MyStereoRegion typeofEnv)
        {
            var originalProjection = message.ProjectionMatrix;
            var viewMatrix         = message.ViewMatrix;
            var cameraPosition     = message.CameraPosition;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null && message.LastMomentUpdateIndex != 0)
                {
                    MatrixD origin = MatrixD.Identity;
                    MyOpenVR.LMUMatrixGetOrigin(ref origin, message.LastMomentUpdateIndex);
                    viewMatrix = MatrixD.Invert(origin);
                }
            }

            var viewMatrixAt0 = viewMatrix;

            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null)
                {
                    if (message.LastMomentUpdateIndex != 0)
                    {
                        var tViewMatrix = Matrix.Transpose(viewMatrix);
                        var viewHMDat0  = MyOpenVR.ViewHMD;
                        viewHMDat0.M14 = 0;
                        viewHMDat0.M24 = 0;
                        viewHMDat0.M34 = 0;
                        viewHMDat0.M41 = 0;
                        viewHMDat0.M42 = 0;
                        viewHMDat0.M43 = 0;
                        viewHMDat0.M44 = 1;

                        //cameraPosition += tViewMatrix.Up * MyOpenVR.ViewHMD.Translation.Y;
                        //cameraPosition += tViewMatrix.Backward * MyOpenVR.ViewHMD.Translation.X;
                        //cameraPosition += tViewMatrix.Right * MyOpenVR.ViewHMD.Translation.Z;

                        viewMatrixAt0 = viewMatrixAt0 * viewHMDat0;
                        viewMatrix    = viewMatrix * viewHMDat0;

                        if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                        }
                        else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                        }
                    }
                }
                else
                {
                    if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                    }
                    else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                    }
                }
            }

            float aspectRatio = MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y;

            if (typeofEnv != MyStereoRegion.FULLSCREEN)
            {
                aspectRatio /= 2;
            }
            Matrix projMatrix = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, aspectRatio, message.NearPlane);

            cameraPosition.AssertIsValid();

            envMatrices.ViewAt0              = viewMatrixAt0;
            envMatrices.InvViewAt0           = Matrix.Invert(viewMatrixAt0);
            envMatrices.ViewProjectionAt0    = viewMatrixAt0 * projMatrix;
            envMatrices.InvViewProjectionAt0 = Matrix.Invert(viewMatrixAt0 * projMatrix);
            envMatrices.CameraPosition       = cameraPosition;
            envMatrices.View  = viewMatrix;
            envMatrices.ViewD = viewMatrix;
            envMatrices.OriginalProjectionD      = originalProjection;
            envMatrices.InvView                  = Matrix.Invert(viewMatrix);
            envMatrices.ViewProjection           = viewMatrix * projMatrix;
            envMatrices.InvViewProjection        = Matrix.Invert(viewMatrix * projMatrix);
            envMatrices.Projection               = projMatrix;
            envMatrices.InvProjection            = Matrix.Invert(projMatrix);
            envMatrices.ViewProjectionD          = envMatrices.ViewD * (MatrixD)projMatrix;
            envMatrices.NearClipping             = message.NearPlane;
            envMatrices.FarClipping              = message.FarPlane;
            envMatrices.LargeDistanceFarClipping = message.FarPlane * 500.0f;

            int   width  = MyRender11.ViewportResolution.X;
            int   height = MyRender11.ViewportResolution.Y;
            float fovH   = message.FOV;

            envMatrices.FovH = fovH;
            envMatrices.FovV = (float)(2 * Math.Atan(Math.Tan(fovH / 2.0) * (height / (double)width)));

            MyUtils.Init(ref envMatrices.ViewFrustumD);
            envMatrices.ViewFrustumD.Matrix = envMatrices.ViewProjectionD;

            MyUtils.Init(ref envMatrices.ViewFrustumClippedD);
            envMatrices.ViewFrustumClippedD.Matrix = envMatrices.ViewD * envMatrices.OriginalProjectionD;
        }
Exemplo n.º 25
0
        static void DrawInstanceComponent(MyInstanceComponent instanceComponent, List <MyHighlightDesc> highlightDescs)
        {
            MyRenderContext RC = MyRender11.RC;

            // common settings (combination of MyHighlightPass.cs and MyRenderingPass.cs):
            MyMapping mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants);
            Matrix    matrix  = MyRender11.Environment.Matrices.ViewProjectionAt0;

            matrix = Matrix.Transpose(matrix);
            mapping.WriteAndPosition(ref matrix);
            mapping.Unmap();

            RC.VertexShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants);
            RC.VertexShader.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);
            RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers);
            RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex);
            //RC.AllShaderStages.SetConstantBuffer(MyCommon.ALPHAMASK_VIEWS_SLOT, MyCommon.AlphamaskViewsConstants); // not used! Maybe impostors?
            RC.SetDepthStencilState(MyDepthStencilStateManager.WriteHighlightStencil, MyHighlight.HIGHLIGHT_STENCIL_MASK);
            RC.SetBlendState(null);
            RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState);
            RC.SetScreenViewport();

            RC.PixelShader.SetConstantBuffer(4, MyCommon.HighlightConstants);


            MyLod lod = instanceComponent.GetHighlightLod();
            MyInstanceLodState stateId = MyInstanceLodState.Solid;
            float stateData            = 0;

            RC.SetIndexBuffer(lod.IB);
            RC.SetVertexBuffer(0, lod.VB0);

            IConstantBuffer objectCB = GetObjectCB(RC, instanceComponent, stateData);

            RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, objectCB);
            RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, objectCB);

            foreach (MyHighlightDesc desc in highlightDescs)
            {
                MyHighlightDesc descRef = desc;
                WriteHighlightConstants(ref descRef);

                if (string.IsNullOrEmpty(desc.SectionName))
                {
                    foreach (var part in lod.Parts)
                    {
                        DrawHighlightedPart(RC, part, stateId);
                    }
                }
                else
                {
                    if (lod.HighlightSections != null && lod.HighlightSections.ContainsKey(desc.SectionName))
                    {
                        foreach (var part in lod.HighlightSections[desc.SectionName].Parts)
                        {
                            DrawHighlightedPart(RC, part, stateId);
                        }
                    }
                }
            }
        }
        internal static void Draw(MyBindableResource renderTarget, MyBindableResource depth)
        {
            RC.SetupScreenViewport();
            RC.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            RC.SetIL(m_inputLayout);

            RC.SetRS(MyRender11.m_nocullRasterizerState);
            RC.SetDS(MyDepthStencilState.DefaultDepthState);

            RC.SetVS(m_vs);
            RC.SetPS(m_ps);

            RC.BindDepthRT(depth, DepthStencilAccess.ReadOnly, renderTarget);

            RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            RC.SetBS(MyRender11.BlendTransparent);

            SortTransparent();
            var transpose = Matrix.Transpose(MyRender11.Environment.ViewProjectionAt0);
            var mapping   = MyMapping.MapDiscard(MyCommon.ProjectionConstants);

            mapping.WriteAndPosition(ref transpose);
            mapping.Unmap();

            CheckBufferSize(m_vertexList.Count);

            RC.SetVB(0, m_VB.Buffer, m_VB.Stride);

            if (m_vertexList.Count > 0)
            {
                mapping = MyMapping.MapDiscard(m_VB.Buffer);
                mapping.WriteAndPosition(m_vertexList.GetInternalArray(), 0, m_vertexList.Count);
                mapping.Unmap();
            }

            RC.DeviceContext.Draw(m_vertexList.Count, 0);

            if (m_debugMeshes.Count > 0)
            {
                var transposeViewProj = Matrix.Transpose(MyRender11.Environment.ViewProjection);
                mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants);
                mapping.WriteAndPosition(ref transposeViewProj);
                mapping.Unmap();
            }

            foreach (var mesh in m_debugMeshes.Values)
            {
                if (mesh.depth)
                {
                    RC.BindDepthRT(depth, DepthStencilAccess.ReadWrite, MyRender11.Backbuffer);
                }
                else
                {
                    RC.BindDepthRT(null, DepthStencilAccess.ReadOnly, null);
                }

                if (mesh.edges)
                {
                    RC.SetRS(MyRender11.m_nocullWireframeRasterizerState);
                }
                else
                {
                    RC.SetRS(MyRender11.m_nocullRasterizerState);
                }

                RC.SetVB(0, mesh.vbuffer.Buffer, mesh.vbuffer.Stride);
                RC.DeviceContext.Draw(mesh.vbuffer.Capacity, 0);
            }

            RC.SetBS(null);

            m_vertexList.Clear();
            m_postSortVertexList.Clear();
            m_triangleSortDistance.Clear();
            m_sortedIndices.Clear();
        }
Exemplo n.º 27
0
        internal static void Draw(IRtvBindable renderTarget)
        {
            RC.SetScreenViewport();
            RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            RC.SetInputLayout(m_inputLayout);

            RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState);
            RC.SetDepthStencilState(MyDepthStencilStateManager.DefaultDepthState);

            RC.VertexShader.Set(m_vs);
            RC.PixelShader.Set(m_ps);

            RC.SetRtv(MyGBuffer.Main.ResolvedDepthStencil, MyDepthStencilAccess.ReadOnly, renderTarget);

            RC.AllShaderStages.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            RC.SetBlendState(MyBlendStateManager.BlendTransparent);

            SortTransparent();
            var transpose = Matrix.Transpose(MyRender11.Environment.Matrices.ViewProjectionAt0);
            var mapping   = MyMapping.MapDiscard(MyCommon.ProjectionConstants);

            mapping.WriteAndPosition(ref transpose);
            mapping.Unmap();

            CheckBufferSize(m_vertexList.Count);

            RC.SetVertexBuffer(0, m_VB);

            if (m_vertexList.Count > 0)
            {
                mapping = MyMapping.MapDiscard(m_VB);
                mapping.WriteAndPosition(m_vertexList.GetInternalArray(), m_vertexList.Count);
                mapping.Unmap();
            }

            RC.Draw(m_vertexList.Count, 0);

            if (m_debugMeshes.Count > 0)
            {
                var transposeViewProj = Matrix.Transpose(MyRender11.Environment.Matrices.ViewProjection);
                mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants);
                mapping.WriteAndPosition(ref transposeViewProj);
                mapping.Unmap();
            }

            foreach (var mesh in m_debugMeshes.Values)
            {
                if (mesh.depth)
                {
                    RC.SetRtv(MyGBuffer.Main.ResolvedDepthStencil, MyDepthStencilAccess.ReadWrite, renderTarget);
                }
                else
                {
                    RC.SetRtv(renderTarget);
                }

                if (mesh.edges)
                {
                    RC.SetRasterizerState(MyRasterizerStateManager.NocullWireframeRasterizerState);
                }
                else
                {
                    RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState);
                }

                RC.SetVertexBuffer(0, mesh.vbuffer);
                RC.Draw(mesh.vbuffer.ElementCount, 0);
            }

            RC.SetBlendState(null);

            m_vertexList.Clear();
            m_postSortVertexList.Clear();
            m_triangleSortDistance.Clear();
            m_sortedIndices.Clear();
        }
Exemplo n.º 28
0
        internal static unsafe void Draw(IRtvBindable renderTarget, IDepthStencil depth)
        {
            RC.SetScreenViewport();
            RC.SetPrimitiveTopology(PrimitiveTopology.LineList);
            RC.SetInputLayout(m_inputLayout);

            RC.SetRasterizerState(MyRasterizerStateManager.LinesRasterizerState);

            RC.VertexShader.Set(m_vs);
            RC.PixelShader.Set(m_ps);
            RC.SetBlendState(MyBlendStateManager.BlendAlphaPremult);

            RC.SetDepthStencilState(MyDepthStencilStateManager.DefaultDepthState);

            CheckBufferSize(m_vertices.Count);
            RC.SetVertexBuffer(0, m_VB.Buffer, m_VB.Stride);

            RC.SetRtv(depth, MyDepthStencilAccess.ReadOnly, renderTarget);

            RC.AllShaderStages.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants);

            if (m_batches.Count > 0)
            {
                var mapping = MyMapping.MapDiscard(m_VB.Buffer);
                mapping.WriteAndPosition(m_vertices.GetInternalArray(), 0, m_vertices.Count);
                mapping.Unmap();

                Matrix prevMatrix = Matrix.Zero;
                foreach (var batch in m_batches)
                {
                    Matrix matrix;
                    if (batch.CustomViewProjection.HasValue)
                    {
                        matrix = batch.CustomViewProjection.Value;
                    }
                    else
                    {
                        matrix = MyRender11.Environment.Matrices.ViewProjectionAt0;
                    }

                    if (prevMatrix != matrix)
                    {
                        prevMatrix = matrix;
                        var transpose = Matrix.Transpose(matrix);

                        mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants);
                        mapping.WriteAndPosition(ref transpose);
                        mapping.Unmap();
                    }

                    if (batch.IgnoreDepth)
                    {
                        RC.SetDepthStencilState(MyDepthStencilStateManager.IgnoreDepthStencil);
                    }
                    else
                    {
                        RC.SetDepthStencilState(MyDepthStencilStateManager.DefaultDepthState);
                    }

                    RC.Draw(batch.VertexCount, batch.StartVertex);
                }
            }

            RC.SetDepthStencilState(null);
            RC.SetRasterizerState(null);

            m_vertices.Clear();

            foreach (var batch in m_batches)
            {
                m_batchesPool.Deallocate(batch);
            }
            m_batches.Clear();
        }
Exemplo n.º 29
0
        private static void SetupCameraMatricesInternal(MyRenderMessageSetCameraViewMatrix message, MyEnvironmentMatrices envMatrices, MyStereoRegion typeofEnv)
        {//uses m_leftEye to handle HMD images
            var viewMatrix     = message.ViewMatrix;
            var cameraPosition = message.CameraPosition;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null && message.LastMomentUpdateIndex != 0)
                {
                    MatrixD origin = MatrixD.Identity;
                    MyOpenVR.LMUMatrixGetOrigin(ref origin, message.LastMomentUpdateIndex);
                    viewMatrix = MatrixD.Invert(origin);
                }
            }

            var viewMatrixAt0 = viewMatrix;

            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null)
                {
                    if (message.LastMomentUpdateIndex != 0)
                    {
                        var tViewMatrix = Matrix.Transpose(viewMatrix);
                        var viewHMDat0  = MyOpenVR.ViewHMD;
                        viewHMDat0.M14 = 0;
                        viewHMDat0.M24 = 0;
                        viewHMDat0.M34 = 0;
                        viewHMDat0.M41 = 0;
                        viewHMDat0.M42 = 0;
                        viewHMDat0.M43 = 0;
                        viewHMDat0.M44 = 1;

                        //cameraPosition += tViewMatrix.Up * MyOpenVR.ViewHMD.Translation.Y;
                        //cameraPosition += tViewMatrix.Backward * MyOpenVR.ViewHMD.Translation.X;
                        //cameraPosition += tViewMatrix.Right * MyOpenVR.ViewHMD.Translation.Z;

                        viewMatrixAt0 = viewMatrixAt0 * viewHMDat0;
                        viewMatrix    = viewMatrix * viewHMDat0;

                        if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                        }
                        else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                        }
                    }
                }
                else
                {
                    if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                    }
                    else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                    }
                }
            }

            var originalProjection = message.ProjectionMatrix;
            //var invOriginalProjection = Matrix.CreatePerspectiveFovRhInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane, message.FarPlane);

            float aspectRatio = MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y;

            if (typeofEnv != MyStereoRegion.FULLSCREEN)
            {
                aspectRatio /= 2;
            }
            var renderProjection = Matrix.CreatePerspectiveFieldOfView(message.FOV, aspectRatio, message.FarPlane, message.NearPlane);
            var invProj          = Matrix.CreatePerspectiveFovRhInverse(message.FOV, aspectRatio, message.FarPlane, message.NearPlane);

            renderProjection = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, aspectRatio, message.NearPlane);
            invProj          = Matrix.CreatePerspectiveFovRhInfiniteComplementaryInverse(message.FOV, aspectRatio, message.NearPlane);

            var invView = Matrix.Transpose(viewMatrixAt0);

            invView.M41 = (float)cameraPosition.X;
            invView.M42 = (float)cameraPosition.Y;
            invView.M43 = (float)cameraPosition.Z;

            envMatrices.ViewAt0              = viewMatrixAt0;
            envMatrices.InvViewAt0           = Matrix.Transpose(viewMatrixAt0);
            envMatrices.ViewProjectionAt0    = viewMatrixAt0 * renderProjection;
            envMatrices.InvViewProjectionAt0 = invProj * Matrix.Transpose(viewMatrixAt0);
            cameraPosition.AssertIsValid();
            envMatrices.CameraPosition      = cameraPosition;
            envMatrices.View                = viewMatrix;
            envMatrices.ViewD               = viewMatrix;
            envMatrices.OriginalProjectionD = originalProjection;
            envMatrices.InvView             = invView;
            envMatrices.ViewProjection      = viewMatrix * renderProjection;
            envMatrices.InvViewProjection   = invProj * invView;
            envMatrices.Projection          = renderProjection;
            envMatrices.InvProjection       = invProj;

            envMatrices.ViewProjectionD = envMatrices.ViewD * (MatrixD)renderProjection;

            envMatrices.NearClipping             = message.NearPlane;
            envMatrices.FarClipping              = message.FarPlane;
            envMatrices.LargeDistanceFarClipping = message.FarPlane * 500.0f;
            envMatrices.FovY = message.FOV;

            MyUtils.Init(ref envMatrices.ViewFrustumD);
            envMatrices.ViewFrustumD.Matrix = envMatrices.ViewProjectionD;

            MyUtils.Init(ref envMatrices.ViewFrustumClippedD);
            envMatrices.ViewFrustumClippedD.Matrix = envMatrices.ViewD * envMatrices.OriginalProjectionD;
        }