Example #1
0
        private void PopulateCommandListsForSkeletalMesh()
        {
            m_CommandList.CommandAllocator.Reset();
            //m_DeviceContext.MainCommandListPool

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

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

            // @TODO - redesign this section
            // extract and update bone matrix
            H1SkeletalMeshComponent skeletalMeshComponent = H1Global <H1World> .Instance.PersistentLevel.GetActor(0).GetActorComponent <H1SkeletalMeshComponent>();

            skeletalMeshComponent.SkeletalMesh.SkeletonDebugDrawing(m_CommandList.CommandList, false);

            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, 101);
            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;

            H1SkeletalMeshObjectGPUSkin skeletalMeshObject = H1Global <H1World> .Instance.PersistentLevel.GetActor(0).GetActorComponent <H1SkeletalMeshComponent>().SkeletalMeshObjectGPUSkin;

            H1SkeletalMeshObjectGPUSkin.H1SkeletalMeshObjectLOD skeletalMeshObjectLOD_0 = skeletalMeshObject.GetSkeletalMeshObjectLODByIndex(0);

            SharpDX.Direct3D12.IndexBufferView rawIBV = ((Gen2Layer.H1IndexBufferView)skeletalMeshObjectLOD_0.IndexBuffer.View).View;
            m_CommandList.CommandList.SetIndexBuffer(rawIBV);

            H1SkeletalMesh skeletalMesh = H1Global <H1World> .Instance.PersistentLevel.GetActor(0).GetActorComponent <H1SkeletalMeshComponent>().SkeletalMesh;

            List <H1SkelMeshSection> skelSections = skeletalMesh.SkeletalMeshResource.GetLODModel(0).Sections;

            Int32 sectionIndex          = 0;
            Int32 currIndexBufferOffset = 0;
            Int32 totalCount            = skeletalMeshObjectLOD_0.GPUSkinVertexFactories.VertexFactories.Count;

            for (Int32 vertexfactoryIndex = 0; vertexfactoryIndex < totalCount; ++vertexfactoryIndex)
            {
                var vertexfactory = skeletalMeshObjectLOD_0.GPUSkinVertexFactories.VertexFactories[vertexfactoryIndex];
                vertexfactory.setVertexBuffers(m_CommandList.CommandList);

                H1SkelMeshSection currSkelMeshSection = skelSections[sectionIndex];
                Int32             indexCount          = Convert.ToInt32(currSkelMeshSection.NumTriangles) * 3;

                m_CommandList.CommandList.DrawIndexedInstanced(indexCount, 1, currIndexBufferOffset, 0, 0);

                currIndexBufferOffset += indexCount;
                sectionIndex++;
            }

            // 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();
        }
        public Boolean GenerateSkeleltalMeshObjectGpuSkin()
        {
            // allocate new instance of SkeletalMeshGpuSkin
            m_SkeletalMeshObject = new H1SkeletalMeshObjectGPUSkin(m_SkeletalMesh.SkeletalMeshResource);
            H1SkeletalMeshObjectGPUSkin refSkeletalMeshObject = m_SkeletalMeshObject as H1SkeletalMeshObjectGPUSkin;

            // process soft vertices for each chunk to separate vertex buffers
            // @TODO - handle LOD variation
            List <H1SkelMeshChunk> skelChunks = SkeletalMesh.SkeletalMeshResource.GetLODModel(0).Chunks;

            // @TODO - temporary add LOD_0
            H1SkeletalMeshObjectGPUSkin.H1SkeletalMeshObjectLOD skeletalMeshObjectLOD_0 = refSkeletalMeshObject.AddSkeletalMeshObjectLOD();

            Int32 skelChunkIndex = 0;

            foreach (H1SkelMeshChunk skelChunk in skelChunks)
            {
                H1SkeletalMeshVertexBuffers skeletalMeshVBsRef = skeletalMeshObjectLOD_0.SkelMeshResourceRef.GetLODModel(0).VertexBufferGPUSkin.SkeletalMeshVertexBuffers[skelChunkIndex];

                // add GPUSkinFactory
                skeletalMeshObjectLOD_0.GPUSkinVertexFactories.VertexFactories.Add(new H1GpuSkinVertexFactory());
                H1GpuSkinVertexFactory newGpuSkinVertexFactoryRef = skeletalMeshObjectLOD_0.GPUSkinVertexFactories.VertexFactories.Last();

                // offset tracking
                Int32 currOffset = 0;

                Int32 skelMeshVBsCount = skeletalMeshVBsRef.GetSkeletalMeshVertexDataCount();
                for (Int32 skelMeshVBIndex = 0; skelMeshVBIndex < skelMeshVBsCount; ++skelMeshVBIndex)
                {
                    H1SkeletalMeshVertexDataInterface skeletalMeshVertexData = skeletalMeshVBsRef.GetSkeletalMeshVertexData(skelMeshVBIndex);
                    switch (skeletalMeshVertexData.DataType)
                    {
                    case H1SkeletalMeshVertexDataInterface.VertexDataType.Position:
                    {
                        H1SkeletalMeshVertexData <Vector4> positionVertexData = skeletalMeshVertexData as H1SkeletalMeshVertexData <Vector4>;
                        newGpuSkinVertexFactoryRef.ShaderData.PositionBuffer = H1VertexBuffer.ProcessVertexBuffer(positionVertexData.VertexBuffer);
                        newGpuSkinVertexFactoryRef.PositionStreamComponent   = new H1VertexStreamComponent(H1VertexStreamSematicType.Position, H1VertexElementType.Float4, currOffset++);
                        break;
                    }

                    case H1SkeletalMeshVertexDataInterface.VertexDataType.TangentZ:
                    {
                        H1SkeletalMeshVertexData <Vector4> tangentZVertexData = skeletalMeshVertexData as H1SkeletalMeshVertexData <Vector4>;
                        newGpuSkinVertexFactoryRef.ShaderData.TangentZBuffer = H1VertexBuffer.ProcessVertexBuffer(tangentZVertexData.VertexBuffer);
                        newGpuSkinVertexFactoryRef.TangentZStreamComponent   = new H1VertexStreamComponent(H1VertexStreamSematicType.TangentZ, H1VertexElementType.Float4, currOffset++);
                        break;
                    }

                    case H1SkeletalMeshVertexDataInterface.VertexDataType.TangentX:
                    {
                        H1SkeletalMeshVertexData <Vector4> tangentXVertexData = skeletalMeshVertexData as H1SkeletalMeshVertexData <Vector4>;
                        newGpuSkinVertexFactoryRef.ShaderData.TangentXBuffer = H1VertexBuffer.ProcessVertexBuffer(tangentXVertexData.VertexBuffer);
                        newGpuSkinVertexFactoryRef.TangentXStreamComponent   = new H1VertexStreamComponent(H1VertexStreamSematicType.TangentX, H1VertexElementType.Float4, currOffset++);
                        break;
                    }

                    case H1SkeletalMeshVertexDataInterface.VertexDataType.InfluencedBones:
                    {
                        H1SkeletalMeshVertexData <Int4> boneIndicesVertexData = skeletalMeshVertexData as H1SkeletalMeshVertexData <Int4>;
                        newGpuSkinVertexFactoryRef.ShaderData.BoneIndices     = H1VertexBuffer.ProcessVertexBuffer(boneIndicesVertexData.VertexBuffer);
                        newGpuSkinVertexFactoryRef.BoneIndicesStreamComponent = new H1VertexStreamComponent(H1VertexStreamSematicType.BoneIndices, H1VertexElementType.Int4, currOffset++);
                        break;
                    }

                    case H1SkeletalMeshVertexDataInterface.VertexDataType.InfluencedWeights:
                    {
                        H1SkeletalMeshVertexData <Vector4> blendWeightsVertexData = skeletalMeshVertexData as H1SkeletalMeshVertexData <Vector4>;
                        newGpuSkinVertexFactoryRef.ShaderData.BoneWeights     = H1VertexBuffer.ProcessVertexBuffer(blendWeightsVertexData.VertexBuffer);
                        newGpuSkinVertexFactoryRef.BoneWeightsStreamComponent = new H1VertexStreamComponent(H1VertexStreamSematicType.BoneWeights, H1VertexElementType.Float4, currOffset++);
                        break;
                    }

                    // @TODO - support multiple texcoords
                    case H1SkeletalMeshVertexDataInterface.VertexDataType.Texcoord:
                    {
                        H1SkeletalMeshVertexData <Vector2> texcoordVertexData = skeletalMeshVertexData as H1SkeletalMeshVertexData <Vector2>;
                        newGpuSkinVertexFactoryRef.ShaderData.TexcoordBuffers.Add(H1VertexBuffer.ProcessVertexBuffer(texcoordVertexData.VertexBuffer));
                        newGpuSkinVertexFactoryRef.TexcoordStreamComponents.Add(new H1VertexStreamComponent(H1VertexStreamSematicType.Texcoord, H1VertexElementType.Float2, currOffset++));
                        break;
                    }

                    case H1SkeletalMeshVertexDataInterface.VertexDataType.Color:
                    {
                        H1SkeletalMeshVertexData <Vector4> colorVertexData = skeletalMeshVertexData as H1SkeletalMeshVertexData <Vector4>;
                        newGpuSkinVertexFactoryRef.ShaderData.ColorBuffer = H1VertexBuffer.ProcessVertexBuffer(colorVertexData.VertexBuffer);
                        newGpuSkinVertexFactoryRef.ColorStreamComponent   = new H1VertexStreamComponent(H1VertexStreamSematicType.Color, H1VertexElementType.Float4, currOffset++);
                        break;
                    }
                    }
                }

                // generate RHIVertexFormat Declaration
                newGpuSkinVertexFactoryRef.GenerateVertexDeclaration();

                skelChunkIndex++;
            }

            // add index buffer (containing multiple skeletal mesh chunks's indices)
            List <UInt32> indices    = SkeletalMesh.SkeletalMeshResource.GetLODModel(0).MultiSizeIndexContainer.Indices;
            UInt32        bufferSize = Convert.ToUInt32(Utilities.SizeOf <UInt32>() * indices.Count);

            skeletalMeshObjectLOD_0.IndexBuffer = H1Global <H1ManagedRenderer> .Instance.CreateIndexBuffer(bufferSize);

            // write indices data
            skeletalMeshObjectLOD_0.IndexBuffer.WriteData(indices.ToArray());

            return(true);
        }