private void RunLoadLightToBuffer(SceneParseResult sceneParseResult, ref ComputeBuffer lightInfoBuffer)
        {
            int numberOfLights = sceneParseResult.Lights.Count;

            lightInfoBuffer = new ComputeBuffer(numberOfLights, RTLightInfo.Stride);
            lightInfoBuffer.SetData(sceneParseResult.Lights);
        }
        private void ParseLight(
            GameObject[] roots,
            ref SceneParseResult sceneParseResult)
        {
            var lights = GetAllLights(roots);

            if (!IsAllLightsDirty(lights))
            {
                // All the lights are unchange, no need to rebuild
                return;
            }

            sceneParseResult.ClearAllLights();

            foreach (var light in lights)
            {
                if (light.gameObject.activeInHierarchy)
                {
                    sceneParseResult.AddLight(light.GetLightInfo());
                }
            }

            // // Placeholder for scene parsing
            // sceneParseResult.AddLight(new RTLightInfo(
            //     position: new Vector3(1, 1, 1),
            //     rotation: new Vector3(0, 0, -1),
            //     type: 0
            // ));

            // sceneParseResult.AddLight(new RTLightInfo(
            //     position: new Vector3(0, 0, 0),
            //     rotation: new Vector3(0, 0, 0),
            //     type: 1
            // ));
        }
        private void LoadBufferWithGeometryInstances(
            SceneParseResult sceneParseResult,
            ref ComputeBuffer bvhBuffer,
            ref ComputeBuffer primitiveBuffer,
            ref ComputeBuffer worldToPrimitiveBuffer,
            ref SortedList <ISIdx, ComputeBuffer> gemoetryInstanceBuffers)
        {
            foreach (var item in gemoetryInstanceBuffers)
            {
                item.Value?.Release();
            }
            gemoetryInstanceBuffers.Clear();

            var geoInsIter = sceneParseResult.GeometryInstances.GetEnumerator();

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

            sceneParseResult.TopLevelBVH.Flatten(
                scenePrimitives: sceneParseResult.Primitives,
                flatten: out List <RTBoundingBox> flattenBVH,
                reorderedPrimitives: out List <Primitive> reorderedPrimitives);
            bvhBuffer = new ComputeBuffer(flattenBVH.Count, RTBoundingBox.stride);
            bvhBuffer.SetData(flattenBVH);
            primitiveBuffer = new ComputeBuffer(reorderedPrimitives.Count, Primitive.GetStride());
            primitiveBuffer.SetData(reorderedPrimitives);
            worldToPrimitiveBuffer = new ComputeBuffer(reorderedPrimitives.Count, sizeof(float) * 16);
            worldToPrimitiveBuffer.SetData(sceneParseResult.WorldToPrimitive);
        }
        private void RunLoadMaterialToBuffer(SceneParseResult sceneParseResult,
                                             ref ComputeShader mainShader)
        {
            SceneTextureCollection sceneTexture = new SceneTextureCollection();

            PipelineMaterialToBuffer.MaterialsToBuffer(sceneParseResult.Materials,
                                                       ref mainShader,
                                                       ref sceneTexture);

            PipelineMaterialToBuffer.LoadTextureToBuffer(sceneTexture, ref mainShader);
        }
        private void RunLoadMaterialToBuffer(List <ComputeBuffer> computeShadersForMaterials,
                                             SceneParseResult sceneParseResult,
                                             ref ComputeShader mainShader)
        {
            sceneParseResult.ClearAllMaterials();

            PipelineMaterialToBuffer.MaterialsToBuffer(computeShadersForMaterials,
                                                       sceneParseResult,
                                                       ref mainShader);

            PipelineMaterialToBuffer.LoadTextureToBuffer(sceneParseResult, ref mainShader);
        }
 private void RunLoadGeometryToBuffer(
     SceneParseResult sceneParseResult,
     ref ComputeBuffer bvhBuffer,
     ref ComputeShader mainShader,
     ref ComputeBuffer primitiveBuffer,
     ref ComputeBuffer worldToPrimitiveBuffer,
     ref SortedList <ISIdx, ComputeBuffer> gemoetryInstanceBuffers)
 {
     LoadBufferWithGeometryInstances(
         sceneParseResult,
         bvhBuffer: ref bvhBuffer,
         primitiveBuffer: ref primitiveBuffer,
         worldToPrimitiveBuffer: ref worldToPrimitiveBuffer,
         gemoetryInstanceBuffers: ref gemoetryInstanceBuffers);
 }
 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);
 }
예제 #8
0
        private void ParseLight(ref SceneParseResult sceneParseResult)
        {
            sceneParseResult.ClearAllLights();

            // Placeholder for scene parsing
            sceneParseResult.AddLight(new RTLightInfo(
                                          instance: 0,
                                          position: new Vector3(1, 1, 1),
                                          rotation: new Vector3(0, 0, -1),
                                          type: 0
                                          ));

            sceneParseResult.AddLight(new RTLightInfo(
                                          instance: 0,
                                          position: new Vector3(0, 0, 0),
                                          rotation: new Vector3(0, 0, 0),
                                          type: 1
                                          ));
        }
        private void RunParseScene()
        {
            var scene = SceneManager.GetActiveScene();

            sceneParseResult = SceneParser.Instance.ParseScene(scene);
        }
예제 #10
0
        private void ParseGeometry(
            GameObject[] roots,
            ref SceneParseResult sceneParseResult)
        {
            // TODO: Optimize dynamic array generation
            sceneParseResult.ClearAllPrimitives();
            sceneParseResult.ClearAllGeometries();
            sceneParseResult.ClearAllMaterials();
            sceneParseResult.ClearTopLevelBVH();

            foreach (var root in roots)
            {
                RTRenderer[] renderers = root.GetComponentsInChildren <RTRenderer>();

                foreach (var renderer in renderers)
                {
                    if (renderer.gameObject.activeSelf)
                    {
                        List <float> geoInsData           = renderer.geometry.GetGeometryInstanceData();
                        var          closestShaderGUID    = renderer.material.GetClosestHitGUID();
                        int          closestShaderIndex   = CustomShaderDatabase.Instance.GUIDToShaderIndex(closestShaderGUID, EShaderType.ClosestHit);
                        var          intersectShaderGUID  = renderer.geometry.GetIntersectShaderGUID();
                        int          intersectShaderIndex = CustomShaderDatabase.Instance.GUIDToShaderIndex(intersectShaderGUID, EShaderType.Intersect);

                        RTMaterial material = renderer.material;

                        if (geoInsData == null || material == null)
                        {
                            continue;
                        }

                        if (!sceneParseResult.GeometryStride.ContainsKey(intersectShaderIndex))
                        {
                            sceneParseResult.GeometryStride.Add(intersectShaderIndex, renderer.geometry.GetStride());
                        }

                        sceneParseResult.AddGeometryData(
                            geometryData: geoInsData,
                            intersectIndex: intersectShaderIndex
                            );

                        int startIndex = sceneParseResult.AddGeometryCount(
                            count: renderer.geometry.GetCount(),
                            intersectIndex: intersectShaderIndex
                            );

                        sceneParseResult.AddWorldToPrimitive(renderer.gameObject.transform.worldToLocalMatrix);

                        sceneParseResult.AddPrimitive(new Primitive(
                                                          geometryIndex: intersectShaderIndex,
                                                          geometryInstanceBegin: startIndex,
                                                          geometryInstanceCount: renderer.geometry.GetCount(),
                                                          materialIndex: closestShaderIndex,
                                                          transformIndex: sceneParseResult.WorldToPrimitive.Count - 1
                                                          ));

                        var boxOfThisObject = renderer.geometry.GetBoundingBox();
                        sceneParseResult.AddBoundingBox(new RTBoundingBox(
                                                            max: boxOfThisObject.max,
                                                            min: boxOfThisObject.min,
                                                            primitive: sceneParseResult.Primitives.Count - 1
                                                            ));

                        sceneParseResult.AddMaterial(material);
                    }
                }
            }
        }
예제 #11
0
 private SceneParser()
 {
     sceneParseResult = new SceneParseResult();
 }
        private void ParseGeometry(
            GameObject[] roots,
            ref SceneParseResult sceneParseResult)
        {
            var renderers = GetAllRenderers(roots);

            if (!IsAllGeometriesDirty(renderers) && sceneParseResult.Primitives.Count != 0)
            {
                // All the geometries are unchange, no need to rebuild
                return;
            }

            // TODO: Optimize dynamic array generation
            sceneParseResult.ClearAllPrimitives();
            sceneParseResult.ClearAllGeometries();
            sceneParseResult.ClearAllMaterials();
            sceneParseResult.ClearTopLevelBVH();

            foreach (var renderer in renderers)
            {
                if (renderer.gameObject.activeInHierarchy)
                {
                    RTMaterial material = renderer.material;
                    if (renderer.geometry == null || !renderer.geometry.IsGeometryValid() || material == null)
                    {
                        continue;
                    }

                    var closestShaderGUID    = renderer.material.GetClosestHitGUID();
                    int closestShaderIndex   = CustomShaderDatabase.Instance.GUIDToShaderIndex(closestShaderGUID, EShaderType.ClosestHit);
                    var intersectShaderGUID  = renderer.geometry.GetIntersectShaderGUID();
                    int intersectShaderIndex = CustomShaderDatabase.Instance.GUIDToShaderIndex(intersectShaderGUID, EShaderType.Intersect);

                    if (!sceneParseResult.GeometryStride.ContainsKey(intersectShaderIndex))
                    {
                        sceneParseResult.GeometryStride.Add(intersectShaderIndex, renderer.geometry.IsAccelerationStructure() ? 0 : renderer.geometry.GetStride());
                    }

                    if (renderer.geometry.IsAccelerationStructure())
                    {
                        // Such as Low-Level BVH (RTMeshBVH)
                        int mapOffset = sceneParseResult.ObjectLevelAccGeoMapCursor(intersectShaderIndex);
                        int geoOffset = sceneParseResult.ObjectLevelAccGeoCursor(intersectShaderIndex);
                        ((IRTMeshBVH)(renderer.geometry)).BuildBVHAndTriangleList(geoLocalToGlobalIndexOffset: geoOffset,
                                                                                  mappingLocalToGlobalIndexOffset: mapOffset);

                        List <float> geoInsData = renderer.geometry.GetGeometryInstanceData(geoLocalToGlobalIndexOffset: geoOffset,
                                                                                            mappingLocalToGlobalIndexOffset: mapOffset);
                        sceneParseResult.AddAccelerationStructureGeometry(
                            accelerationStructureData: geoInsData,
                            accelGeometryMapping: renderer.geometry.GetAccelerationStructureGeometryMapping(geoLocalToGlobalIndexOffset: geoOffset,
                                                                                                            mappingLocalToGlobalIndexOffset: mapOffset),
                            accelGeometryData: renderer.geometry.GetAccelerationStructureGeometryData(geoLocalToGlobalIndexOffset: geoOffset,
                                                                                                      mappingLocalToGlobalIndexOffset: mapOffset),
                            intersectIndex: intersectShaderIndex
                            );
                    }
                    else
                    {
                        // Standardized Geometry (Sphere, Triangle)
                        List <float> geoInsData = renderer.geometry.GetGeometryInstanceData(geoLocalToGlobalIndexOffset: 0, mappingLocalToGlobalIndexOffset: 0);  // No offset
                        sceneParseResult.AddGeometryData(
                            geometryData: geoInsData,
                            intersectIndex: intersectShaderIndex
                            );
                    }

                    int startIndex = sceneParseResult.AddGeometryCount(
                        count: renderer.geometry.GetCount(),
                        intersectIndex: intersectShaderIndex
                        );

                    int materialInstanceIndex = sceneParseResult.AddMaterial(material);

                    sceneParseResult.AddWorldToPrimitive(renderer.gameObject.transform.worldToLocalMatrix);

                    sceneParseResult.AddPrimitive(new Primitive(
                                                      geometryIndex: intersectShaderIndex,
                                                      geometryInstanceBegin: startIndex,
                                                      geometryInstanceCount: renderer.geometry.GetCount(),
                                                      materialIndex: closestShaderIndex,
                                                      materialInstanceIndex: materialInstanceIndex,
                                                      transformIndex: sceneParseResult.WorldToPrimitive.Count - 1
                                                      ));

                    var boxOfThisObject = renderer.geometry.GetTopLevelBoundingBox(assginedPrimitiveId: sceneParseResult.Primitives.Count - 1);
                    sceneParseResult.AddBoundingBox(boxOfThisObject);
                }
            }
        }
        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);
        }