Made color and dapth calculations in parallel

- Edited RenderDepth.sahder to 2 sapces calculations
 - In DepthRenderPassFeature.cs changes passEvent to rendering after post-processing
 - Remove side cutting merged image in BlendShader.compute
This commit is contained in:
Max Barashev 2026-06-03 15:37:10 +03:00
parent f995cf6721
commit a2f9328f5f
6 changed files with 62 additions and 58 deletions

View File

@ -658,7 +658,7 @@ Transform:
m_GameObject: {fileID: 365053774}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.12557845, y: 0.64992166, z: -10.212498}
m_LocalPosition: {x: 0.37, y: -9.73, z: -0.46}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
@ -748,7 +748,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!4 &409312061
Transform:
m_ObjectHideFlags: 0
@ -1444,7 +1444,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &686694434
RectTransform:
m_ObjectHideFlags: 0
@ -1591,6 +1591,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
testDistance: 0.08
DepthShader: {fileID: 4800000, guid: 38575fbac906d53499789597d61d2fa4, type: 3}
depthMaterial: {fileID: 0}
BlendShader: {fileID: 7200000, guid: 5bf2857e309eda648b826d75af82d1ab, type: 3}
camera1: {fileID: 146129744}
camera2: {fileID: 73635654}
@ -1748,7 +1749,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &867358639
MonoBehaviour:
m_ObjectHideFlags: 0
@ -2120,7 +2121,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &1682170431
RectTransform:
m_ObjectHideFlags: 0
@ -2302,9 +2303,9 @@ RectTransform:
m_Children: []
m_Father: {fileID: 2034953397}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: -160}
m_AnchorMin: {x: 1, y: 0}
m_AnchorMax: {x: 1, y: 0}
m_AnchoredPosition: {x: -300, y: 200}
m_SizeDelta: {x: 500, y: 300}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!1 &2034953393

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal.Internal;
using UnityEngine.UI;
@ -37,6 +38,7 @@ public class BlendShaderController : MonoBehaviour
[Range(0.0f, 2.0f)]
public float testDistance = 0.08f;
public Shader DepthShader;
public Material depthMaterial;
public ComputeShader BlendShader;
public Camera camera1;
public Camera camera2;
@ -57,6 +59,7 @@ public class BlendShaderController : MonoBehaviour
private bool flag = false;
private ComputeBuffer computeBuffer;
private ComputeBuffer depthBuffer;
private float[] resultArray;
Thread savingThread;
Task<string> task1;
@ -74,6 +77,10 @@ public class BlendShaderController : MonoBehaviour
void InitializeComputeBuffer()
{
computeBuffer = new ComputeBuffer(currentRes * currentRes, 1 * sizeof(float));//2 dimensional array of 1 value type of float
depthBuffer = new ComputeBuffer(currentRes * currentRes, 1 * sizeof(float));
depthMaterial = CoreUtils.CreateEngineMaterial(DepthShader);
depthMaterial.SetBuffer("distBuffer", depthBuffer);
}
void InitializeTextures()
@ -115,25 +122,23 @@ public class BlendShaderController : MonoBehaviour
if (Input.GetKeyDown(KeyCode.F))
{
SetFlag();
flag = !flag;
}
if (flag)
{
GenerateDepth();
}
// if (flag)
// {
// GenerateDepth();
// }
// if(flag)
// {
// RenderTexture tempRt = new RenderTexture(cameraTexture1);
// Graphics.Blit(tempRt, cameraTexture1, depthMaterial);
// tempRt = new RenderTexture(cameraTexture2);
// Graphics.Blit(tempRt, cameraTexture2, depthMaterial);
// }
UpdateShader(cameraTexture1, cameraTexture2);
if(Input.GetKeyDown(KeyCode.S))
{
SaveTextureAsync();
}
if (Input.GetKeyDown(KeyCode.A))
{
SaveTexture();
}
}
void UpdateShader(RenderTexture tex1, RenderTexture tex2)
@ -311,5 +316,11 @@ public class BlendShaderController : MonoBehaviour
{
computeBuffer.Release();
computeBuffer = null;
depthBuffer.Release();
depthBuffer = null;
Destroy(depthMaterial);
depthMaterial = null;
}
}

View File

@ -1,13 +1,16 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.XR;
public class DepthRenderPassFeature : ScriptableRendererFeature
{
class DepthRenderPass : ScriptableRenderPass
{
public static Action<float[]> OnDepthReady;
public static Queue<bool> Requests;
public static Queue<float[]> Answers;
@ -38,6 +41,7 @@ public class DepthRenderPassFeature : ScriptableRendererFeature
{
material.SetBuffer("distBuffer", computeBuffer);
material.SetFloat("res", rt.height);
material.SetInt("_RenderDepth", this.depth ? 1 : 0);
}
}
@ -52,8 +56,6 @@ public class DepthRenderPassFeature : ScriptableRendererFeature
// You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline.
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
if (!depth) return;
CommandBuffer cmd = CommandBufferPool.Get();
//RTHandle rt = renderingData.cameraData.renderer.cameraColorTargetHandle;
@ -132,7 +134,7 @@ public class DepthRenderPassFeature : ScriptableRendererFeature
m_ScriptablePass = new DepthRenderPass(material);
// Configures where the render pass should be injected.
m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
}
// Here you can inject one or multiple render passes in the renderer.

View File

@ -14,7 +14,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_Active: 1
shader: {fileID: 4800000, guid: 38575fbac906d53499789597d61d2fa4, type: 3}
Depth: 1
Depth: 0
--- !u!114 &-1878332245247344467
MonoBehaviour:
m_ObjectHideFlags: 0
@ -108,7 +108,7 @@ MonoBehaviour:
zFailOperation: 0
m_ShadowTransparentReceive: 1
m_RenderingMode: 1
m_DepthPrimingMode: 0
m_DepthPrimingMode: 1
m_CopyDepthMode: 0
m_AccurateGbufferNormals: 0
m_IntermediateTextureMode: 1

View File

@ -16,8 +16,16 @@ void CSMain (uint3 id : SV_DispatchThreadID)
{
int2 idxy1 = int2(clamp(id.x + Offset, 0, ResX), id.y);
int2 idxy2 = int2(clamp(id.x - Offset, 0, ResX), id.y);
float diffFactor = saturate(id.x - Offset) * saturate(ResX - (id.x + Offset));
float4 resVal = float4(((Camera1[idxy1] * diffFactor + Camera2[idxy2] * diffFactor) / 2).xyz, 1);
float3 col1 = Camera1[idxy1].xyz;
float3 col2 = Camera1[idxy2].xyz;
float diffFactorL = saturate(id.x - Offset);
float diffFactorR = saturate(ResX - (id.x + Offset));
float4 resVal = 0;
//if(diffFactorL && diffFactorR > 0)
resVal = float4(((col1 * diffFactorR + col2 * diffFactorL) / ((diffFactorL && diffFactorR) == false ? 1 : 2)).xyz, 1);
//else
//resVal = float4(max(col1, col2), 1);
Result[id.xy] = resVal;
MonoChannelResult[id.x + id.y * ResX] = resVal.r * FarClipPlane;
}

View File

@ -14,54 +14,36 @@ Shader "Hidden/RenderDepth"
Pass
{
HLSLPROGRAM
#pragma target 5.0
#pragma vertex Vert
#pragma fragment frag
//#include "UnityCG.cginc"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
// v2f vert (appdata v)
// {
// v2f o;
// o.vertex = UnityObjectToClipPos(v.vertex);
// o.uv = v.uv;
// return o;
// }
sampler2D _MainTex;
TEXTURE2D_X(_CameraOpaqueTexture);
SAMPLER(sampler_CameraOpaqueTexture);
RWStructuredBuffer<float> distBuffer;
int res;
bool _RenderDepth;
float4 frag (Varyings i) : SV_Target
{
float2 UV = i.positionCS.xy / _ScaledScreenParams.xy;
uint2 pixelIdx = uint2(i.positionCS.xy);
float3 color = SampleSceneColor(i.texcoord);
#if UNITY_REVERSED_Z
real depth = SampleSceneDepth(UV);
real depth = SampleSceneDepth(i.texcoord);
#else
// Adjust z to match NDC for OpenGL
real depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(UV));
real depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(i.texcoord));
#endif
depth = Linear01Depth(depth, _ZBufferParams);
return float4(depth, depth, depth, 1);
distBuffer[pixelIdx.y * _ScaledScreenParams.x + pixelIdx.x] = depth * _ProjectionParams.z;
return _RenderDepth ? float4(depth, depth, depth, 1) : float4(color, 1);
}
ENDHLSL
}