private void RunSetGeometryInstanceToMainShader(int count,
                                                        ref TopLevelAccelerationBuffer bvhBuffer,
                                                        ref SortedList <ISIdx, ObjectLevelAccelerationGeometryBuffer> objectLvAccGeoBuffers,
                                                        ref SortedList <ISIdx, ObjectLevelAccelerationGeometryMappingCollectionBuffer> objectLvAccGeoMapBuffers,
                                                        ref PrimitiveBuffer primitiveBuffer,
                                                        ref WorldToLocalBuffer worldToPrimitiveBuffer,
                                                        string[] intersectShaderNames,
                                                        ref SortedList <ISIdx, GeometryInstanceBuffer> geoInsBuffers,
                                                        ref TopLevelAccelerationGeometryMappingCollectionBuffer topLevelAccGeoMapColBuffer)
        {
            m_mainShader.SetInt("_NumOfPrimitive", count);
            m_mainShader.SetBuffer(kIndex, "_Primitives", primitiveBuffer);
            m_mainShader.SetBuffer(kIndex, "_WorldToPrimitives", worldToPrimitiveBuffer);
            m_mainShader.SetBuffer(kIndex, "_BVHTree", bvhBuffer);
            m_mainShader.SetBuffer(kIndex, "_TopLevelAccelerationGeometryMapping", topLevelAccGeoMapColBuffer);

            empty = new ComputeBuffer(1, sizeof(float));

            for (int intersectIdx = 0; intersectIdx < intersectShaderNames.Length; intersectIdx++)
            {
                if (geoInsBuffers.ContainsKey(intersectIdx))
                {
                    m_mainShader.SetBuffer(kIndex, $"_{intersectShaderNames[intersectIdx]}", geoInsBuffers[intersectIdx]);
                }
                else
                {
                    m_mainShader.SetBuffer(kIndex, $"_{intersectShaderNames[intersectIdx]}", empty);
                }
            }
            // -
            // -
            // -
            // -
            // -

            var objectLvAccGeoBuffersIter = objectLvAccGeoBuffers.GetEnumerator();

            while (objectLvAccGeoBuffersIter.MoveNext())
            {
                m_mainShader.SetBuffer(kIndex, $"_{intersectShaderNames[objectLvAccGeoBuffersIter.Current.Key]}GeometryData", objectLvAccGeoBuffersIter.Current.Value);
            }

            var objectLvAccGeoMapBuffersIter = objectLvAccGeoMapBuffers.GetEnumerator();

            while (objectLvAccGeoMapBuffersIter.MoveNext())
            {
                m_mainShader.SetBuffer(kIndex, $"_{intersectShaderNames[objectLvAccGeoMapBuffersIter.Current.Key]}GeometryMapping", objectLvAccGeoMapBuffersIter.Current.Value);
            }
        }
 private void RunLoadGeometryToBuffer(SceneParseResult sceneParseResult,
                                      ref TopLevelAccelerationBuffer topLevelAcc,
                                      ref SortedList <ISIdx, ObjectLevelAccelerationGeometryBuffer> objectLvAccGeoBuffers,
                                      ref SortedList <ISIdx, ObjectLevelAccelerationGeometryMappingCollectionBuffer> objectLvAccGeoMapBuffers,
                                      ref PrimitiveBuffer primitiveBuffer,
                                      ref WorldToLocalBuffer worldToPrimitiveBuffer,
                                      ref SortedList <ISIdx, GeometryInstanceBuffer> gemoetryInstanceBuffers,
                                      ref TopLevelAccelerationGeometryMappingCollectionBuffer topLevelAccGeoMap)
 {
     LoadBufferWithGeometryInstances(sceneParseResult,
                                     bvhBuffer: ref topLevelAcc,
                                     objectLvAccGeoBuffers: ref objectLvAccGeoBuffers,
                                     objectLvAccGeoMapBuffers: ref objectLvAccGeoMapBuffers,
                                     primitiveBuffer: ref primitiveBuffer,
                                     worldToPrimitiveBuffer: ref worldToPrimitiveBuffer,
                                     gemoetryInstanceBuffers: ref gemoetryInstanceBuffers,
                                     topLevelAccGeoMapColBuffer: ref topLevelAccGeoMap);
 }
        private void LoadBufferWithGeometryInstances(SceneParseResult sceneParseResult,
                                                     ref TopLevelAccelerationBuffer bvhBuffer,
                                                     ref SortedList <ISIdx, ObjectLevelAccelerationGeometryBuffer> objectLvAccGeoBuffers,
                                                     ref SortedList <ISIdx, ObjectLevelAccelerationGeometryMappingCollectionBuffer> objectLvAccGeoMapBuffers,
                                                     ref PrimitiveBuffer primitiveBuffer,
                                                     ref WorldToLocalBuffer worldToPrimitiveBuffer,
                                                     ref SortedList <ISIdx, GeometryInstanceBuffer> gemoetryInstanceBuffers,
                                                     ref TopLevelAccelerationGeometryMappingCollectionBuffer topLevelAccGeoMapColBuffer)
        {
            gemoetryInstanceBuffers.Clear();
            objectLvAccGeoBuffers.Clear();
            objectLvAccGeoMapBuffers.Clear();

            var geoInsIter = sceneParseResult.GeometryInstances.GetEnumerator();

            while (geoInsIter.MoveNext())
            {
                var buffer = new GeometryInstanceBuffer(sceneParseResult.GetGeometryInstancesCount(geoInsIter.Current.Key), sceneParseResult.GetGeometryInstancesStride(geoInsIter.Current.Key));
                buffer.SetData(geoInsIter.Current.Value);
                gemoetryInstanceBuffers.Add(geoInsIter.Current.Key, buffer);
            }

            var objectLevelAccGeoIter = sceneParseResult.ObjectLevelAccelerationGeometries.GetEnumerator();

            while (objectLevelAccGeoIter.MoveNext())
            {
                var buffer = new ObjectLevelAccelerationGeometryBuffer(objectLevelAccGeoIter.Current.Value.Count, sizeof(float));
                buffer.SetData(objectLevelAccGeoIter.Current.Value);
                objectLvAccGeoBuffers.Add(objectLevelAccGeoIter.Current.Key, buffer);
            }

            var objectLvAccGeoMapIter = sceneParseResult.ObjectLevelAccelerationGeometryMapping.GetEnumerator();

            while (objectLvAccGeoMapIter.MoveNext())
            {
                var buffer = new ObjectLevelAccelerationGeometryMappingCollectionBuffer(objectLvAccGeoMapIter.Current.Value.Count, sizeof(int));
                buffer.SetData(objectLvAccGeoMapIter.Current.Value);
                objectLvAccGeoMapBuffers.Add(objectLvAccGeoMapIter.Current.Key, buffer);
            }

            var objectLevelAccStrInsIter = sceneParseResult.ObjectLevelAccelerationStructures.GetEnumerator();

            while (objectLevelAccStrInsIter.MoveNext())
            {
                var buffer = new GeometryInstanceBuffer(objectLevelAccStrInsIter.Current.Value.Count, sizeof(float));
                buffer.SetData(objectLevelAccStrInsIter.Current.Value);
                gemoetryInstanceBuffers.Add(objectLevelAccStrInsIter.Current.Key, buffer);
            }

            sceneParseResult.TopLevelBVH.Flatten(
                flatten: out List <RTBoundingBoxToGPU> _flattenBVH,
                accelerationGeometryMapping: out List <int> _topLevelAccelerationGeometryMapping);
            bvhBuffer = new TopLevelAccelerationBuffer(_flattenBVH.Count, RTBoundingBox.stride);
            bvhBuffer.SetData(_flattenBVH);
            primitiveBuffer = new PrimitiveBuffer(sceneParseResult.Primitives.Count, Primitive.GetStride());
            primitiveBuffer.SetData(sceneParseResult.Primitives);
            topLevelAccGeoMapColBuffer = new TopLevelAccelerationGeometryMappingCollectionBuffer(_topLevelAccelerationGeometryMapping.Count, sizeof(int));
            topLevelAccGeoMapColBuffer.SetData(_topLevelAccelerationGeometryMapping);
            worldToPrimitiveBuffer = new WorldToLocalBuffer(sceneParseResult.WorldToPrimitive.Count, sizeof(float) * 16);
            worldToPrimitiveBuffer.SetData(sceneParseResult.WorldToPrimitive);
        }