AsyncGPUReadback Implementations - Gemini-team/Gemini GitHub Wiki

The functional way of doing AsyncGPUReadback

private IEnumerator UpdateLidarLines()
        {
            // Setting up Compute Buffers

            int kernelHandle = computeShader.FindKernel("CSMain");

            SetCameraBuffers(computeShader, "CSMain", lidarCameras);

            computeShader.SetTexture(kernelHandle, "sphericalMaskImage", sphericalMaskImage);
            computeShader.SetBuffer(kernelHandle, "sphericalPixelCoordinates", pixelCoordinatesBuffer);

            UnifiedArray<Vector3> particleUnifiedArray = new UnifiedArray<Vector3>(NrOfCameras * NrOfLasers * LidarHorisontalRes, sizeof(float) * 3, "lines");
            particleUnifiedArray.SetBuffer(computeShader, "CSMain");

            lidarDataByte = new UnifiedArray<byte>(NrOfCameras * NrOfLasers * LidarHorisontalRes, sizeof(float) * 6, "LidarData");
            lidarDataByte.SetBuffer(computeShader, "CSMain");

            lidarData = new UnifiedArray<LidarFieldData>(NrOfCameras * NrOfLasers * LidarHorisontalRes, sizeof(float) * 6, "LidarData2");
            lidarData.SetBuffer(computeShader, "CSMain");

            while (true)
            {
                computeShader.Dispatch(kernelHandle, (int)Mathf.Ceil((float)NrOfCameras * (float)NrOfLasers * (float)LidarHorisontalRes / 1024.0f), 1, 1);

                var request = AsyncGPUReadback.Request(particleUnifiedArray.buffer);
                yield return new WaitUntil(() => request.done);
                particleUnifiedArray.nativeArray = request.GetData<Vector3>();
                if (ShowPointCloud) { particleMesh.SetVertices(particleUnifiedArray.nativeArray); }

                var request2 = AsyncGPUReadback.Request(lidarDataByte.buffer);
                yield return new WaitUntil(() => request2.done);
                lidarDataByte.nativeArray = request2.GetData<byte>();
                byte[] LidarByteArray = lidarDataByte.nativeArray.ToArray();

                var request3 = AsyncGPUReadback.Request(lidarData.buffer);
                yield return new WaitUntil(() => request3.done);
                lidarData.nativeArray = request3.GetData<LidarFieldData>();

                // https://answers.unity.com/questions/1581776/bytearray-impossible-to-convert-1.html
                // Vær obs på om systemene kjører big / small endian
                var stringarray = System.BitConverter.ToSingle(LidarByteArray, 0);
                Debug.Log("x in bytes: " + stringarray.ToString());


                Debug.Log("Intesnsity: " + lidarData.nativeArray[0].intensity.ToString());
            }
        }
⚠️ **GitHub.com Fallback** ⚠️