public static void SetCullingBuffer(ref CullingBuffers buffers)
 {
     buffers.cullingShader.SetBuffer(buffers.csmainKernel, ShaderIDs.allBounds, buffers.allBoundBuffer);
     buffers.cullingShader.SetBuffer(buffers.csmainKernel, ShaderIDs.Transforms, buffers.resultBuffer);
     buffers.cullingShader.SetBuffer(buffers.csmainKernel, ShaderIDs.sizeBuffer, buffers.sizeBuffer);
     buffers.sizeBuffer.SetData(ComputeShaderUtility.zero);
 }
 public static void Dispose(ref CullingBuffers buffers)
 {
     buffers.allBoundBuffer.Dispose();
     buffers.resultBuffer.Dispose();
     buffers.sizeBuffer.Dispose();
     buffers.vertexBuffer.Dispose();
     buffers.lastFrameMatricesBuffer.Dispose();
 }
 public static void SetLastFrameMatrix(ref CullingBuffers cullingBuffers, ref Matrix4x4 lastVP)
 {
     if (cullingBuffers.currentCullingResult > 0)
     {
         cullingBuffers.cullingShader.SetMatrix(ShaderIDs.LAST_VP_MATRIX, lastVP);
         cullingBuffers.cullingShader.SetBuffer(cullingBuffers.lastMatricesKernel, ShaderIDs.Transforms, cullingBuffers.resultBuffer);
         cullingBuffers.cullingShader.SetBuffer(cullingBuffers.lastMatricesKernel, ShaderIDs.lastFrameMatrices, cullingBuffers.lastFrameMatricesBuffer);
         ComputeShaderUtility.Dispatch(cullingBuffers.cullingShader, cullingBuffers.lastMatricesKernel, cullingBuffers.currentCullingResult, THREADGROUPCOUNT);
     }
 }
        public static void DrawNoMotionVectors(ref CullingBuffers cullingBuffers, Material proceduralMaterial)
        {
            int instanceCount = cullingBuffers.currentCullingResult;

            if (instanceCount == 0)
            {
                return;
            }
            geometryCommandBuffer.SetGlobalBuffer(ShaderIDs.Transforms, cullingBuffers.resultBuffer);
            geometryCommandBuffer.SetGlobalBuffer(ShaderIDs.VertexBuffer, cullingBuffers.vertexBuffer);
            geometryCommandBuffer.DrawProcedural(Matrix4x4.identity, proceduralMaterial, 0, MeshTopology.Triangles, cullingBuffers.vertexBuffer.count, instanceCount);
        }
 public static void InitBuffers(ref CullingBuffers buffers, Vector3[] boundExtents, Matrix4x4[] localToWorldMatrices, Mesh mesh)
 {
     buffers.sizeBuffer              = new ComputeBuffer(1, INTSIZE);
     buffers.allBoundBuffer          = new ComputeBuffer(boundExtents.Length, Bounds.SIZE);
     buffers.resultBuffer            = new ComputeBuffer(boundExtents.Length, TRANSFORMSIZE);
     buffers.csmainKernel            = buffers.cullingShader.FindKernel("CSMain");
     buffers.lastMatricesKernel      = buffers.cullingShader.FindKernel("GetLast");
     buffers.lastFrameMatricesBuffer = new ComputeBuffer(boundExtents.Length, MATRIXSIZE);
     buffers.count = boundExtents.Length;
     buffers.currentCullingResult = 0;
     Bounds[] allBounds = new Bounds[boundExtents.Length];
     for (int i = 0; i < allBounds.Length; ++i)
     {
         allBounds[i].extent             = boundExtents[i];
         allBounds[i].localToWorldMatrix = localToWorldMatrices[i];
     }
     buffers.allBoundBuffer.SetData(allBounds);
     Vector3[] allVertex  = mesh.vertices;
     int[]     allIndices = mesh.triangles;
     Vector2[] allUVs     = mesh.uv;
     Vector4[] tangents   = mesh.tangents;
     Vector3[] normals    = mesh.normals;
     Point[]   points     = new Point[allIndices.Length];
     for (int i = 0; i < allIndices.Length; ++i)
     {
         Point p;
         int   index = allIndices[i];
         p.normal   = normals[index];
         p.vertex   = allVertex[index];
         p.texcoord = allUVs[index];
         p.tangent  = tangents[index];
         points[i]  = p;
     }
     buffers.vertexBuffer = new ComputeBuffer(points.Length, Point.SIZE);
     buffers.vertexBuffer.SetData(points);
 }
        /// <summary>
        /// Run Culling With Compute Shader
        /// </summary>
        /// <param name="view"></param> World To View Matrix
        /// <param name="proj"></param> View To NDC Matrix
        /// <param name="buffers"></param> Buffers
        public static void RunCulling(ref Matrix4x4 view, ref Matrix4x4 proj, ref Matrix4x4 rtProj, ref CullingBuffers buffers)
        {
            Matrix4x4 vp = proj * view;

            buffers.cullingShader.SetMatrix(ShaderIDs._VPMatrix, rtProj * view);
            GetCullingPlanes(ref vp, planes);
            for (int i = 0; i < 6; ++i)
            {
                Vector3 normal   = planes[i].normal;
                float   distance = planes[i].distance;
                planesVector[i] = new Vector4(normal.x, normal.y, normal.z, distance);
            }
            buffers.cullingShader.SetVectorArray(ShaderIDs.planes, planesVector);
            ComputeShaderUtility.Dispatch(buffers.cullingShader, buffers.csmainKernel, buffers.count, THREADGROUPCOUNT);
            buffers.currentCullingResult = (int)GetCullingBufferResult(ref buffers);
        }
 public static uint GetCullingBufferResult(ref CullingBuffers buffers)
 {
     buffers.sizeBuffer.GetData(currentDrawSize);
     return(currentDrawSize[0]);
 }