Beispiel #1
0
        public override void OnUpdateSystem(bool rebuildSDF, RenderTexture sourceRT, RenderTexture boundRT)
        {
            if (!rebuildSDF)
            {
                return;
            }

            rts[READ]  = sourceRT;
            rts[WRITE] = anotherTex;

            // clear list buffer
            // resolutionX and resolutionY should be odd, otherwise it will cause symchronized problem
            iterateListBuffer.SetCounterValue(0);

            // kernel0: just need bound tex BOUND_TEX_IS_BOUNDARY, init grid val and surface grid val, build first queue, init complete tag
            LevelSet2DFSMComputeShader.SetTexture(InitSDFKernel, "_BoundTex", boundRT);
            LevelSet2DFSMComputeShader.SetTexture(InitSDFKernel, "_SDFRead", rts[READ]);
            LevelSet2DFSMComputeShader.SetInt("_ResolutionX", resolutionX);
            LevelSet2DFSMComputeShader.SetInt("_ResolutionY", resolutionY);
            LevelSet2DFSMComputeShader.SetBuffer(InitSDFKernel, "_ItrateListBuffer", iterateListBuffer);
            LevelSet2DFSMComputeShader.Dispatch(InitSDFKernel, resolutionX, resolutionY, 1);

            for (int dilate = 0; dilate < dilateTime; ++dilate)
            {
                ComputeBuffer.CopyCount(iterateListBuffer, iterateListArgBuffer, 0);
                iterateListBufferTo.SetCounterValue(0);

                // kernel2: SweepingKernel, sweeping, build next queue
                LevelSet2DFSMComputeShader.SetInt("_ResolutionX", resolutionX);
                LevelSet2DFSMComputeShader.SetInt("_ResolutionY", resolutionY);
                LevelSet2DFSMComputeShader.SetTexture(SweepingKernel, "_BoundTex", boundRT);
                LevelSet2DFSMComputeShader.SetTexture(SweepingKernel, "_SDFRead", rts[READ]);
                LevelSet2DFSMComputeShader.SetBuffer(SweepingKernel, "_ItrateListBufferNew", iterateListBufferTo);
                LevelSet2DFSMComputeShader.SetBuffer(SweepingKernel, "_ItrateListBufferRead", iterateListBuffer);
                LevelSet2DFSMComputeShader.DispatchIndirect(SweepingKernel, iterateListArgBuffer);

                Liquid2DUtils.Swap(ref iterateListBuffer, ref iterateListBufferTo);
            }
        }
Beispiel #2
0
        public override void OnUpdateSystem(bool rebuildSDF, RenderTexture sourceRT, RenderTexture boundRT)
        {
            if (!rebuildSDF)
            {
                return;
            }

            rts[READ]  = sourceRT;
            rts[WRITE] = anotherTex;

            // clear list buffer
            iterateListBuffer.SetCounterValue(0);

            // kernel0: init bound texture and other buffers
            LevelSet2DFIMComputeShader.SetTexture(BuildInitBoundTex, "_BoundTex", boundRT);
            LevelSet2DFIMComputeShader.SetTexture(BuildInitBoundTex, "_SDFRead", rts[READ]);
            LevelSet2DFIMComputeShader.SetInt("_ResolutionX", resolutionX);
            LevelSet2DFIMComputeShader.SetInt("_ResolutionY", resolutionY);
            LevelSet2DFIMComputeShader.Dispatch(BuildInitBoundTex, resolutionX, resolutionY, 1);

            // kernel1: mark known point
            LevelSet2DFIMComputeShader.SetTexture(MarkSurface, "_BoundTex", boundRT);
            LevelSet2DFIMComputeShader.SetTexture(MarkSurface, "_SDFRead", rts[READ]);
            LevelSet2DFIMComputeShader.SetInt("_ResolutionX", resolutionX);
            LevelSet2DFIMComputeShader.SetInt("_ResolutionY", resolutionY);
            LevelSet2DFIMComputeShader.Dispatch(MarkSurface, resolutionX, resolutionY, 1);

            // kernel2: build first queue, copy read to write tex
            LevelSet2DFIMComputeShader.SetBuffer(BuildFirstQueue, "_ItrateListBuffer", iterateListBuffer);
            LevelSet2DFIMComputeShader.SetTexture(BuildFirstQueue, "_BoundTex", boundRT);
            LevelSet2DFIMComputeShader.SetTexture(BuildFirstQueue, "_SDFRead", rts[READ]);
            LevelSet2DFIMComputeShader.SetTexture(BuildFirstQueue, "_SDFWrite", rts[WRITE]);
            LevelSet2DFIMComputeShader.SetInt("_ResolutionX", resolutionX);
            LevelSet2DFIMComputeShader.SetInt("_ResolutionY", resolutionY);
            LevelSet2DFIMComputeShader.Dispatch(BuildFirstQueue, resolutionX, resolutionY, 1);

            // kernel3: debug queue
            // LevelSet2DFMMStarComputeShader.SetBuffer(DebugKernel, "_ItrateListBufferRead", iterateListBuffer);
            // LevelSet2DFMMStarComputeShader.SetTexture(DebugKernel, "_SDFWrite", rts[WRITE]);
            // LevelSet2DFMMStarComputeShader.DispatchIndirect(DebugKernel, iterateListArgBuffer);

            // loop:
            // 1、 dispatchIndirect our list, calc SDF val due to BOUND_TEX_IS_KNOWN pixel, set BOUND_TEX_IS_KNOWN, build next list, generate next queue, swap listbuffer
            for (int dilate = 0; dilate < dilateTime; ++dilate)
            {
                Liquid2DUtils.Swap(ref READ, ref WRITE);

                // kernel4: FIMCopyKernel, copy read to write tex
                LevelSet2DFIMComputeShader.SetTexture(FIMCopyKernel, "_SDFRead", rts[READ]);
                LevelSet2DFIMComputeShader.SetTexture(FIMCopyKernel, "_SDFWrite", rts[WRITE]);
                LevelSet2DFIMComputeShader.Dispatch(FIMCopyKernel, resolutionX + 2, resolutionY + 2, 1);


                ComputeBuffer.CopyCount(iterateListBuffer, iterateListArgBuffer, 0);
                iterateListBufferTo.SetCounterValue(0);

                // kernel5: FIMLoopKernel, require blit between two textures
                LevelSet2DFIMComputeShader.SetInt("_ResolutionX", resolutionX);
                LevelSet2DFIMComputeShader.SetInt("_ResolutionY", resolutionY);
                LevelSet2DFIMComputeShader.SetTexture(FIMLoopKernel, "_BoundTex", boundRT);
                LevelSet2DFIMComputeShader.SetTexture(FIMLoopKernel, "_SDFRead", rts[READ]);
                LevelSet2DFIMComputeShader.SetTexture(FIMLoopKernel, "_SDFWrite", rts[WRITE]);
                LevelSet2DFIMComputeShader.SetBuffer(FIMLoopKernel, "_ItrateListBufferRead", iterateListBuffer);
                LevelSet2DFIMComputeShader.SetBuffer(FIMLoopKernel, "_ItrateListBufferNew", iterateListBufferTo);
                LevelSet2DFIMComputeShader.DispatchIndirect(FIMLoopKernel, iterateListArgBuffer);

                ComputeBuffer.CopyCount(iterateListBufferTo, iterateListArgBufferTo, 0);

                Liquid2DUtils.Swap(ref iterateListBuffer, ref iterateListBufferTo);
                Liquid2DUtils.Swap(ref iterateListArgBuffer, ref iterateListArgBufferTo);
            }
        }