private void CoeffToRenderTexture(CommandBuffer buffer) { ComputeShader shader = pipelineRes.shaders.probeCoeffShader; buffer.SetComputeBufferParam(shader, coeffToTex3DKernel, ShaderIDs._Coeff, coeff); buffer.SetComputeTextureParam(shader, coeffToTex3DKernel, _CoeffIDs[0], currentTexture.coeff0); buffer.SetComputeTextureParam(shader, coeffToTex3DKernel, _CoeffIDs[1], currentTexture.coeff1); buffer.SetComputeTextureParam(shader, coeffToTex3DKernel, _CoeffIDs[2], currentTexture.coeff2); buffer.SetComputeTextureParam(shader, coeffToTex3DKernel, _CoeffIDs[3], currentTexture.coeff3); buffer.SetComputeTextureParam(shader, coeffToTex3DKernel, _CoeffIDs[4], currentTexture.coeff4); buffer.SetComputeTextureParam(shader, coeffToTex3DKernel, _CoeffIDs[5], currentTexture.coeff5); buffer.SetComputeTextureParam(shader, coeffToTex3DKernel, _CoeffIDs[6], currentTexture.coeff6); buffer.SetComputeVectorParam(shader, ShaderIDs._Tex3DSize, new float4(currentIrr.resolution.x, currentIrr.resolution.y, currentIrr.resolution.z, 1)); ComputeShaderUtility.Dispatch(shader, buffer, coeffToTex3DKernel, len / 9, 64); }
public static void MoveAllScenes(float3 delta, int offset, int clusterCount) { if (clusterCount <= 0) { return; } ComputeShader shad = resources.shaders.streamingShader; CommandBuffer cb = RenderPipeline.BeforeFrameBuffer; cb.SetComputeIntParam(shad, ShaderIDs._Offset, offset); cb.SetComputeBufferParam(shad, EXECUTE_CLUSTER_KERNEL_MOVE_ALL, ShaderIDs.clusterBuffer, baseBuffer.clusterBuffer); cb.SetComputeBufferParam(shad, EXECUTE_POINT_KERNEL_MOVE_ALL, ShaderIDs.verticesBuffer, baseBuffer.verticesBuffer); cb.SetComputeVectorParam(shad, ShaderIDs._OffsetDirection, float4(delta, 1)); ComputeShaderUtility.Dispatch(shad, cb, EXECUTE_CLUSTER_KERNEL_MOVE_ALL, clusterCount); cb.DispatchCompute(shad, EXECUTE_POINT_KERNEL_MOVE_ALL, clusterCount, 1, 1); }
public override void FinishJob() { CommandBuffer buffer = RenderPipeline.BeforeFrameBuffer; ComputeBuffer temp = currentPosBuffer; currentPosBuffer = lastPosBuffer; lastPosBuffer = temp; buffer.SetComputeBufferParam(movingShader, 0, ShaderIDs._TransformMatrices, currentPosBuffer); buffer.SetComputeBufferParam(movingShader, 0, ShaderIDs._OriginTransformMatrices, originPosBuffer); buffer.SetComputeTextureParam(movingShader, 0, ShaderIDs._NoiseTexture, noiseTexture); buffer.SetComputeVectorParam(movingShader, ShaderIDs._OffsetDirection, transform.forward); offset += Time.deltaTime * runSpeed; offset = frac(offset); buffer.SetComputeFloatParam(movingShader, ShaderIDs._Offset, offset * noiseTexture.height); buffer.SetComputeVectorParam(movingShader, ShaderIDs._NoiseTexture_Size, new Vector2(noiseTexture.width, noiseTexture.height)); ComputeShaderUtility.Dispatch(movingShader, buffer, 0, currentPosBuffer.count); }
public void DrawTerrain(CommandBuffer buffer, int pass, Vector4[] planes) { if (loadedBufferList.Length <= 0) { return; } buffer.SetComputeBufferParam(shader, 1, ShaderIDs._DispatchBuffer, dispatchDrawBuffer); buffer.SetComputeBufferParam(shader, 0, ShaderIDs._DispatchBuffer, dispatchDrawBuffer); buffer.SetComputeBufferParam(shader, 0, ShaderIDs._CullResultBuffer, culledResultsBuffer); buffer.SetComputeBufferParam(shader, 0, ShaderIDs._TerrainChunks, loadedBuffer); buffer.SetGlobalBuffer(ShaderIDs._TerrainChunks, loadedBuffer); buffer.SetGlobalVector(ShaderIDs._HeightScaleOffset, float4(terrainData.heightScale, terrainData.heightOffset, 1, 1)); buffer.SetGlobalBuffer(ShaderIDs._CullResultBuffer, culledResultsBuffer); buffer.SetComputeVectorArrayParam(shader, ShaderIDs.planes, planes); buffer.DispatchCompute(shader, 1, 1, 1, 1); ComputeShaderUtility.Dispatch(shader, buffer, 0, loadedBufferList.Length); buffer.DrawProceduralIndirect(Matrix4x4.identity, terrainData.drawTerrainMaterial, pass, MeshTopology.Triangles, dispatchDrawBuffer); }
public static void MoveScene(int sceneIndex, float3 deltaPosition, int clusterCount) { ComputeShader shad = resources.shaders.streamingShader; ComputeBuffer tempBuffer = GetTempPropertyBuffer(clusterCount + 1, sizeof(uint)); CommandBuffer cb = RenderPipeline.BeforeFrameBuffer; cb.SetComputeIntParam(shad, ShaderIDs._TargetElement, sceneIndex); cb.SetComputeBufferParam(shad, CLEAR_KERNEL, ShaderIDs._TempPropBuffer, tempBuffer); cb.SetComputeBufferParam(shad, COLLECT_KERNEL, ShaderIDs._TempPropBuffer, tempBuffer); cb.SetComputeBufferParam(shad, COLLECT_KERNEL, ShaderIDs.clusterBuffer, baseBuffer.clusterBuffer); cb.SetComputeBufferParam(shad, EXECUTE_CLUSTER_KERNEL, ShaderIDs._TempPropBuffer, tempBuffer); cb.SetComputeBufferParam(shad, EXECUTE_CLUSTER_KERNEL, ShaderIDs.clusterBuffer, baseBuffer.clusterBuffer); cb.SetComputeBufferParam(shad, EXECUTE_POINT_KERNEL, ShaderIDs.verticesBuffer, baseBuffer.verticesBuffer); cb.SetComputeBufferParam(shad, EXECUTE_POINT_KERNEL, ShaderIDs._TempPropBuffer, tempBuffer); cb.SetComputeVectorParam(shad, ShaderIDs._OffsetDirection, float4(deltaPosition, 1)); cb.DispatchCompute(shad, CLEAR_KERNEL, 1, 1, 1); ComputeShaderUtility.Dispatch(shad, cb, COLLECT_KERNEL, baseBuffer.clusterCount); ComputeShaderUtility.Dispatch(shad, cb, EXECUTE_CLUSTER_KERNEL, clusterCount); cb.DispatchCompute(shad, EXECUTE_POINT_KERNEL, clusterCount, 1, 1); }
public override void DrawClusterOccSingleCheck(ref RenderClusterOptions options, ref HizOptions hizOpts) { CommandBuffer buffer = options.command; buffer.SetComputeVectorParam(options.cullingShader, ShaderIDs._CameraUpVector, hizOpts.hizData.lastFrameCameraUp); buffer.SetComputeBufferParam(options.cullingShader, 5, ShaderIDs.clusterBuffer, baseBuffer.clusterBuffer); buffer.SetComputeTextureParam(options.cullingShader, 5, ShaderIDs._HizDepthTex, hizOpts.hizData.historyDepth); buffer.SetComputeVectorArrayParam(options.cullingShader, ShaderIDs.planes, options.frustumPlanes); buffer.SetComputeBufferParam(options.cullingShader, 5, ShaderIDs.resultBuffer, baseBuffer.resultBuffer); buffer.SetComputeBufferParam(options.cullingShader, 5, ShaderIDs.instanceCountBuffer, baseBuffer.instanceCountBuffer); buffer.SetComputeBufferParam(options.cullingShader, PipelineBaseBuffer.ComputeShaderKernels.ClearClusterKernel, ShaderIDs.instanceCountBuffer, baseBuffer.instanceCountBuffer); ComputeShaderUtility.Dispatch(options.cullingShader, options.command, 5, baseBuffer.clusterCount, 256); hizOpts.hizData.lastFrameCameraUp = hizOpts.currentCameraUpVec; buffer.SetGlobalBuffer(ShaderIDs.resultBuffer, baseBuffer.resultBuffer); buffer.SetGlobalBuffer(ShaderIDs.verticesBuffer, baseBuffer.verticesBuffer); PipelineFunctions.RenderProceduralCommand(baseBuffer, options.proceduralMaterial, buffer); buffer.DispatchCompute(options.cullingShader, PipelineBaseBuffer.ComputeShaderKernels.ClearClusterKernel, 1, 1, 1); //TODO 绘制其他物体 //TODO buffer.Blit(hizOpts.currentDepthTex, hizOpts.hizData.historyDepth, hizOpts.linearLODMaterial, 0); hizOpts.hizDepth.GetMipMap(hizOpts.hizData.historyDepth, buffer); }
public override void DrawCubeMap(MPointLight lit, ref RenderClusterOptions opts, ref CubeCullingBuffer buffer, int offset) { CommandBuffer cb = opts.command; ComputeShader shader = opts.cullingShader; Material depthMaterial = opts.proceduralMaterial; cb.SetComputeIntParam(shader, ShaderIDs._LightOffset, offset); ComputeShaderUtility.Dispatch(shader, cb, CubeFunction.RunFrustumCull, baseBuffer.clusterCount, 256); PerspCam cam = new PerspCam(); cam.aspect = 1; cam.farClipPlane = lit.range; cam.nearClipPlane = 0.3f; cam.position = lit.position; cam.fov = 90f; Matrix4x4 vpMatrix; cb.SetGlobalVector(ShaderIDs._LightPos, new Vector4(lit.position.x, lit.position.y, lit.position.z, lit.range)); cb.SetGlobalBuffer(ShaderIDs.verticesBuffer, baseBuffer.verticesBuffer); cb.SetGlobalBuffer(ShaderIDs.resultBuffer, baseBuffer.resultBuffer); //Forward cam.forward = Vector3.forward; cam.up = Vector3.down; cam.right = Vector3.left; cam.position = lit.position; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeZ); cb.ClearRenderTarget(true, true, Color.white); cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix); offset = offset * 20; cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset); //Back cam.forward = Vector3.back; cam.up = Vector3.down; cam.right = Vector3.right; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveZ); cb.ClearRenderTarget(true, true, Color.white); cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset); //Up cam.forward = Vector3.up; cam.up = Vector3.back; cam.right = Vector3.right; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveY); cb.ClearRenderTarget(true, true, Color.white); cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset); //Down cam.forward = Vector3.down; cam.up = Vector3.forward; cam.right = Vector3.right; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeY); cb.ClearRenderTarget(true, true, Color.white); cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset); //Right cam.forward = Vector3.right; cam.up = Vector3.down; cam.right = Vector3.forward; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveX); cb.ClearRenderTarget(true, true, Color.white); cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset); //Left cam.forward = Vector3.left; cam.up = Vector3.down; cam.right = Vector3.back; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeX); cb.ClearRenderTarget(true, true, Color.white); cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset); }
public void RemoveQuadTrees(NativeList <ulong> removeList) { void ErasePoint(TerrainQuadTree.QuadTreeNode *node) { node->listPosition = -1; notUsedHeightmapIndices.Add(node->panel.heightMapIndex); } int length = removeList.Length; TerrainQuadTree.QuadTreeNode **tree = (TerrainQuadTree.QuadTreeNode * *)removeList.unsafePtr; int targetLength = referenceBuffer.Length - length; int len = 0; if (targetLength <= 0) { for (int i = 0; i < length; ++i) { ErasePoint(tree[i]); } referenceBuffer.Clear(); return; } for (int i = 0; i < length; ++i) { TerrainQuadTree.QuadTreeNode *currentNode = tree[i]; if (currentNode->listPosition >= targetLength) { referenceBuffer[currentNode->listPosition] = 0; ErasePoint(currentNode); } } NativeArray <int2> transformList = new NativeArray <int2>(length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); int2 *transformPtr = transformList.Ptr(); len = 0; int currentIndex = referenceBuffer.Length - 1; for (int i = 0; i < length; ++i) { TerrainQuadTree.QuadTreeNode *treeNode = tree[i]; if (treeNode->listPosition < 0) { continue; } while (referenceBuffer[currentIndex] == 0) { currentIndex--; if (currentIndex < 0) { goto FINALIZE; } } TerrainQuadTree.QuadTreeNode *lastNode = (TerrainQuadTree.QuadTreeNode *)referenceBuffer[currentIndex]; currentIndex--; transformPtr[len] = new int2(treeNode->listPosition, lastNode->listPosition); len++; lastNode->listPosition = treeNode->listPosition; referenceBuffer[lastNode->listPosition] = (ulong)lastNode; ErasePoint(treeNode); } FINALIZE: referenceBuffer.RemoveLast(length); if (len <= 0) { return; } if (len > removebuffer.count) { removebuffer.Dispose(); removebuffer = new ComputeBuffer(len, sizeof(int2)); } removebuffer.SetData(transformList, 0, 0, len); transformShader.SetBuffer(0, ShaderIDs._IndexBuffer, removebuffer); transformShader.SetBuffer(0, ShaderIDs.clusterBuffer, clusterBuffer); ComputeShaderUtility.Dispatch(transformShader, 0, len, 64); transformList.Dispose(); }
public static void DrawShadow(MPointLight lit, CommandBuffer cb, MaterialPropertyBlock block, ref CubeCullingBuffer buffer, ref PipelineBaseBuffer baseBuffer, ComputeShader shader, int offset, Material depthMaterial) { cb.SetComputeIntParam(shader, ShaderIDs._LightOffset, offset); ComputeShaderUtility.Dispatch(shader, cb, RunFrustumCull, baseBuffer.clusterCount, 64); PerspCam cam = new PerspCam(); cam.aspect = 1; cam.farClipPlane = lit.range; cam.nearClipPlane = 0.3f; cam.position = lit.position; cam.fov = 90f; Matrix4x4 vpMatrix; block.SetVector(ShaderIDs._LightPos, new Vector4(lit.position.x, lit.position.y, lit.position.z, lit.range)); PipelineFunctions.SetShaderBuffer(ref baseBuffer, block); //Forward cam.forward = Vector3.forward; cam.up = Vector3.down; cam.right = Vector3.left; cam.position = lit.position; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeZ); cb.ClearRenderTarget(true, true, Color.white); block.SetMatrix(ShaderIDs._VP, vpMatrix); offset = offset * 20; cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block); //Back cam.forward = Vector3.back; cam.up = Vector3.down; cam.right = Vector3.right; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveZ); cb.ClearRenderTarget(true, true, Color.white); block.SetMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block); //Up cam.forward = Vector3.up; cam.up = Vector3.back; cam.right = Vector3.right; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveY); cb.ClearRenderTarget(true, true, Color.white); block.SetMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block); //Down cam.forward = Vector3.down; cam.up = Vector3.forward; cam.right = Vector3.right; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeY); cb.ClearRenderTarget(true, true, Color.white); block.SetMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block); //Right cam.forward = Vector3.right; cam.up = Vector3.down; cam.right = Vector3.forward; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveX); cb.ClearRenderTarget(true, true, Color.white); block.SetMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block); //Left cam.forward = Vector3.left; cam.up = Vector3.down; cam.right = Vector3.back; cam.UpdateTRSMatrix(); cam.UpdateProjectionMatrix(); vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix; cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeX); cb.ClearRenderTarget(true, true, Color.white); block.SetMatrix(ShaderIDs._VP, vpMatrix); cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block); }