コード例 #1
0
        public unsafe void UpdateGpuBuffer(CRenderContext rc, EngineNS.CCommandList cmd, Graphics.CGfxCamera Camera)
        {
            if (UseVTF)
            {
                UpdateGpuBufferVTF(rc, cmd, Camera);
            }
            else
            {
                var bfDesc = new CGpuBufferDesc();
                //mAllVertexSRV
                {
                    bfDesc.SetMode(false, false);
                    bfDesc.ByteWidth           = (uint)(mAllVertices.Count * sizeof(EngineNS.Bricks.GpuDriven.Cluster.GpuSceneVertex));
                    bfDesc.StructureByteStride = (uint)sizeof(EngineNS.Bricks.GpuDriven.Cluster.GpuSceneVertex);
                    var copyArray = mAllVertices.ToArray();
                    fixed(EngineNS.Bricks.GpuDriven.Cluster.GpuSceneVertex *p = &copyArray[0])
                    {
                        bufferAllVertex = rc.CreateGpuBuffer(bfDesc, (IntPtr)p);
                    }

                    var srvDesc = new ISRVDesc();
                    srvDesc.ToDefault();
                    srvDesc.ViewDimension        = EResourceDimension.RESOURCE_DIMENSION_BUFFER;
                    srvDesc.Buffer.ElementOffset = 0;
                    srvDesc.Buffer.NumElements   = (uint)mAllVertices.Count;
                    mAllVertexSRV = rc.CreateShaderResourceViewFromBuffer(bufferAllVertex, srvDesc);
                }
                //uavMeshInstanceArray
                {
                    bfDesc.SetMode(false, true);
                    bfDesc.ByteWidth           = (uint)(GpuInstanceDatas.Count * sizeof(EngineNS.Bricks.GpuDriven.GpuScene.GpuMeshInstanceData));
                    bfDesc.StructureByteStride = (uint)sizeof(EngineNS.Bricks.GpuDriven.GpuScene.GpuMeshInstanceData);
                    var copyArray = GpuInstanceDatas.ToArray();
                    fixed(EngineNS.Bricks.GpuDriven.GpuScene.GpuMeshInstanceData *p = &copyArray[0])
                    {
                        bufferMeshInstanceArray = rc.CreateGpuBuffer(bfDesc, (IntPtr)p);
                        //bufferMeshInstanceArray.UpdateBufferData(cmd, (IntPtr)p, bfDesc.ByteWidth);
                    }

                    var uavDesc = new CUnorderedAccessViewDesc();
                    uavDesc.ToDefault();
                    uavDesc.Buffer.NumElements = (uint)GpuInstanceDatas.Count;
                    uavMeshInstanceArray       = rc.CreateUnorderedAccessView(bufferMeshInstanceArray, uavDesc);

                    var srvDesc = new ISRVDesc();
                    srvDesc.ToDefault();
                    srvDesc.ViewDimension        = EResourceDimension.RESOURCE_DIMENSION_BUFFER;
                    srvDesc.Buffer.ElementOffset = 0;
                    srvDesc.Buffer.NumElements   = (uint)GpuInstanceDatas.Count;
                    mMeshInstanceSRV             = rc.CreateShaderResourceViewFromBuffer(bufferMeshInstanceArray, srvDesc);
                }
            }
            if (UseComputeShader)
            {
                var bfDesc = new CGpuBufferDesc();
                if (uavMeshInstanceArray == null)
                {
                    bfDesc.SetMode(false, true);
                    bfDesc.ByteWidth           = (uint)(GpuInstanceDatas.Count * sizeof(EngineNS.Bricks.GpuDriven.GpuScene.GpuMeshInstanceData));
                    bfDesc.StructureByteStride = (uint)sizeof(EngineNS.Bricks.GpuDriven.GpuScene.GpuMeshInstanceData);
                    var copyArray = GpuInstanceDatas.ToArray();
                    fixed(EngineNS.Bricks.GpuDriven.GpuScene.GpuMeshInstanceData *p = &copyArray[0])
                    {
                        bufferMeshInstanceArray = rc.CreateGpuBuffer(bfDesc, (IntPtr)p);
                        //bufferMeshInstanceArray.UpdateBufferData(cmd, (IntPtr)p, bfDesc.ByteWidth);
                    }

                    var uavDesc = new CUnorderedAccessViewDesc();
                    uavDesc.ToDefault();
                    uavDesc.Buffer.NumElements = (uint)GpuInstanceDatas.Count;
                    uavMeshInstanceArray       = rc.CreateUnorderedAccessView(bufferMeshInstanceArray, uavDesc);

                    var srvDesc = new ISRVDesc();
                    srvDesc.ToDefault();
                    srvDesc.ViewDimension        = EResourceDimension.RESOURCE_DIMENSION_BUFFER;
                    srvDesc.Buffer.ElementOffset = 0;
                    srvDesc.Buffer.NumElements   = (uint)GpuInstanceDatas.Count;
                    mMeshInstanceSRV             = rc.CreateShaderResourceViewFromBuffer(bufferMeshInstanceArray, srvDesc);
                }

                //uavClusterArray
                {
                    bfDesc.SetMode(false, true);
                    bfDesc.ByteWidth           = (uint)(GpuClusters.Count * sizeof(EngineNS.Bricks.GpuDriven.Cluster.GpuCluster));
                    bfDesc.StructureByteStride = (uint)sizeof(EngineNS.Bricks.GpuDriven.Cluster.GpuCluster);
                    var copyArray = GpuClusters.ToArray();
                    fixed(EngineNS.Bricks.GpuDriven.Cluster.GpuCluster *p = &copyArray[0])
                    {
                        bufferClusterArray = rc.CreateGpuBuffer(bfDesc, (IntPtr)p);
                        //bufferClusterArray.UpdateBufferData(cmd, (IntPtr)p, bfDesc.ByteWidth);
                    }

                    var uavDesc = new CUnorderedAccessViewDesc();
                    uavDesc.ToDefault();
                    uavDesc.Buffer.NumElements = (uint)GpuClusters.Count;
                    uavClusterArray            = rc.CreateUnorderedAccessView(bufferClusterArray, uavDesc);
                }

                //uavStaticSceneAllFaces
                {
                    bfDesc.SetMode(false, true);
                    bfDesc.ByteWidth           = (uint)(AllIndices.Count * sizeof(uint));
                    bfDesc.StructureByteStride = (uint)sizeof(uint);
                    var copyArray = AllIndices.ToArray();
                    fixed(uint *p = &copyArray[0])
                    {
                        bufferStaticSceneAllFaces = rc.CreateGpuBuffer(bfDesc, (IntPtr)p);
                        //bufferStaticSceneAllFaces.UpdateBufferData(cmd, (IntPtr)p, bfDesc.ByteWidth);
                    }

                    var uavDesc = new CUnorderedAccessViewDesc();
                    uavDesc.ToDefault();
                    uavDesc.Buffer.NumElements = (uint)(AllIndices.Count);
                    uavStaticSceneAllFaces     = rc.CreateUnorderedAccessView(bufferStaticSceneAllFaces, uavDesc);
                }

                //uavStaticSceneDrawFaces
                {
                    bfDesc.SetMode(false, true);

                    int MaxInstanceNumber = 20;
                    bfDesc.ByteWidth           = (uint)(AllIndices.Count * MaxInstanceNumber * sizeof(uint));
                    bfDesc.StructureByteStride = (uint)sizeof(uint);
                    bfDesc.MiscFlags           = (UInt32)EResourceMiscFlag.BUFFER_ALLOW_RAW_VIEWS;
                    bfDesc.BindFlags          |= (UInt32)EBindFlag.INDEX_BUFFER;
                    bfDesc.CPUAccessFlags      = 0;
                    bufferStaticSceneDrawFaces = rc.CreateGpuBuffer(bfDesc, IntPtr.Zero);

                    var uavDesc = new CUnorderedAccessViewDesc();
                    uavDesc.ToDefault();
                    uavDesc.Format             = EPixelFormat.PXF_R32_TYPELESS;
                    uavDesc.Buffer.NumElements = (uint)(AllIndices.Count * MaxInstanceNumber);
                    uavDesc.Buffer.Flags       = (UInt32)EUAVBufferFlag.UAV_FLAG_RAW;
                    uavStaticSceneDrawFaces    = rc.CreateUnorderedAccessView(bufferStaticSceneDrawFaces, uavDesc);

                    var ibDesc = new CIndexBufferDesc();
                    ibDesc.CPUAccess = 0;
                    ibDesc.InitData  = IntPtr.Zero;
                    ibDesc.ByteWidth = bfDesc.ByteWidth;
                    ibDesc.Type      = EIndexBufferType.IBT_Int32;

                    mDrawIndexBuffer = rc.CreateIndexBufferFromBuffer(ibDesc, bufferStaticSceneDrawFaces);
                }

                //uavIndirectDrawArgs
                {
                    bfDesc.SetMode(false, true);

                    bfDesc.ByteWidth           = 20; //(uint)(1 * sizeof(EngineNS.Bricks.GpuDriven.GpuScene.GpuDrawArgs));
                    bfDesc.StructureByteStride = 4;  //(uint)sizeof(EngineNS.Bricks.GpuDriven.GpuScene.GpuDrawArgs);
                    bfDesc.MiscFlags           = (UInt32)(EResourceMiscFlag.DRAWINDIRECT_ARGS | EResourceMiscFlag.BUFFER_ALLOW_RAW_VIEWS);
                    bfDesc.CPUAccessFlags      = 0;
                    bufferIndirectDrawArgs     = rc.CreateGpuBuffer(bfDesc, IntPtr.Zero);
                    var uavDesc = new CUnorderedAccessViewDesc();
                    uavDesc.ToDefault();
                    uavDesc.Format = EPixelFormat.PXF_R32_TYPELESS;

                    uavDesc.Buffer.NumElements = (uint)(5);
                    uavDesc.Buffer.Flags       = (UInt32)EUAVBufferFlag.UAV_FLAG_RAW;
                    uavIndirectDrawArgs        = rc.CreateUnorderedAccessView(bufferIndirectDrawArgs, uavDesc);

                    var drawAgrs = new EngineNS.Bricks.GpuDriven.GpuScene.GpuDrawArgs();
                    drawAgrs.InstanceCount         = 1;
                    drawAgrs.StartInstanceLocation = 0;
                    drawAgrs.IndexCountPerInstance = 0;
                    bufferIndirectDrawArgs.UpdateBufferData(cmd, (IntPtr)(&drawAgrs), bfDesc.ByteWidth);
                }

                ComputeDispatch(rc, cmd, Camera);
            }
            else
            {
                CIndexBufferDesc ibDesc = new CIndexBufferDesc(EIndexBufferType.IBT_Int32);
                ibDesc.CPUAccess    = (UInt32)ECpuAccess.CAS_WRITE;
                ibDesc.ByteWidth    = (uint)(mAllIndices.Count * sizeof(UInt32) * 20);
                mCpuDrawIndexBuffer = rc.CreateIndexBuffer(ibDesc);
            }


            //CEngine.Instance.EventPoster.RunOn(() =>
            //{
            //    var blobDrawArgs = new EngineNS.Support.CBlobObject();
            //    bufferIndirectDrawArgs.GetBufferData(rc, blobDrawArgs);
            //    EngineNS.Bricks.GpuDriven.GpuScene.GpuDrawArgs* pArg = (EngineNS.Bricks.GpuDriven.GpuScene.GpuDrawArgs*)blobDrawArgs.Data.ToPointer();
            //    if (pArg != null)
            //    {
            //        mDrawArgs = *pArg;
            //    }
            //    return null;
            //}, Thread.Async.EAsyncTarget.Main);
        }