Exemple #1
0
        private void PopulateCommandListsForVisualDebugDrawing()
        {
            m_CommandList.CommandAllocator.Reset();

            m_CommandList.CommandList.Reset(m_CommandList.CommandAllocator, m_PipelineState);

            // set viewport and scissors
            m_CommandList.CommandList.SetGraphicsRootSignature(m_RootSignature);

            // @TODO - redesign this section
            m_TransformationCBPointer = m_ConstantBuffer.Map(0);
            //Utilities.Write(m_TransformationCBPointer, ref m_TransformationCB);
            List <Matrix> dataToCopy = new List <Matrix>();

            dataToCopy.Add(m_TransformationCB.viewProjectionMatrix);
            foreach (Matrix mtx in m_TransformationCB.BoneMatrices)
            {
                dataToCopy.Add(mtx);
            }
            Utilities.Write(m_TransformationCBPointer, dataToCopy.ToArray(), 0, dataToCopy.Count);
            m_ConstantBuffer.Unmap(0);

            m_CommandList.CommandList.SetDescriptorHeaps(1, new DescriptorHeap[] { m_ConstantBufferViewHeap });
            //m_CommandList.SetDescriptorHeaps(2, new DescriptorHeap[] { m_ConstantBufferViewHeap, m_srvDescriptorHeap });

            GpuDescriptorHandle hDescriptor = m_ConstantBufferViewHeap.GPUDescriptorHandleForHeapStart;

            m_CommandList.CommandList.SetGraphicsRootDescriptorTable(0, hDescriptor);

            hDescriptor += m_ConstantBufferDescriptorSize;
            //Int32 sizeInBytes = (Utilities.SizeOf<TransformationCB>() + 255) & ~255;
            //hDescriptor += sizeInBytes;

            m_CommandList.CommandList.SetGraphicsRootDescriptorTable(1, hDescriptor);

            m_CommandList.CommandList.SetViewport(m_Viewport);
            m_CommandList.CommandList.SetScissorRectangles(m_SissorRect);

            // use barrier to notify we are using the m_RenderTarget
            Resource rtvBackBuffer = m_SwapChainDX12.GetBackBuffer(m_FrameIndex);

            m_CommandList.CommandList.ResourceBarrierTransition(rtvBackBuffer, ResourceStates.Present, ResourceStates.RenderTarget);

            //CpuDescriptorHandle rtvHandle = m_RenderTargetViewHeap.CPUDescriptorHandleForHeapStart;
            //rtvHandle += m_FrameIndex * m_RTVDescriptorSize;
            //CpuDescriptorHandle dsvHandle = m_DepthStencilViewHeap.CPUDescriptorHandleForHeapStart;

            CpuDescriptorHandle rtvHandle = m_DeviceContext.Dx12Device.RenderTargetDescriptorCache.GetCpuAddressByOffset(m_FrameIndex);
            // @TODO - need to set the depth value consistent with frame counts!
            CpuDescriptorHandle dsvHandle = m_DeviceContext.Dx12Device.DepthStencilDescriptorCache.GetCpuAddressByOffset(0);

            m_CommandList.CommandList.SetRenderTargets(rtvHandle, dsvHandle);

            // clear the render target & depth stencil
            m_CommandList.CommandList.ClearRenderTargetView(rtvHandle, new Color4(0, 0.2f, 0.4f, 1), 0, null);
            m_CommandList.CommandList.ClearDepthStencilView(dsvHandle, ClearFlags.FlagsDepth, 1.0f, 0, 0, null);

            // record commands
            //m_CommandList.CommandList.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            //m_CommandList.SetVertexBuffer(0, m_VertexBufferView);
            // @TODO - I need to wrap command list separately!
            //H1StaticMeshLODResource resource = m_TempStaticMesh.StaticMeshData.GetLODResource(0);
            //H1StaticMeshLODResource resource = H1Global<H1World>.Instance.PersistentLevel.GetActor(0).GetActorComponent<H1StaticMeshComponent>().StaticMesh.StaticMeshData.GetLODResource(1);
            //resource.LocalVertexFactory.setVertexBuffers(m_CommandList.CommandList);

            //m_CommandList.DrawInstanced(3, 1, 0, 0);
            //m_CommandList.SetIndexBuffer(m_IndexBufferView);
            //m_CommandList.CommandList.SetIndexBuffer(resource.IndexBuffer.View);

            //int instanceCount = Convert.ToInt32(resource.IndexBuffer.Count);
            //m_CommandList.CommandList.DrawIndexedInstanced(instanceCount, 1, 0, 0, 0);

            //H1RenderUtils.DrawBox(m_CommandList.CommandList, new H1Transform());
            //H1RenderUtils.DrawPlane10x10(m_CommandList.CommandList, new Vector2(0, 0), new Vector2(1, 1));
            //H1RenderUtils.DrawSphere(m_CommandList.CommandList, new Vector3(0, 0, 0), new Vector3(1), 8, 6);
            //AngleSingle angle1 = new AngleSingle(10.0f, AngleType.Degree);
            //H1RenderUtils.DrawCone(m_CommandList.CommandList, angle1.Radians, angle1.Radians, 24, false, new Vector4(1));
            //H1RenderUtils.DrawCylinder(m_CommandList.CommandList, new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), 1, 3, 10);
            //H1RenderUtils.DrawCapsule(m_CommandList.CommandList, new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), 1, 3, 10);
            H1RenderUtils.DrawDisc(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), 1, 10);
            //H1RenderUtils.DrawFlatArrow(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), 10, 1, 0.1f);
            //H1RenderUtils.DrawWireBox(m_CommandList.CommandList, Matrix.Identity, new Vector3(-1, -1, -1), new Vector3(1), 1, 0, 0);
            //H1RenderUtils.DrawWireSphere(m_CommandList.CommandList, new Vector3(0), 2.0f, 24);
            //H1RenderUtils.DrawWireCylinder(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), 1.0f, 3, 10);
            //H1RenderUtils.DrawWireCapsule(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), 1.0f, 3, 10);
            //H1RenderUtils.DrawWireCone(m_CommandList.CommandList, Matrix.Identity, 1.0f, 15.0f, 10);
            //H1RenderUtils.DrawWireSphereCappedCone(m_CommandList.CommandList, Matrix.Identity, 1.0f, 15.0f, 16, 4, 10);
            //H1RenderUtils.DrawOrientedWireBox(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), new Vector3(2));
            //H1RenderUtils.DrawCoordinateSystem(m_CommandList.CommandList, new Vector3(0), Matrix.Identity, 2.0f);
            //H1RenderUtils.DrawDirectionalArrow(m_CommandList.CommandList, Matrix.Identity, 2.0f, 0.2f);
            //H1RenderUtils.DrawWireStar(m_CommandList.CommandList, new Vector3(0), 1.0f);
            //H1RenderUtils.DrawDashedLine(m_CommandList.CommandList, new Vector3(-1), new Vector3(1), 0.2f);
            //H1RenderUtils.DrawWireDiamond(m_CommandList.CommandList, Matrix.Identity, 0.2f);

            //H1SkeletalMeshComponent skeletalMeshComponent = H1Global<H1World>.Instance.PersistentLevel.GetActor(0).GetActorComponent<H1SkeletalMeshComponent>();
            //skeletalMeshComponent.SkeletalMesh.SkeletonDebugDrawing(m_CommandList.CommandList);

            // execute commands
            H1Global <Commands.H1CommandManager> .Instance.ExecuteCommands();

            // use barrier to notify that we are going to present the render target
            m_CommandList.CommandList.ResourceBarrierTransition(rtvBackBuffer, ResourceStates.RenderTarget, ResourceStates.Present);

            // execute the command
            m_CommandList.CommandList.Close();
        }
Exemple #2
0
        // @TODO - change this delivering directly command list from the renderer after implementing overall rendering system
        public void SkeletonDebugDrawing(SharpDX.Direct3D12.GraphicsCommandList commandList, Boolean bDebugDraw = true)
        {
            // @TODO - temporary to check if animation is working
            H1SkeletalMeshComponent skeletalMeshComponent = H1Global <H1World> .Instance.PersistentLevel.GetActor(0).GetActorComponent <H1SkeletalMeshComponent>();

            List <H1AnimInstance.InterpolatedLocalTransform> interpolatedLocalTransforms = new List <H1AnimInstance.InterpolatedLocalTransform>();

            for (Int32 boneIndex = 0; boneIndex < m_Skeleton.RefSkeleton.RefBoneInfoList.Count; ++boneIndex)
            {
                H1AnimInstance.InterpolatedLocalTransform interpolatedLocalTransform = new H1AnimInstance.InterpolatedLocalTransform();
                H1MeshBoneInfo meshBoneInfo = m_Skeleton.RefSkeleton.RefBoneInfoList[boneIndex];

                interpolatedLocalTransform.BoneName       = meshBoneInfo.Name;
                interpolatedLocalTransform.LocalTransform = m_Skeleton.RefSkeleton.RefBoneBases[boneIndex];

                interpolatedLocalTransforms.Add(interpolatedLocalTransform);
            }

            // extract interpolated local transforms
            skeletalMeshComponent.AnimScriptInstance.ExtractInterpolatedLocalTransform(0.0f, ref interpolatedLocalTransforms);

            H1Transform[] globalTransforms = new H1Transform[m_Skeleton.RefSkeleton.RefBoneBases.Count];
            // tracking only upper parent index by one
            Int32[] transformParentIndices = new Int32[m_Skeleton.RefSkeleton.RefBoneBases.Count];

            for (Int32 boneIndex = 0; boneIndex < m_Skeleton.RefSkeleton.RefBoneInfoList.Count; ++boneIndex)
            {
                H1MeshBoneInfo meshBoneInfo = m_Skeleton.RefSkeleton.RefBoneInfoList[boneIndex];

                //H1Transform localTransform = m_Skeleton.RefSkeleton.RefBoneBases[boneIndex];
                H1Transform localTransform = interpolatedLocalTransforms[boneIndex].LocalTransform;

                // extract parent locals
                List <H1Transform> parentLocalTransforms = new List <H1Transform>();
                H1MeshBoneInfo     currMeshBoneInfo      = meshBoneInfo;
                while (currMeshBoneInfo.ParentIndex != -1)
                {
                    //parentLocalTransforms.Add(m_Skeleton.RefSkeleton.RefBoneBases[currMeshBoneInfo.ParentIndex]);
                    parentLocalTransforms.Add(interpolatedLocalTransforms[currMeshBoneInfo.ParentIndex].LocalTransform);
                    currMeshBoneInfo = m_Skeleton.RefSkeleton.RefBoneInfoList[currMeshBoneInfo.ParentIndex];
                }

                // generate global transform for current bone space
                // 1. start to transform current local transformation matrix
                Matrix globalTransformMtx = localTransform.Transformation;
                // 2. from curr bone space to root space by going up to each parent node space
                for (Int32 index = 0; index < parentLocalTransforms.Count; ++index)
                {
                    Matrix parentLocalTransformMtx = parentLocalTransforms[index].Transformation;
                    globalTransformMtx = Matrix.Multiply(globalTransformMtx, parentLocalTransformMtx);
                }

                // decompose global matrix and generate H1Transform
                Vector3    translation;
                Vector3    scale;
                Quaternion rotate;
                globalTransformMtx.Decompose(out scale, out rotate, out translation);

                H1Transform globalTransform = new H1Transform();
                globalTransform.Translation = translation;
                globalTransform.Scaling     = scale;
                globalTransform.Rotation    = rotate;

                globalTransforms[boneIndex]       = globalTransform;
                transformParentIndices[boneIndex] = meshBoneInfo.ParentIndex;
            }

            // set bone matrices
            Int32 currGlobalBoneIndex = 0;

            foreach (H1Transform boneTransform in globalTransforms)
            {
                // @TODO - temporary scaling to match when we importing vertices in H1AssimpImporter
                //boneTransform.Scaling = boneTransform.Scaling * 0.01f;

                // get global offset matrices
                Matrix globalOffsetMatrix = m_Skeleton.RefSkeleton.RefOffsetBases[currGlobalBoneIndex].Transformation;

                // pass multiplied global offset matrices and animated global matrices
                H1Global <H1ManagedRenderer> .Instance.SetBoneMatrix(currGlobalBoneIndex, Matrix.Multiply(globalOffsetMatrix, boneTransform.Transformation));

                currGlobalBoneIndex++;
            }

            // when debug draw is enabled
            if (bDebugDraw == true)
            {
                for (Int32 boneIndex = 0; boneIndex < globalTransforms.Count(); ++boneIndex)
                {
                    Int32 parentIndex = transformParentIndices[boneIndex];
                    if (parentIndex < 0)
                    {
                        continue;
                    }

                    H1Transform parentBoneTransform = globalTransforms[parentIndex];
                    H1Transform boneTransform       = globalTransforms[boneIndex];

                    Vector3 parentLoc = parentBoneTransform.Translation * 0.005f;
                    Vector3 currLoc   = boneTransform.Translation * 0.005f;

                    float length = (currLoc - parentLoc).Length();
                    if (length < 0.001f)
                    {
                        continue;
                    }

                    Vector3 zAxis = Vector3.Normalize(currLoc - parentLoc);

                    Matrix  pitchMtx     = Matrix.RotationYawPitchRoll(0, 0.1f, 0);
                    Vector4 rotatedZAxis = Vector3.Transform(zAxis, pitchMtx);
                    rotatedZAxis /= rotatedZAxis.W;

                    Vector3 yAxis = Vector3.Normalize(Vector3.Cross(zAxis, Vector3.Normalize(new Vector3(rotatedZAxis.X, rotatedZAxis.Y, rotatedZAxis.Z))));
                    Vector3 xAxis = Vector3.Normalize(Vector3.Cross(zAxis, yAxis));

                    //H1RenderUtils.DrawCapsule(commandList, parentLoc, xAxis, yAxis, zAxis, 0.03f, length / 2.0f, 16);
                    H1RenderUtils.DrawDashedLine(commandList, parentLoc, currLoc, 2.0f);
                    //H1RenderUtils.DrawWireStar(commandList, currLoc, 0.03f);
                }
            }
        }