//TODO: Streaming Loading /// <summary> /// Initialize pipeline buffers /// </summary> /// <param name="baseBuffer"></param> pipeline base buffer public static void InitBaseBuffer(PipelineBaseBuffer baseBuffer, ClusterMatResources materialResources, string name, int maximumLength) { baseBuffer.clusterBuffer = new ComputeBuffer(maximumLength, sizeof(CullBox)); baseBuffer.resultBuffer = new ComputeBuffer(maximumLength, PipelineBaseBuffer.UINTSIZE); baseBuffer.instanceCountBuffer = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); NativeArray <uint> instanceCountBufferValue = new NativeArray <uint>(5, Allocator.Temp); instanceCountBufferValue[0] = PipelineBaseBuffer.CLUSTERVERTEXCOUNT; baseBuffer.instanceCountBuffer.SetData(instanceCountBufferValue); instanceCountBufferValue.Dispose(); baseBuffer.verticesBuffer = new ComputeBuffer(maximumLength * PipelineBaseBuffer.CLUSTERCLIPCOUNT, sizeof(Point)); baseBuffer.clusterCount = 0; }
public static void Initialize(ref PipelineBaseBuffer baseBuffer, RenderObject[] allObjects, int maximumInstancing) { baseBuffer.Transforms = new ComputeBuffer(maximumInstancing, MATRIXSIZE); baseBuffer.instanceCountBuffer = new ComputeBuffer(allObjects.Length, 4); baseBuffer.allCountBuffer = new ComputeBuffer(1, 4); List <Point> allPoints = new List <Point>(); List <Vector3> currentPoints = new List <Vector3>(); List <Vector4> currentTangents = new List <Vector4>(); List <Vector3> currentNormal = new List <Vector3>(); List <Vector2> currentTexcoord = new List <Vector2>(); instanceCountArray = new int[allObjects.Length]; for (int i = 0; i < allObjects.Length; ++i) { Transform[] trs = allObjects[i].objPositions; allObjects[i].objBuffer = new ComputeBuffer(trs.Length, MATRIXSIZE); NativeArray <Matrix4x4> matrices = new NativeArray <Matrix4x4>(trs.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); for (int a = 0; a < trs.Length; ++a) { matrices[a] = trs[a].localToWorldMatrix; } allObjects[i].objBuffer.SetData(matrices); allObjects[i].extent = allObjects[i].staticMesh.bounds.extents; matrices.Dispose(); Mesh mesh = allObjects[i].staticMesh; int[] triangles = mesh.triangles; mesh.GetVertices(currentPoints); mesh.GetUVs(0, currentTexcoord); mesh.GetTangents(currentTangents); mesh.GetNormals(currentNormal); for (int j = 0; j < triangles.Length; ++j) { Point p; int index = triangles[j]; p.vertex = currentPoints[index]; p.normal = currentNormal[index]; p.texcoord = currentTexcoord[index]; p.tangent = currentTangents[index]; allPoints.Add(p); } allObjects[i].vertexCount = triangles.Length; } for (int i = 1; i < allObjects.Length; ++i) { allObjects[i].vertexOffset = allObjects[i - 1].vertexOffset + allObjects[i - 1].vertexCount; } baseBuffer.vertexBuffer = new ComputeBuffer(allPoints.Count, Point.SIZE); baseBuffer.vertexBuffer.SetData(allPoints); }
public static void SetBaseBuffer(ref PipelineBaseBuffer baseBuffer, ComputeShader computeShader, Vector4[] cullingPlanes) { NativeArray <int> instanceArray = new NativeArray <int>(baseBuffer.instanceCountBuffer.count, Allocator.Temp, NativeArrayOptions.ClearMemory); baseBuffer.instanceCountBuffer.SetData(instanceArray); instanceArray.Dispose(); instanceArray = new NativeArray <int>(1, Allocator.Temp, NativeArrayOptions.ClearMemory); baseBuffer.allCountBuffer.SetData(instanceArray); instanceArray.Dispose(); computeShader.SetBuffer(0, ShaderIDs.instanceCountBuffer, baseBuffer.instanceCountBuffer); computeShader.SetBuffer(0, ShaderIDs.allCountBuffer, baseBuffer.allCountBuffer); computeShader.SetBuffer(0, ShaderIDs.Transforms, baseBuffer.Transforms); computeShader.SetVectorArray(ShaderIDs.planes, cullingPlanes); }
//TODO: Streaming Loading /// <summary> /// Initialize pipeline buffers /// </summary> /// <param name="baseBuffer"></param> pipeline base buffer public static void InitBaseBuffer(ref PipelineBaseBuffer baseBuffer, ClusterMatResources materialResources, string name, int maximumLength) { /* * baseBuffer.propertyBuffer = new ComputeBuffer(materialResources.values.Length, sizeof(PropertyValue)); * baseBuffer.propertyBuffer.SetData(materialResources.values); * baseBuffer.combinedMaterial = new Material(Shader.Find("Maxwell/CombinedProcedural")); * baseBuffer.combinedMaterial.SetBuffer("_PropertiesBuffer", baseBuffer.propertyBuffer); * foreach (var i in materialResources.textures) * { * baseBuffer.combinedMaterial.SetTexture(i.key, i.value); * }*/ baseBuffer.clusterBuffer = new ComputeBuffer(maximumLength, sizeof(ClusterMeshData)); baseBuffer.resultBuffer = new ComputeBuffer(maximumLength, PipelineBaseBuffer.UINTSIZE); baseBuffer.instanceCountBuffer = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); NativeArray <uint> instanceCountBufferValue = new NativeArray <uint>(5, Allocator.Temp); instanceCountBufferValue[0] = PipelineBaseBuffer.CLUSTERVERTEXCOUNT; baseBuffer.instanceCountBuffer.SetData(instanceCountBufferValue); instanceCountBufferValue.Dispose(); baseBuffer.verticesBuffer = new ComputeBuffer(maximumLength * PipelineBaseBuffer.CLUSTERCLIPCOUNT, sizeof(Point)); baseBuffer.clusterCount = 0; baseBuffer.dispatchBuffer = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); NativeArray <uint> occludedCountList = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory); occludedCountList[0] = 0; occludedCountList[1] = 1; occludedCountList[2] = 1; occludedCountList[3] = 0; occludedCountList[4] = 0; baseBuffer.dispatchBuffer.SetData(occludedCountList); baseBuffer.reCheckCount = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); baseBuffer.reCheckResult = new ComputeBuffer(maximumLength, 4); occludedCountList[0] = PipelineBaseBuffer.CLUSTERVERTEXCOUNT; occludedCountList[1] = 0; occludedCountList[2] = 0; baseBuffer.reCheckCount.SetData(occludedCountList); occludedCountList.Dispose(); }
public static void RenderShadowProcedural(ref ShadowMapComponent shadMap, ref PipelineBaseBuffer baseBuffer, int pass) { shadMap.shadowDepthMaterial.SetPass(pass); Graphics.DrawProceduralIndirect(MeshTopology.Triangles, baseBuffer.instanceCountBuffer); }
public static void RenderProceduralCommand(ref PipelineBaseBuffer buffer, Material material) { buffer.geometryCommand.DrawProceduralIndirect(Matrix4x4.identity, material, 0, MeshTopology.Triangles, buffer.instanceCountBuffer); }
public static void RunCullDispatching(ref PipelineBaseBuffer baseBuffer, ComputeShader computeShader) { computeShader.Dispatch(1, 1, 1, 1); ComputeShaderUtility.Dispatch(computeShader, 0, baseBuffer.clusterCount, 64); }
/// <summary> /// Initialize pipeline buffers /// </summary> /// <param name="baseBuffer"></param> pipeline base buffer public static void InitBaseBuffer(ref PipelineBaseBuffer baseBuffer) { TextAsset[] allFileFlags = Resources.LoadAll <TextAsset>("MapSigns"); int clusterCount = 0; foreach (var i in allFileFlags) { clusterCount += int.Parse(i.text); } StringBuilder sb = new StringBuilder(50, 150); NativeArray <ClusterMeshData> allInfos = new NativeArray <ClusterMeshData>(clusterCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory); NativeArray <Point> allPoints = new NativeArray <Point>(clusterCount * PipelineBaseBuffer.CLUSTERCLIPCOUNT, Allocator.Temp, NativeArrayOptions.UninitializedMemory); clusterCount = 0; int pointCount = 0; foreach (var i in allFileFlags) { sb.Clear(); sb.Append("MapInfos/"); sb.Append(i.name); TextAsset clusterFile = Resources.Load <TextAsset>(sb.ToString()); sb.Clear(); sb.Append("MapPoints/"); sb.Append(i.name); TextAsset pointFile = Resources.Load <TextAsset>(sb.ToString()); byte[] clusterArray = clusterFile.bytes; byte[] pointArray = pointFile.bytes; fixed(void *source = &clusterArray[0]) { byte *dest = (byte *)allInfos.GetUnsafePtr() + clusterCount; UnsafeUtility.MemCpy(dest, source, clusterArray.Length); } clusterCount += clusterArray.Length; fixed(void *source = &pointArray[0]) { byte *dest = (byte *)allPoints.GetUnsafePtr() + pointCount; UnsafeUtility.MemCpy(dest, source, pointArray.Length); } pointCount += pointArray.Length; Resources.UnloadAsset(clusterFile); Resources.UnloadAsset(pointFile); } TextAsset propFile = Resources.Load <TextAsset>("MapMat/CombinedMatProps"); /*PropertyValue[] values = MStudio.Json.StringToJson<PropertyValue[]>(propFile.text); * baseBuffer.propertyBuffer = new ComputeBuffer(values.Length, sizeof(PropertyValue)); * baseBuffer.propertyBuffer.SetData(values); * baseBuffer.combinedMaterial = new Material(Shader.Find("Maxwell/CombinedProcedural")); * baseBuffer.combinedMaterial.SetBuffer("_PropertiesBuffer", baseBuffer.propertyBuffer);*/ //Set Tex2DArray Texture2DArray mainTex = Resources.Load <Texture2DArray>("MapMat/_MainTex"); Texture2DArray bumpTex = Resources.Load <Texture2DArray>("MapMat/_BumpMap"); Texture2DArray specularMap = Resources.Load <Texture2DArray>("MapMat/_SpecularMap"); Texture2DArray occlusionMap = Resources.Load <Texture2DArray>("MapMat/_OcclusionMap"); /* baseBuffer.combinedMaterial.SetTexture(ShaderIDs._MainTex, mainTex); * baseBuffer.combinedMaterial.SetTexture("_BumpMap", bumpTex); * baseBuffer.combinedMaterial.SetTexture("_SpecularMap", specularMap); * baseBuffer.combinedMaterial.SetTexture("_OcclusionMap", occlusionMap);*/ //Over Resources.UnloadAsset(propFile); baseBuffer.clusterBuffer = new ComputeBuffer(allInfos.Length, sizeof(ClusterMeshData)); baseBuffer.clusterBuffer.SetData(allInfos); baseBuffer.resultBuffer = new ComputeBuffer(allInfos.Length, PipelineBaseBuffer.UINTSIZE); baseBuffer.instanceCountBuffer = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); NativeArray <uint> instanceCountBufferValue = new NativeArray <uint>(5, Allocator.Temp); instanceCountBufferValue[0] = PipelineBaseBuffer.CLUSTERVERTEXCOUNT; baseBuffer.instanceCountBuffer.SetData(instanceCountBufferValue); instanceCountBufferValue.Dispose(); baseBuffer.verticesBuffer = new ComputeBuffer(allPoints.Length, sizeof(Point)); baseBuffer.verticesBuffer.SetData(allPoints); baseBuffer.clusterCount = allInfos.Length; allInfos.Dispose(); allPoints.Dispose(); baseBuffer.dispatchBuffer = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); NativeArray <uint> occludedCountList = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory); occludedCountList[0] = 0; occludedCountList[1] = 1; occludedCountList[2] = 1; occludedCountList[3] = 0; occludedCountList[4] = 0; baseBuffer.dispatchBuffer.SetData(occludedCountList); baseBuffer.reCheckCount = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); baseBuffer.reCheckResult = new ComputeBuffer(baseBuffer.clusterCount, 4); occludedCountList[0] = PipelineBaseBuffer.CLUSTERVERTEXCOUNT; occludedCountList[1] = 0; occludedCountList[2] = 0; baseBuffer.reCheckCount.SetData(occludedCountList); occludedCountList.Dispose(); foreach (var i in allFileFlags) { Resources.UnloadAsset(i); } }
public static void SetShaderBuffer(CommandBuffer cb, ref PipelineBaseBuffer basebuffer) { cb.SetGlobalBuffer(ShaderIDs.VertexBuffer, basebuffer.vertexBuffer); cb.SetGlobalBuffer(ShaderIDs.Transforms, basebuffer.Transforms); }
/// <summary> /// Initialize pipeline buffers /// </summary> /// <param name="baseBuffer"></param> pipeline base buffer public static void InitBaseBuffer(ref PipelineBaseBuffer baseBuffer) { TextAsset[] allFileFlags = Resources.LoadAll <TextAsset>("MapSigns"); int clusterCount = 0; foreach (var i in allFileFlags) { clusterCount += int.Parse(i.text); } StringBuilder sb = new StringBuilder(50, 150); NativeArray <ClusterMeshData> allInfos = new NativeArray <ClusterMeshData>(clusterCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory); NativeArray <Point> allPoints = new NativeArray <Point>(clusterCount * PipelineBaseBuffer.CLUSTERCLIPCOUNT, Allocator.Temp, NativeArrayOptions.UninitializedMemory); clusterCount = 0; int pointCount = 0; foreach (var i in allFileFlags) { sb.Clear(); sb.Append("MapInfos/"); sb.Append(i.name); TextAsset clusterFile = Resources.Load <TextAsset>(sb.ToString()); sb.Clear(); sb.Append("MapPoints/"); sb.Append(i.name); TextAsset pointFile = Resources.Load <TextAsset>(sb.ToString()); byte[] clusterArray = clusterFile.bytes; byte[] pointArray = pointFile.bytes; fixed(void *source = &clusterArray[0]) { byte *dest = (byte *)allInfos.GetUnsafePtr() + clusterCount; UnsafeUtility.MemCpy(dest, source, clusterArray.Length); } clusterCount += clusterArray.Length; fixed(void *source = &pointArray[0]) { byte *dest = (byte *)allPoints.GetUnsafePtr() + pointCount; UnsafeUtility.MemCpy(dest, source, pointArray.Length); } pointCount += pointArray.Length; } baseBuffer.clusterBuffer = new ComputeBuffer(allInfos.Length, sizeof(ClusterMeshData)); baseBuffer.clusterBuffer.SetData(allInfos); baseBuffer.resultBuffer = new ComputeBuffer(allInfos.Length, PipelineBaseBuffer.UINTSIZE); baseBuffer.instanceCountBuffer = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); NativeArray <uint> instanceCountBufferValue = new NativeArray <uint>(5, Allocator.Temp); instanceCountBufferValue[0] = PipelineBaseBuffer.CLUSTERVERTEXCOUNT; baseBuffer.instanceCountBuffer.SetData(instanceCountBufferValue); instanceCountBufferValue.Dispose(); baseBuffer.verticesBuffer = new ComputeBuffer(allPoints.Length, sizeof(Point)); baseBuffer.verticesBuffer.SetData(allPoints); baseBuffer.clusterCount = allInfos.Length; allInfos.Dispose(); allPoints.Dispose(); baseBuffer.dispatchBuffer = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); NativeArray <uint> occludedCountList = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory); occludedCountList[0] = 0; occludedCountList[1] = 1; occludedCountList[2] = 1; occludedCountList[3] = 0; occludedCountList[4] = 0; baseBuffer.dispatchBuffer.SetData(occludedCountList); baseBuffer.reCheckCount = new ComputeBuffer(5, 4, ComputeBufferType.IndirectArguments); baseBuffer.reCheckResult = new ComputeBuffer(baseBuffer.clusterCount, 4); occludedCountList[0] = PipelineBaseBuffer.CLUSTERVERTEXCOUNT; occludedCountList[1] = 0; occludedCountList[2] = 0; baseBuffer.reCheckCount.SetData(occludedCountList); occludedCountList.Dispose(); }