Skip to content

Commit 5e93d22

Browse files
committed
Remove redundant matrix multiplication in voxelization step
1 parent 0137dcb commit 5e93d22

File tree

6 files changed

+41
-45
lines changed

6 files changed

+41
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
- Improved cone tracing direction selection.
1717
- Removed cone tracing bias.
18+
- Removed redundant matrix multiplication in voxelization step.
1819

1920
### Fixed
2021

Resources/VXGI/Compute/VoxelShader.compute

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void CSRender(uint id : SV_DispatchThreadID)
8181

8282
uint4 intColor = (uint4)round(color * RADIANCE_PRECISION);
8383

84+
position = min(position, Resolution - 1);
85+
8486
InterlockedAdd(RadianceRG[position], intColor.r | intColor.g << 16);
8587
InterlockedAdd(RadianceBA[position], intColor.b | intColor.a << 16);
8688
InterlockedAdd(RadianceCount[position], 1);

Runtime/Stages/Voxelizer.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
public class Voxelizer : System.IDisposable {
55
int _antiAliasing;
66
int _resolution;
7-
float _bound;
87
Camera _camera;
98
CommandBuffer _command;
109
DrawingSettings _drawingSettings;
1110
FilteringSettings _filteringSettings;
12-
Rect _rect;
1311
RenderTextureDescriptor _cameraDescriptor;
1412
ScriptableCullingParameters _cullingParameters;
1513
VXGI _vxgi;
@@ -18,7 +16,6 @@ public Voxelizer(VXGI vxgi) {
1816
_vxgi = vxgi;
1917

2018
_command = new CommandBuffer { name = "VXGI.Voxelizer" };
21-
_rect = new Rect(0f, 0f, 1f, 1f);
2219

2320
CreateCamera();
2421
CreateCameraDescriptor();
@@ -51,17 +48,14 @@ public void Voxelize(ScriptableRenderContext renderContext, VXGIRenderer rendere
5148

5249
UpdateCamera();
5350

54-
_camera.pixelRect = _rect;
55-
5651
_command.BeginSample(_command.name);
5752

5853
_command.GetTemporaryRT(ShaderIDs.Dummy, _cameraDescriptor);
5954
_command.SetRenderTarget(ShaderIDs.Dummy, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare);
6055

6156
_command.SetGlobalInt(ShaderIDs.Resolution, _resolution);
62-
_command.SetGlobalMatrix(ShaderIDs.WorldToVoxel, _vxgi.worldToVoxel);
63-
_command.SetGlobalMatrix(ShaderIDs.VoxelToProjection, GL.GetGPUProjectionMatrix(_camera.projectionMatrix, true) * _camera.worldToCameraMatrix * _vxgi.voxelToWorld);
6457
_command.SetRandomWriteTarget(1, _vxgi.voxelBuffer, false);
58+
_command.SetViewProjectionMatrices(_camera.worldToCameraMatrix, _camera.projectionMatrix);
6559

6660
_drawingSettings.perObjectData = renderer.RenderPipeline.PerObjectData;
6761

@@ -87,7 +81,7 @@ void CreateCamera() {
8781
_camera = gameObject.AddComponent<Camera>();
8882
_camera.allowMSAA = true;
8983
_camera.orthographic = true;
90-
_camera.nearClipPlane = 0f;
84+
_camera.pixelRect = new Rect(0f, 0f, 1f, 1f);
9185
}
9286

9387
void CreateCameraDescriptor() {
@@ -106,28 +100,20 @@ void CreateCameraSettings() {
106100
_filteringSettings = new FilteringSettings(RenderQueueRange.all);
107101
}
108102

109-
void ResizeCamera() {
110-
_camera.farClipPlane = _bound;
111-
_camera.orthographicSize = .5f * _camera.farClipPlane;
112-
}
113-
114103
void UpdateCamera() {
115104
if (_antiAliasing != (int)_vxgi.antiAliasing) {
116105
_antiAliasing = (int)_vxgi.antiAliasing;
117106
_cameraDescriptor.msaaSamples = _antiAliasing;
118107
}
119108

120-
if (_bound != _vxgi.bound) {
121-
_bound = _vxgi.bound;
122-
ResizeCamera();
123-
}
124-
125109
if (_resolution != (int)_vxgi.resolution) {
126110
_resolution = (int)_vxgi.resolution;
127111
_cameraDescriptor.height = _cameraDescriptor.width = _resolution;
128112
}
129113

130-
_camera.transform.position = _vxgi.voxelSpaceCenter - Vector3.forward * _camera.orthographicSize;
131-
_camera.transform.LookAt(_vxgi.voxelSpaceCenter, Vector3.up);
114+
_camera.farClipPlane = .5f * _vxgi.bound;
115+
_camera.orthographicSize = .5f * _vxgi.bound;
116+
_camera.nearClipPlane = -.5f * _vxgi.bound;
117+
_camera.transform.position = _vxgi.voxelSpaceCenter;
132118
}
133119
}

Runtime/Utilities/ShaderIDs.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ internal static class ShaderIDs {
4343
internal static readonly int Source = Shader.PropertyToID("Source");
4444
internal static readonly int Target = Shader.PropertyToID("Target");
4545
internal static readonly int VoxelBuffer = Shader.PropertyToID("VoxelBuffer");
46-
internal static readonly int VoxelToProjection = Shader.PropertyToID("VoxelToProjection");
4746
internal static readonly int VoxelToWorld = Shader.PropertyToID("VoxelToWorld");
4847
internal static readonly int WorldToVoxel = Shader.PropertyToID("WorldToVoxel");
4948
}

Runtime/VXGI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void SetupShader(ScriptableRenderContext renderContext) {
188188

189189
#region Messages
190190
void OnDrawGizmosSelected() {
191-
Gizmos.color = new Color(0, 0, 1, .2f);
191+
Gizmos.color = Color.green;
192192
Gizmos.DrawWireCube(voxelSpaceCenter, Vector3.one * bound);
193193
}
194194

Shaders/Voxelization.shader

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Shader "Hidden/VXGI/Voxelization"
88
Tags { "LightMode"="Voxelization" }
99

1010
Cull Off
11-
ZWrite Off
1211
ZTest Always
12+
ZWrite Off
1313

1414
HLSLPROGRAM
1515
#pragma require geometry
@@ -18,6 +18,7 @@ Shader "Hidden/VXGI/Voxelization"
1818
#pragma geometry geom
1919
#pragma fragment frag
2020
#pragma shader_feature _EMISSION
21+
#pragma shader_feature_local _METALLICGLOSSMAP
2122

2223
#include "UnityCG.cginc"
2324
#include "Packages/com.looooong.srp.vxgi/ShaderLibrary/Variables.cginc"
@@ -29,18 +30,14 @@ Shader "Hidden/VXGI/Voxelization"
2930

3031
CBUFFER_START(UnityPerMaterial)
3132
half4 _Color;
33+
half3 _EmissionColor;
3234
float4 _MainTex_ST;
3335
half _Metallic;
34-
half3 _EmissionColor;
3536
CBUFFER_END
3637

38+
sampler2D _EmissionMap;
3739
sampler2D _MainTex;
3840
sampler2D _MetallicGlossMap;
39-
sampler2D _EmissionMap;
40-
41-
// Map depth [0, 1] to Z coordinate [0, Resolution)
42-
static float DepthResolution = Resolution * 0.99999999;
43-
float4x4 VoxelToProjection;
4441

4542
AppendStructuredBuffer<VoxelData> VoxelBuffer;
4643

@@ -62,9 +59,14 @@ Shader "Hidden/VXGI/Voxelization"
6259
v2g vert(appdata_base v)
6360
{
6461
v2g o;
65-
o.vertex = mul(WorldToVoxel, mul(unity_ObjectToWorld, v.vertex));
62+
o.vertex = UnityObjectToClipPos(v.vertex);
6663
o.normal = UnityObjectToWorldNormal(v.normal);
6764
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
65+
66+
#ifdef UNITY_REVERSED_Z
67+
o.vertex.z = mad(o.vertex.z, -2.0, 1.0);
68+
#endif
69+
6870
return o;
6971
}
7072

@@ -107,11 +109,15 @@ Shader "Hidden/VXGI/Voxelization"
107109
for (int j = 0; j < 3; j++) {
108110
g2f o;
109111

110-
o.position = mul(VoxelToProjection, float4(SwizzleAxis(i[j].vertex, axis), 1.0));
112+
o.position = float4(SwizzleAxis(i[j].vertex, axis), 1.0);
111113

112-
#if defined(UNITY_REVERSED_Z)
113-
o.position.z = 1.0 - o.position.z;
114-
#endif
114+
#ifdef UNITY_UV_STARTS_AT_TOP
115+
o.position.y = -o.position.y;
116+
#endif
117+
118+
#ifdef UNITY_REVERSED_Z
119+
o.position.z = mad(o.position.z, 0.5, 0.5);
120+
#endif
115121

116122
o.normal = i[j].normal;
117123
o.axis = axis;
@@ -145,23 +151,25 @@ Shader "Hidden/VXGI/Voxelization"
145151

146152
fixed frag(g2f i) : SV_TARGET
147153
{
148-
#ifdef _METALLICGLOSSMAP
149-
float metallic = tex2D(_MetallicGlossMap, i.uv).r;
150-
#else
151-
float metallic = _Metallic;
152-
#endif
154+
#ifdef _METALLICGLOSSMAP
155+
float metallic = tex2D(_MetallicGlossMap, i.uv).r;
156+
#else
157+
float metallic = _Metallic;
158+
#endif
153159

154160
i.normal = normalize(i.normal);
155161

156-
#ifdef _EMISSION
157-
float3 emission = _EmissionColor * tex2Dlod(_EmissionMap, float4(i.uv, 0.0, 0.0));
158-
#else
159-
float3 emission = 0.0;
160-
#endif
162+
#ifdef _EMISSION
163+
float3 emission = _EmissionColor * tex2Dlod(_EmissionMap, float4(i.uv, 0.0, 0.0));
164+
#else
165+
float3 emission = 0.0;
166+
#endif
167+
168+
float3 voxelPosition = float3(i.position.xy, i.position.z * Resolution);
161169

162170
VoxelData d;
163171
d.Initialize();
164-
d.SetPosition(RestoreAxis(float3(i.position.xy, i.position.z * DepthResolution), i.axis));
172+
d.SetPosition(RestoreAxis(voxelPosition, i.axis));
165173
d.SetNormal(i.normal);
166174
d.SetColor(mad(-0.5, metallic, 1.0) * _Color * tex2Dlod(_MainTex, float4(i.uv, 0.0, 0.0)));
167175
d.SetEmission(emission + ShadeSH9(float4(i.normal, 1.0)));

0 commit comments

Comments
 (0)