Exemple #1
0
        public static void CaptureGeoBox(EngineNS.Bricks.HollowMaker.Agent.GeoBox geoBox,
                                         EngineNS.Bricks.HollowMaker.GeomScene.AgentBoxs agentData,
                                         EngineNS.Graphics.CGfxCamera camera,
                                         EngineNS.CRenderContext rc,
                                         UInt32 numElem,
                                         EngineNS.Graphics.RenderPolicy.CGfxRP_SceneCapture rp,
                                         EngineNS.CCommandList cmd,
                                         UInt32 cbIndex,
                                         EngineNS.CConstantBuffer cbuffer,
                                         EngineNS.CGpuBuffer buffer_visible,
                                         EngineNS.CShaderDesc csMain_visible,
                                         EngineNS.CComputeShader cs_visible,
                                         EngineNS.CUnorderedAccessView uav_visible,
                                         EngineNS.CGpuBuffer buffer_setBit,
                                         EngineNS.CShaderDesc csMain_setBit,
                                         EngineNS.CComputeShader cs_setBit,
                                         EngineNS.CUnorderedAccessView uav_setBit,
                                         EngineNS.CComputeShader cs_Clear,
                                         List <EngineNS.Support.BitSet> savedBitsets,
                                         UInt32 textureIdx,
                                         Action <int, EngineNS.Support.CBlobObject, EngineNS.Support.CBlobObject> actionAfterCapturePerDir)
        {
            var cornerPos = geoBox.Box.GetCorners();

            //for(int posIdx = 0; posIdx < cornerPos.Length; posIdx++)
            //{
            //    await CaptureSceneWithPoint(cornerPos[posIdx], world, camera, rc, camDirs, camUps, dataBlobs, picBlobs);
            //}
            cmd.SetComputeShader(cs_visible);
            UInt32[] pUAVInitialCounts = new UInt32[1] {
                1,
            };
            cmd.CSSetUnorderedAccessView(0, uav_visible, pUAVInitialCounts);

            //await CaptureSceneWithPoint(geoBox.Box.GetCenter(), agentData.Mat , camera, rc, camDirs, camUps, numElem/*, dataBlobs, picBlobs*/);
            for (int camIdx = 0; camIdx < 6; camIdx++)
            {
                EngineNS.Support.CBlobObject idBlob  = null;
                EngineNS.Support.CBlobObject picBlob = null;
                EditorCommon.PVSAssist.CaptureWithPoint(geoBox.Box.GetCenter(), agentData.Mat, camera, rc, camDirs[camIdx], camUps[camIdx], numElem, rp, textureIdx, ref idBlob, ref picBlob);

                actionAfterCapturePerDir?.Invoke(camIdx, idBlob, picBlob);
            }

            // gbuffer0
            var blob = new EngineNS.Support.CBlobObject();

            buffer_visible.GetBufferData(rc, blob);
            var idArray = blob.ToUInts();

            // fill uav1, clear uav0
            cmd.SetComputeShader(cs_setBit);
            UInt32[] pUAVInitialCounts1 = new UInt32[1] {
                1,
            };
            cmd.CSSetUnorderedAccessView(0, uav_visible, pUAVInitialCounts);
            cmd.CSSetUnorderedAccessView(1, uav_setBit, pUAVInitialCounts);
            var cbDesc = new EngineNS.CConstantBufferDesc();

            if (csMain_setBit.GetCBufferDesc(cbIndex, ref cbDesc))
            {
                if (cbDesc.Type == EngineNS.ECBufferRhiType.SIT_CBUFFER)
                {
                    cmd.CSSetConstantBuffer(cbDesc.CSBindPoint, cbuffer);
                }
            }
            cmd.CSDispatch(numElem, 1, 1);

            var blob0 = new EngineNS.Support.CBlobObject();

            buffer_visible.GetBufferData(rc, blob0);
            var visArray = blob0.ToUInts();
            // gbuffer1
            var blob1 = new EngineNS.Support.CBlobObject();

            buffer_setBit.GetBufferData(rc, blob1);
            var idArray1 = blob1.ToBytes();

            var bitSet  = new EngineNS.Support.BitSet();
            var bitSet1 = new EngineNS.Support.BitSet();

            bitSet.Init(numElem * 2, idArray1);
            bitSet1.Init(numElem);
            for (UInt32 e = 0; e < numElem; e++)
            {
                var bit1 = bitSet.IsBit(e * 2 + 0);
                var bit2 = bitSet.IsBit(e * 2 + 1);

                bitSet1.SetBit(e, bit1 || bit2);
            }

            savedBitsets.Add(bitSet1);

            cmd.SetComputeShader(cs_Clear);
            cmd.CSSetUnorderedAccessView(0, uav_visible, pUAVInitialCounts);
            cmd.CSSetUnorderedAccessView(1, uav_setBit, pUAVInitialCounts);
            cmd.CSDispatch(numElem / 16 + 1, 1, 1);
        }
Exemple #2
0
        public static void CaptureWithPoint(EngineNS.Vector3 point,
                                            EngineNS.Matrix worldMatrix,
                                            EngineNS.Graphics.CGfxCamera camera,
                                            EngineNS.CRenderContext rc,
                                            EngineNS.Vector3 camDir,
                                            EngineNS.Vector3 camUp,
                                            UInt32 actorsCount,
                                            EngineNS.Graphics.RenderPolicy.CGfxRP_SceneCapture rp,
                                            UInt32 textureIdx,
                                            ref EngineNS.Support.CBlobObject dataBlob,
                                            ref EngineNS.Support.CBlobObject picBlob)
        {
            var lookat = point + camDir;

            lookat = EngineNS.Vector3.TransformCoordinate(lookat, worldMatrix);
            point  = EngineNS.Vector3.TransformCoordinate(point, worldMatrix);
            camUp  = EngineNS.Vector3.TransformNormal(camUp, worldMatrix);
            camera.LookAtLH(point, lookat, camUp);
            camera.BeforeFrame();

            //world.Tick();
            //world.SlowDrawAll(rc, camera);

            var maxId = actorsCount;

            rp.TickLogic(null, rc);
            camera.SwapBuffer(true);
            rp.TickRender(null);
            //camera.ClearAllRenderLayerData();

            // debug
#pragma warning disable 0162
            if (false)
            {
                var texRGB = rp.CaptureSV.FrameBuffer.GetSRV_RenderTarget(1);
                texRGB.Save2File(rc, "e:/testCapture.bmp", EngineNS.EIMAGE_FILE_FORMAT.BMP);
            }
#pragma warning restore 0162

            if (rp.UseCapture)
            {
                var blob = new EngineNS.Support.CBlobObject();
                blob.PushData(rp.TexData0.Data, rp.TexData0.Size);
                dataBlob = blob;
                if (rp.CaptureRGBData)
                {
                    blob = new EngineNS.Support.CBlobObject();
                    blob.PushData(rp.TexData1.Data, rp.TexData1.Size);
                    picBlob = blob;
                }
                //await EngineNS.CEngine.Instance.EventPoster.Post(() =>
                //{
                //    rp.CaptureSV.mFrameBuffer.GetSRV_RenderTarget(0).Save2File($"D:\\{camIdx}.png", (int)mWidth, (int)mHeight, (int)(mWidth * 4), rp.TexData0.ToBytes(), EngineNS.EIMAGE_FILE_FORMAT.PNG);
                //    rp.CaptureSV.mFrameBuffer.GetSRV_RenderTarget(1).Save2File($"D:\\{camIdx}_ref.png", (int)mWidth, (int)mHeight, (int)(mWidth * 4), rp.TexData1.ToBytes(), EngineNS.EIMAGE_FILE_FORMAT.PNG);

                //    return true;
                //}, EngineNS.Thread.Async.EAsyncTarget.AsyncEditor);
            }

            var cmd            = EngineNS.CEngine.Instance.RenderContext.ImmCommandList;
            var actirIdTexture = rp.GetActoridTexture();
            cmd.CSSetShaderResource(textureIdx, actirIdTexture);


            uint texSize = 512;
            cmd.CSDispatch(texSize / 8, texSize / 8, 1);
        }
        /*EngineNS.Graphics.CGfxCamera mCamera;
         * async Task Test_Init()
         * {
         *  var rc = EngineNS.CEngine.Instance.RenderContext;
         *  mCamera = new EngineNS.Graphics.CGfxCamera();
         *  mCamera.Init(rc, false);
         *  mCamera.PerspectiveFovLH((float)(System.Math.PI * 0.6f), mWidth, mHeight);
         *  mSceneCaptureRP = new EngineNS.Graphics.RenderPolicy.CGfxRP_SceneCapture();
         *  await mSceneCaptureRP.Init(rc, mWidth, mHeight, mCamera, IntPtr.Zero);
         * }
         *
         * void Test_TickLogic(EditorCommon.ViewPort.ViewPortControl vp)
         * {
         * }
         * private void Test_TickRender(EditorCommon.ViewPort.ViewPortControl vp)
         * {
         *  if (mCamera == null)
         *      return;
         *  var rc = EngineNS.CEngine.Instance.RenderContext;
         *  EngineNS.Vector3[] camDirs = { EngineNS.Vector3.UnitX, -EngineNS.Vector3.UnitZ, -EngineNS.Vector3.UnitX, EngineNS.Vector3.UnitZ, EngineNS.Vector3.UnitY, -EngineNS.Vector3.UnitY };
         *  EngineNS.Vector3[] camUps = { EngineNS.Vector3.UnitY, EngineNS.Vector3.UnitY, EngineNS.Vector3.UnitY, EngineNS.Vector3.UnitY, EngineNS.Vector3.UnitZ, -EngineNS.Vector3.UnitZ };
         *  rc.BindCurrentSwapChain(vp.RPolicy.mSwapChain);
         *  for (int camIdx = 0; camIdx < 6; camIdx++)
         *  {
         *      var dir = camDirs[camIdx];
         *      var pos = new EngineNS.Vector3(0, 1, 0);
         *      var up = camUps[camIdx];
         *      var lookat = pos + dir;
         *      mCamera.LookAtLH(pos, lookat, up);
         *      mCamera.BeforeFrame();
         *
         *      vp.World.CheckVisible(rc, mCamera);
         *      vp.World.Tick();
         *
         *      mSceneCaptureRP.TickLogic(null, rc);
         *      mCamera.SwapBuffer(true);
         *
         *      mSceneCaptureRP.TickRender(null);
         *      mCamera.ClearAllRenderLayerData();
         *  }
         *  //rc.Present(0, 0);
         *
         * }
         * private void Test_TickSync(EditorCommon.ViewPort.ViewPortControl vp)
         * {
         *
         * }*/

        async Task BuildSceneCaptures(EngineNS.GamePlay.SceneGraph.GSceneGraph processScene, List <EngineNS.GamePlay.Actor.GActor> drawActors)
        {
            if (processScene.SceneFilename == EngineNS.RName.EmptyName)
            {
                EditorCommon.MessageBox.Show("没有找到当前场景的保存路径!");
                return;
            }
            var rc = EngineNS.CEngine.Instance.RenderContext;

            var camera = new EngineNS.Graphics.CGfxCamera();

            camera.Init(rc, false);
            camera.PerspectiveFovLH((float)(System.Math.PI * 0.6f), mWidth, mHeight);
            mSceneCaptureRP = new EngineNS.Graphics.RenderPolicy.CGfxRP_SceneCapture();
            await mSceneCaptureRP.Init(rc, mWidth, mHeight, camera, IntPtr.Zero);

            mSceneCaptureRP.CaptureRGBData = false;

            // Compute shader
            //////const string CSVersion = "cs_5_0";
            //////var macros = new EngineNS.CShaderDefinitions();
            //////var shaderFile = EngineNS.RName.GetRName("Shaders/Compute/extractID.compute").Address;
            //////var csMain0 = rc.CompileHLSLFromFile(shaderFile, "CSMain", CSVersion, macros);
            //////var csMain1 = rc.CompileHLSLFromFile(shaderFile, "CSMain1", CSVersion, macros);
            //////var cs = rc.CreateComputeShader(csMain0);
            //////var cs1 = rc.CreateComputeShader(csMain1);

            uint numElem = (UInt32)drawActors.Count;
            //// debug
            //numElem = 255 * 255 * 2;
            //////var bfDesc = new EngineNS.CGpuBufferDesc();
            //////bfDesc.ToDefault();
            //////bfDesc.ByteWidth = 4 * numElem;
            //////bfDesc.StructureByteStride = 4;
            //////var buffer = rc.CreateGpuBuffer(bfDesc, IntPtr.Zero);
            //////var uavDesc = new EngineNS.CUnorderedAccessViewDesc();
            //////uavDesc.ToDefault();
            //////uavDesc.Buffer.NumElements = numElem;
            //////var uav = rc.CreateUnorderedAccessView(buffer, uavDesc);

            //////// 一个uint保存16个actorID的权重
            //////var numElemUAV1 = numElem / 16 + 1;
            //////bfDesc.ByteWidth = 4* numElemUAV1;
            //////bfDesc.StructureByteStride = 4;
            //////var buffer1 = rc.CreateGpuBuffer(bfDesc, IntPtr.Zero);
            //////uavDesc.Buffer.NumElements = numElemUAV1;
            //////var uav1 = rc.CreateUnorderedAccessView(buffer1, uavDesc);

            //////var cbIndex = csMain1.FindCBufferDesc("CaptureEnv");
            //////EngineNS.CConstantBuffer cbuffer = null;
            //////if((int)cbIndex!=-1)
            //////{
            //////    cbuffer = rc.CreateConstantBuffer(csMain1, cbIndex);

            //////    var varIdx = cbuffer.FindVar("rateWeight");
            //////    cbuffer.SetValue(varIdx, new EngineNS.Quaternion(1, 1, 10000, 100000), 0);
            //////    cbuffer.FlushContent2(rc);
            //////}

            //////var texIdx = csMain0.FindSRVDesc("SrcTexture");

            var cmd = rc.ImmCommandList;

            UInt32 cbIndex;

            EngineNS.CConstantBuffer      cbuffer;
            EngineNS.CGpuBuffer           buffer_visible;
            EngineNS.CShaderDesc          csMain_visible;
            EngineNS.CComputeShader       cs_visible;
            EngineNS.CUnorderedAccessView uav_visible;
            EngineNS.CGpuBuffer           buffer_setBit;
            EngineNS.CShaderDesc          csMain_setBit;
            EngineNS.CComputeShader       cs_setBit;
            EngineNS.CUnorderedAccessView uav_setBit;
            EngineNS.CShaderDesc          csMain_Clear;
            EngineNS.CComputeShader       cs_clear;
            UInt32 textureIdx;

            EditorCommon.PVSAssist.ComputeShaderInit(numElem, out cbIndex, out cbuffer, out buffer_visible, out csMain_visible, out cs_visible, out uav_visible, out buffer_setBit,
                                                     out csMain_setBit, out cs_setBit, out uav_setBit,
                                                     out csMain_Clear, out cs_clear,
                                                     out textureIdx);

            var checkVisibleParam = new EngineNS.GamePlay.SceneGraph.CheckVisibleParam();

            foreach (var actor in drawActors)
            {
                actor.OnCheckVisible(rc.ImmCommandList, processScene, camera, checkVisibleParam);
            }
            //mSceneCaptureRP.TickLogic(null, rc);

            int VoxelMemSize = ((int)numElem / 8 + 1) + 4;

            // 读取geoScene
            var t1 = EngineNS.Support.Time.HighPrecision_GetTickCount();

            EngineNS.Profiler.Log.WriteLine(EngineNS.Profiler.ELogTag.Info, "Editor", "Begin Capture Scene VIS");
            var geoScene = await EngineNS.Bricks.HollowMaker.GeomScene.CreateGeomScene(processScene.SceneFilename.Address + "/geoscene.dat");

            EditorCommon.Controls.Debugger.PVSDebugger.GeoScene = geoScene;
            for (int j = 0; j < geoScene.AgentDatas.Count; j++)
            {
                var capDataDir = processScene.SceneFilename.Address + "/capturedata/" + j + "/";
                if (!EngineNS.CEngine.Instance.FileManager.DirectoryExists(capDataDir))
                {
                    EngineNS.CEngine.Instance.FileManager.CreateDirectory(capDataDir);
                }

                List <EngineNS.Support.BitSet> savedBitsets = new List <EngineNS.Support.BitSet>();
                var agentData = geoScene.AgentDatas[j];
                var memSize   = VoxelMemSize * agentData.AgentData.Count;
                for (int i = 0; i < agentData.AgentData.Count; i++)
                {
                    var geoBox = geoScene.AgentDatas[j].AgentData[i];
                    EditorCommon.PVSAssist.CaptureGeoBox(geoBox, agentData, camera, rc, numElem, mSceneCaptureRP, cmd, cbIndex, cbuffer, buffer_visible, csMain_visible, cs_visible, uav_visible, buffer_setBit, csMain_setBit, cs_setBit, uav_setBit, cs_clear, savedBitsets, textureIdx, null);
                }

                var saveXnd  = EngineNS.IO.XndHolder.NewXNDHolder();
                var voxelAtt = saveXnd.Node.AddAttrib("voxelData");
                voxelAtt.BeginWrite();
                voxelAtt.Write(agentData.BVSize);
                voxelAtt.Write(agentData.Mat);
                voxelAtt.Write(agentData.AgentData.Count);
                for (int i = 0; i < agentData.AgentData.Count; i++)
                {
                    var geoBox   = agentData.AgentData[i];
                    var pvsVoxel = new EngineNS.Bricks.HollowMaker.PVSVoxel();
                    pvsVoxel.Shape        = geoBox.Box;
                    pvsVoxel.X            = geoBox.X;
                    pvsVoxel.Y            = geoBox.Y;
                    pvsVoxel.Z            = geoBox.Z;
                    pvsVoxel.Bits         = savedBitsets[i];
                    pvsVoxel.LinkedVoxels = new List <int>(geoBox.Neighbors);

                    var str = pvsVoxel.Bits.ToBase64String();
                    pvsVoxel.BitsHash = (UInt32)(str.GetHashCode());

                    voxelAtt.WriteMetaObject(pvsVoxel);
                }
                voxelAtt.EndWrite();
                EngineNS.IO.XndHolder.SaveXND(capDataDir + "vis.data", saveXnd);

                System.GC.Collect();
            }


            var t2 = EngineNS.Support.Time.HighPrecision_GetTickCount();

            EngineNS.Profiler.Log.WriteLine(EngineNS.Profiler.ELogTag.Info, "Editor", $"End Capture Scene VIS,Time = {(t2-t1)/1000000}");

            mSceneCaptureRP.Cleanup();
        }