Skip to content

Commit aad2440

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

File tree

6 files changed

+42
-45
lines changed

6 files changed

+42
-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: 32 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,8 @@ Shader "Hidden/VXGI/Voxelization"
1818
#pragma geometry geom
1919
#pragma fragment frag
2020
#pragma shader_feature _EMISSION
21+
#pragma shader_feature_local _METALLICGLOSSMAP
22+
#pragma shader_feature_local _SPECGLOSSMAP
2123

2224
#include "UnityCG.cginc"
2325
#include "Packages/com.looooong.srp.vxgi/ShaderLibrary/Variables.cginc"
@@ -29,18 +31,14 @@ Shader "Hidden/VXGI/Voxelization"
2931

3032
CBUFFER_START(UnityPerMaterial)
3133
half4 _Color;
34+
half3 _EmissionColor;
3235
float4 _MainTex_ST;
3336
half _Metallic;
34-
half3 _EmissionColor;
3537
CBUFFER_END
3638

39+
sampler2D _EmissionMap;
3740
sampler2D _MainTex;
3841
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;
4442

4543
AppendStructuredBuffer<VoxelData> VoxelBuffer;
4644

@@ -62,9 +60,14 @@ Shader "Hidden/VXGI/Voxelization"
6260
v2g vert(appdata_base v)
6361
{
6462
v2g o;
65-
o.vertex = mul(WorldToVoxel, mul(unity_ObjectToWorld, v.vertex));
63+
o.vertex = UnityObjectToClipPos(v.vertex);
6664
o.normal = UnityObjectToWorldNormal(v.normal);
6765
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
66+
67+
#ifdef UNITY_REVERSED_Z
68+
o.vertex.z = mad(o.vertex.z, -2.0, 1.0);
69+
#endif
70+
6871
return o;
6972
}
7073

@@ -107,11 +110,15 @@ Shader "Hidden/VXGI/Voxelization"
107110
for (int j = 0; j < 3; j++) {
108111
g2f o;
109112

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

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

116123
o.normal = i[j].normal;
117124
o.axis = axis;
@@ -145,23 +152,25 @@ Shader "Hidden/VXGI/Voxelization"
145152

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

154161
i.normal = normalize(i.normal);
155162

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
163+
#ifdef _EMISSION
164+
float3 emission = _EmissionColor * tex2Dlod(_EmissionMap, float4(i.uv, 0.0, 0.0));
165+
#else
166+
float3 emission = 0.0;
167+
#endif
168+
169+
float3 voxelPosition = float3(i.position.xy, i.position.z * Resolution);
161170

162171
VoxelData d;
163172
d.Initialize();
164-
d.SetPosition(RestoreAxis(float3(i.position.xy, i.position.z * DepthResolution), i.axis));
173+
d.SetPosition(RestoreAxis(voxelPosition, i.axis));
165174
d.SetNormal(i.normal);
166175
d.SetColor(mad(-0.5, metallic, 1.0) * _Color * tex2Dlod(_MainTex, float4(i.uv, 0.0, 0.0)));
167176
d.SetEmission(emission + ShadeSH9(float4(i.normal, 1.0)));

0 commit comments

Comments
 (0)