示例#1
0
        public RenderPipeline(PipelineResources resources)
        {
            eventsGuideBook = new NativeDictionary <UIntPtr, int, PtrEqual>(resources.availiableEvents.Length, Allocator.Persistent, new PtrEqual());
            resources.SetRenderingPath();
            var allEvents = resources.allEvents;

            GraphicsUtility.UpdatePlatform();
            MLight.ClearLightDict();
            this.resources = resources;
            current        = this;
            data.buffer    = new CommandBuffer();
            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                resources.availiableEvents[i].InitDependEventsList();
            }
            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                eventsGuideBook.Add(new UIntPtr(MUnsafeUtility.GetManagedPtr(resources.availiableEvents[i].GetType())), i);
                resources.availiableEvents[i].Prepare();
            }
            for (int i = 0; i < resources.availiableEvents.Length; ++i)
            {
                resources.availiableEvents[i].InitEvent(resources);
            }
            waitReleaseRT = new NativeList <int>(20, Allocator.Persistent);
        }
        public static ComputeBuffer GetTempPropertyBuffer(int length, int stride, ComputeBufferType type = ComputeBufferType.Default)
        {
            if (!allTempBuffers.isCreated)
            {
                allTempBuffers = new NativeDictionary <BufferKey, int, BufferKey.Equal>(11, Allocator.Persistent, new BufferKey.Equal());
            }
            ComputeBuffer target;
            int           targetIndex;

            if (allTempBuffers.Get(new BufferKey {
                size = stride, type = type
            }, out targetIndex))
            {
                target = MUnsafeUtility.GetHookedObject(targetIndex) as ComputeBuffer;
                if (target.count < length)
                {
                    target.Dispose();
                    target = new ComputeBuffer(length, stride, type);
                    MUnsafeUtility.SetHookedObject(targetIndex, target);
                }
                return(target);
            }
            else
            {
                target            = new ComputeBuffer(length, stride);
                allTempBuffers[new BufferKey {
                                   size = stride, type = type
                               }] = MUnsafeUtility.HookObject(target);
                return(target);
            }
        }
示例#3
0
 public static void InitializeDict()
 {
     if (!cameraSearchDict.isCreated)
     {
         cameraSearchDict = new NativeDictionary <int, ulong, IntEqual>(20, Allocator.Persistent, new IntEqual());
     }
 }
示例#4
0
 /// <summary>
 /// Init Virtual Texture
 /// </summary>
 /// <param name="perTextureSize">Virtual texture's basic size</param>
 /// <param name="maximumSize">Virtual texture's array size</param>
 /// <param name="indexSize">Index Texture's size</param>
 /// <param name="formats">Each VT's format</param>
 public VirtualTexture(int maximumSize, int2 indexSize, VirtualTextureFormat *formats, int formatLen, string indexTexName)
 {
     if (maximumSize > 2048)
     {
         throw new System.Exception("Virtual Texture Maximum Size can not larger than 2048");
     }
     indexTexID     = Shader.PropertyToID(indexTexName);
     this.indexSize = indexSize;
     setIndexBuffer = new ComputeBuffer(START_CHUNKSIZE * START_CHUNKSIZE, sizeof(uint));
     allFormats     = new NativeArray <VirtualTextureFormat>(formatLen, Allocator.Persistent);
     poolDict       = new NativeDictionary <int2, int2, VTChunkHandleEqual>(maximumSize, Allocator.Persistent, new VTChunkHandleEqual());
     UnsafeUtility.MemCpy(allFormats.GetUnsafePtr(), formats, sizeof(VirtualTextureFormat) * formatLen);
     shader   = Resources.Load <ComputeShader>("VirtualTexture");
     pool     = new TexturePool(maximumSize);
     indexTex = new RenderTexture(indexSize.x, indexSize.y, 0, GraphicsFormat.R16G16B16A16_UNorm, 0);
     indexTex.enableRandomWrite = true;
     indexTex.useMipMap         = false;
     indexTex.filterMode        = FilterMode.Point;
     indexTex.Create();
     textures = new RenderTexture[formatLen];
     for (int i = 0; i < formatLen; ++i)
     {
         ref VirtualTextureFormat format = ref formats[i];
         textures[i] = new RenderTexture((int)format.perElementSize, (int)format.perElementSize, 0, format.format, format.mipCount);
         textures[i].autoGenerateMips  = false;
         textures[i].useMipMap         = format.mipCount > 0;
         textures[i].enableRandomWrite = true;
         textures[i].volumeDepth       = maximumSize;
         textures[i].dimension         = TextureDimension.Tex2DArray;
         textures[i].antiAliasing      = 1;
         textures[i].Create();
     }
示例#5
0
 public static void AddRunnableObject(int id, IPipelineRunnable func)
 {
     if (!iRunnableObjects.isCreated)
     {
         iRunnableObjects = new NativeDictionary <int, ulong, IntEqual>(10, Allocator.Persistent, new IntEqual());
     }
     iRunnableObjects.Add(id, (ulong)MUnsafeUtility.GetManagedPtr(func));
 }
 private void OnEnable()
 {
     if (!allCamera.isCreated)
     {
         allCamera = new NativeDictionary <int, UIntPtr>(17, Unity.Collections.Allocator.Persistent, (i, j) => i == j);
     }
     allCamera.Add(gameObject.GetInstanceID(), new UIntPtr(MUnsafeUtility.GetManagedPtr(this)));
 }
示例#7
0
 private void OnEnable()
 {
     if (!allDatas.isCreated)
     {
         allDatas = new NativeDictionary <ulong, int, PtrEqual>(17, Allocator.Persistent, new PtrEqual());
     }
     AddToDict();
     GetComponent <Camera>().layerCullDistances = layerCullDistance;
 }
 private void OnEnable()
 {
     if (!allCamera.isCreated)
     {
         allCamera = new NativeDictionary <int, UIntPtr, IntEqual>(17, Allocator.Persistent, new IntEqual());
     }
     allCamera.Add(gameObject.GetInstanceID(), new UIntPtr(MUnsafeUtility.GetManagedPtr(this)));
     GetComponent <Camera>().layerCullDistances = layerCullDistance;
 }
示例#9
0
        public override void FrameUpdate(PipelineCamera cam, ref PipelineCommandData data)
        {
            if (!enabledPost || !cam.postProfile)
            {
                data.buffer.Blit(cam.targets.renderTargetIdentifier, cam.cameraTarget);
                return;
            }
#if UNITY_EDITOR
            if (!enableInEditor && RenderPipeline.renderingEditor)
            {
                data.buffer.Blit(cam.targets.renderTargetIdentifier, cam.cameraTarget);
                return;
            }
#endif
            NativeDictionary <ulong, ulong, PtrEqual> allSettings = new NativeDictionary <ulong, ulong, PtrEqual>(allPostEffects.Count, Unity.Collections.Allocator.Temp, new PtrEqual());
            foreach (var i in cam.postProfile.settings)
            {
                allSettings.Add((ulong)MUnsafeUtility.GetManagedPtr(i.GetType()), (ulong)MUnsafeUtility.GetManagedPtr(i));
            }
            postContext.camera              = cam.cam;
            postContext.command             = data.buffer;
            postContext.sourceFormat        = RenderTextureFormat.ARGBHalf;
            postContext.autoExposureTexture = RuntimeUtilities.whiteTexture;
            postContext.bloomBufferNameID   = -1;
            RenderTargetIdentifier source, dest;
            postContext.source      = cam.targets.renderTargetIdentifier;
            postContext.destination = cam.targets.backupIdentifier;
            postContext.logHistogram.Generate(postContext);
            cyberGlitch.Render(data.buffer, ref cam.targets);
            foreach (var i in allPostEffects)
            {
                ulong settingsPtr;
                if (allSettings.Get((ulong)MUnsafeUtility.GetManagedPtr(i.type), out settingsPtr))
                {
                    PostProcessEffectSettings setting = MUnsafeUtility.GetObject <PostProcessEffectSettings>((void *)settingsPtr);
                    if (i.needBlit && setting.active)
                    {
                        PipelineFunctions.RunPostProcess(ref cam.targets, out source, out dest);
                        postContext.source      = source;
                        postContext.destination = dest;
                    }
                    i.renderer.SetSettings(setting);
                    i.renderer.Render(postContext);
                }
            }
            ;
            allSettings.Dispose();
            //   data.buffer.Blit(ShaderIDs._CameraMotionVectorsTexture, cam.cameraTarget);
            //     data.buffer.BlitSRT(cam.cameraTarget, debugMat, 0);
            data.buffer.BlitSRT(cam.targets.renderTargetIdentifier, cam.cameraTarget, postContext.uberSheet.material, 0, postContext.uberSheet.properties);
            if (postContext.bloomBufferNameID > -1)
            {
                data.buffer.ReleaseTemporaryRT(postContext.bloomBufferNameID);
            }
        }
示例#10
0
 public void AddToDict()
 {
     if (!cameraSearchDict.isCreated)
     {
         cameraSearchDict = new NativeDictionary <int, ulong, IntEqual>(20, Allocator.Persistent, new IntEqual());
     }
     cameraSearchDict[gameObject.GetInstanceID()] = (ulong)MUnsafeUtility.GetManagedPtr(this);
     if (!frustumArray.isCreated)
     {
         frustumArray = new NativeList <float4>(6, 6, Allocator.Persistent);
     }
 }
        public void Init(PipelineResources res)
        {
            current            = this;
            referenceCacheDict = new NativeDictionary <int4x4, int, Int4x4Equal>(200, Allocator.Persistent, new Int4x4Equal());
            mipIDs             = new NativeArray <int>(2, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            mipIDs[0]          = Shader.PropertyToID("_Mip0");
            mipIDs[1]          = Shader.PropertyToID("_Mip1");

            /*mipIDs[2] = Shader.PropertyToID("_Mip2");
            *  mipIDs[3] = Shader.PropertyToID("_Mip3");
            *  mipIDs[4] = Shader.PropertyToID("_Mip4");
            *  mipIDs[5] = Shader.PropertyToID("_Mip5");*/
            rgbaPool.Init(0, GraphicsFormat.R8G8B8A8_UNorm, (int)fixedTextureSize, this);
            emissionPool.Init(2, GraphicsFormat.R16G16B16A16_SFloat, (int)fixedTextureSize, this);
            heightPool.Init(3, GraphicsFormat.R8_UNorm, (int)fixedTextureSize, this);
            vmManager = new VirtualMaterialManager(materialPoolSize, maximumMaterialCount, res.shaders.streamingShader);
            SceneStreaming.loading = false;
        }
        public AssetReference GetReference(ref int4x4 guid)
        {
            if (!referenceCacheDict.isCreated)
            {
                referenceCacheDict = new NativeDictionary <int4x4, int, Int4x4Equal>(100, Allocator.Persistent, new Int4x4Equal());
            }
            int index;

            if (referenceCacheDict.Get(guid, out index))
            {
                return(allReferenceCache[index]);
            }
            string guidCache = new string((char *)guid.Ptr(), 0, 32);

            referenceCacheDict.Add(guid, allReferenceCache.Count);
            AssetReference aref = new AssetReference(guidCache);

            allReferenceCache.Add(aref);
            return(aref);
        }
        public void Init(PipelineResources res)
        {
            referenceCacheDict = new NativeDictionary <int4x4, int, Int4x4Equal>(200, Allocator.Persistent, new Int4x4Equal());
            mipIDs             = new NativeArray <int>(2, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            mipIDs[0]          = Shader.PropertyToID("_Mip0");
            mipIDs[1]          = Shader.PropertyToID("_Mip1");

            /*mipIDs[2] = Shader.PropertyToID("_Mip2");
            *  mipIDs[3] = Shader.PropertyToID("_Mip3");
            *  mipIDs[4] = Shader.PropertyToID("_Mip4");
            *  mipIDs[5] = Shader.PropertyToID("_Mip5");*/
            msbForCluster = new MStringBuilder(100);
            for (int i = 0; i < clusterProperties.Count; ++i)
            {
                var cur = clusterProperties[i];
                cur.Init(msbForCluster, this);
            }

            rgbaPool.Init(0, GraphicsFormat.R8G8B8A8_UNorm, (int)fixedTextureSize, this);
            emissionPool.Init(2, GraphicsFormat.R16G16B16A16_SFloat, (int)fixedTextureSize, this);
            heightPool.Init(3, GraphicsFormat.R8_UNorm, (int)fixedTextureSize, this);
            vmManager = new VirtualMaterialManager(materialPoolSize, maximumMaterialCount, res.shaders.streamingShader);
        }
示例#14
0
        protected override void OnEnableFunc()
        {
            if (current && current != this)
            {
                enabled = false;
                Debug.LogError("Only One Terrain allowed!");
                return;
            }
            if (!terrainData)
            {
                enabled = false;
                Debug.LogError("No Data!");
                return;
            }
            if (!cam || !decalCamera)
            {
                enabled = false;
                Debug.LogError("No Decal Camera!");
                return;
            }
            initializing      = true;
            lodOffset         = terrainData.GetLodOffset();
            largestChunkCount = (int)(0.1 + pow(2.0, lodOffset));
            msb = new MStringBuilder(32);
            oneVTPixelWorldLength = terrainData.VTTexelLength();
            textureShader         = Resources.Load <ComputeShader>("ProceduralTexture");
            shader  = Resources.Load <ComputeShader>("TerrainCompute");
            current = this;
            int indexMapSize = 1;

            for (int i = 1; i < terrainData.lodDistances.Length; ++i)
            {
                indexMapSize *= 2;
            }
            vtContainer  = new NativeArray <int>(4, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            maskLoadList = new NativeQueue <MaskLoadCommand>(10, Allocator.Persistent);
            ComputeShader editShader = Resources.Load <ComputeShader>("TerrainEdit");

            loadingThread = new MTerrainLoadingThread(10);
            maskLoader    = new VirtualTextureLoader(terrainData.maskmapPath, editShader, largestChunkCount, MASK_RESOLUTION, false, loadingThread);
            heightLoader  = new VirtualTextureLoader(terrainData.heightmapPath, editShader, largestChunkCount, MASK_RESOLUTION, true, loadingThread);

            loadDataList       = new NativeQueue <TerrainLoadData>(100, Allocator.Persistent);
            initializeLoadList = new NativeQueue <TerrainLoadData>(100, Allocator.Persistent);
            NativeArray <uint> dispatchDraw = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory);

            dispatchDraw[0] = 6;
            VirtualTextureFormat *formats = stackalloc VirtualTextureFormat[]
            {
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, GraphicsFormat.R8G8B8A8_UNorm, "_VirtualMainTex", 2),
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, GraphicsFormat.R16G16_SNorm, "_VirtualBumpMap", 2),
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, GraphicsFormat.R8G8_UNorm, "_VirtualSMMap", 2),
                new VirtualTextureFormat((VirtualTextureSize)HEIGHT_RESOLUTION, HEIGHT_FORMAT, "_VirtualDisplacement", 0)
            };

            mipIDs          = new NativeArray <int>(2, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            mipIDs[0]       = Shader.PropertyToID("_Mip0");
            mipIDs[1]       = Shader.PropertyToID("_Mip1");
            chunkCount      = (int)(pow(2.0, terrainData.lodDistances.Length - 1) + 0.1);
            vt              = new VirtualTexture(terrainData.virtualTexCapacity, min(2048, chunkCount), formats, 4, "_TerrainVTIndexTex");
            textureCapacity = terrainData.virtualTexCapacity;
            VirtualTextureFormat *maskFormats = stackalloc VirtualTextureFormat[]
            {
                new VirtualTextureFormat((VirtualTextureSize)MASK_RESOLUTION, GraphicsFormat.R8_UNorm, "_VirtualMaskmap"),
                new VirtualTextureFormat((VirtualTextureSize)MASK_RESOLUTION, GraphicsFormat.R16_UNorm, "_VirtualHeightmap"),
            };

            maskVT = new VirtualTexture(terrainData.heightmapTexCapacity, largestChunkCount, maskFormats, 2, "_MaskIndexMap");
            maskVT.GetTexture(0).filterMode = FilterMode.Point;
            maskVT.GetTexture(1).filterMode = FilterMode.Point;
            vt.GetTexture(0).filterMode     = FilterMode.Trilinear;
            vt.GetTexture(1).filterMode     = FilterMode.Trilinear;
            vt.GetTexture(2).filterMode     = FilterMode.Trilinear;
            vt.GetTexture(3).filterMode     = FilterMode.Bilinear;
            allLodLevles = new NativeList_Float(terrainData.lodDistances.Length, Allocator.Persistent);
            for (int i = 0; i < terrainData.lodDistances.Length; ++i)
            {
                allLodLevles.Add((float)min(terrainData.lodDistances[max(0, i - 1)], terrainData.lodDistances[i]));
            }
            allLodLevles[terrainData.lodDistances.Length] = 0;
            meshResolution = terrainData.GetMeshResolution();
            cullingFlags   = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R8_UNorm,
                dimension         = TextureDimension.Tex2D,
                width             = meshResolution,
                height            = meshResolution,
                volumeDepth       = 1,
                enableRandomWrite = true,
                msaaSamples       = 1,
                useMipMap         = false
            });
            cullingFlags.filterMode = FilterMode.Point;
            cullingFlags.Create();
            albedoTex = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R8G8B8A8_UNorm,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1,
                autoGenerateMips  = false,
                useMipMap         = true,
                mipCount          = 6,
            });
            albedoTex.Create();
            normalTex = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R16G16_SNorm,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                autoGenerateMips  = false,
                useMipMap         = true,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1,
                mipCount          = 6,
            });
            normalTex.Create();
            smTex = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R16G16_UNorm,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1,
                useMipMap         = true,
                autoGenerateMips  = false,
                mipCount          = 6,
                depthBufferBits   = 0,
                useDynamicScale   = false
            });
            smTex.Create();

            /*   heightTex = new RenderTexture(new RenderTextureDescriptor
             * {
             *     graphicsFormat = GraphicsFormat.R8_UNorm,
             *     dimension = TextureDimension.Tex2DArray,
             *     width = COLOR_RESOLUTION,
             *     height = COLOR_RESOLUTION,
             *     volumeDepth = Mathf.Max(1, terrainData.textures.Length),
             *     enableRandomWrite = true,
             *     msaaSamples = 1,
             *     useMipMap = true,
             *     autoGenerateMips = false,
             *     mipCount = 6,
             *     depthBufferBits = 0,
             *     useDynamicScale = false
             * });*/
            heightloadingCacheRT = new RenderTexture(new RenderTextureDescriptor
            {
                width             = COLOR_RESOLUTION + 2,
                height            = COLOR_RESOLUTION + 2,
                volumeDepth       = 1,
                enableRandomWrite = true,
                dimension         = TextureDimension.Tex2D,
                graphicsFormat    = GraphicsFormat.R16_UNorm,
                msaaSamples       = 1,
                useMipMap         = false,
                autoGenerateMips  = false,
                mipCount          = 0,
                depthBufferBits   = 0,
                useDynamicScale   = false,
            });
            randomTileRT = new RenderTexture(256, 256, 0, GraphicsFormat.R16G16B16A16_SNorm, 0);
            randomTileRT.enableRandomWrite = true;
            randomTileRT.wrapMode          = TextureWrapMode.Repeat;
            randomTileRT.filterMode        = FilterMode.Point;
            randomTileRT.Create();

            heightloadingCacheRT.Create();
            //    heightTex.Create();
            smTex.wrapMode     = TextureWrapMode.Repeat;
            normalTex.wrapMode = TextureWrapMode.Repeat;
            albedoTex.wrapMode = TextureWrapMode.Repeat;
            //   heightTex.wrapMode = TextureWrapMode.Repeat;
            boundBoxLoadList   = new NativeQueue <MaskLoadCommand>(10, Allocator.Persistent);
            boundingLoadStream = new FileStream(terrainData.boundPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            boundingDict       = new NativeDictionary <int2, MTerrainBoundingTree, Int2Equal>(20, Allocator.Persistent, new Int2Equal());
            InitializeMeshData();
            allDrawCommand = new NativeList <TerrainDrawCommand>(20, Allocator.Persistent);
            materialBuffer = new ComputeBuffer(max(1, terrainData.allMaterials.Length), sizeof(MTerrainData.HeightBlendMaterial));
            materialBuffer.SetData(terrainData.allMaterials);
            decalLayerOffset = max(0, terrainData.lodDistances.Length - terrainData.allDecalLayers.Length);
            tree             = new TerrainQuadTree(-1, TerrainQuadTree.LocalPos.LeftDown, 0, 0, terrainData.largestChunkSize, double3(1, 0, 0), 0);

            StartCoroutine(AsyncLoader());
        }