Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/gpu instancing/roads indirect #3203

Draft
wants to merge 63 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
9518465
add scripts
popuz Jan 20, 2025
f8454d5
assign data monoBehaviour to prefabs
popuz Jan 20, 2025
5f25777
shader channge
popuz Jan 20, 2025
2ac5a06
swap Unity standard shader to DCL/Scene in some MeshRenderers
popuz Jan 20, 2025
351c068
small clean-up
popuz Jan 20, 2025
9d52656
wip on Roads debug (only one instance rendered)
popuz Jan 20, 2025
4fd7070
wip on fixing roads layout
popuz Jan 20, 2025
909f7d4
fixed roads layout 🟢
popuz Jan 20, 2025
a7b3da5
Modified the Scene Shader for Per Instance Buffer
GBirch33 Jan 16, 2025
20e006f
extracted instancing to a Service
popuz Jan 20, 2025
d0b8125
fixed fountain being disabled
popuz Jan 21, 2025
d99fa62
finally fixed offset issue with matrices collection
popuz Jan 21, 2025
31d0fb4
collection of matrices on road prefab basis
popuz Jan 22, 2025
e33318d
full Roads Layout instancing data collection into RoadConfig
popuz Jan 22, 2025
abdb831
batches optimization
popuz Jan 22, 2025
a9ecd23
scene clean-up
popuz Jan 22, 2025
3749bcc
adjusted PerInstance map to match Indirect shader
popuz Jan 23, 2025
1aa58de
road tile Indirect working with Buffer
popuz Jan 24, 2025
ff37bed
working Indirect with several instanes with debug shader
popuz Jan 24, 2025
35361f6
fixed submeshes
popuz Jan 24, 2025
8d8cc4c
render full prefab with all meshes and instances
popuz Jan 24, 2025
d98961b
re-namings for better understanding of Data collection
popuz Jan 27, 2025
bc5a9e0
with caching and re-usage of the buffer
popuz Jan 27, 2025
a63935b
split to DIrect and Indirect meshes
popuz Jan 28, 2025
cebaa83
Scene shader one mesh Indirect instanced draw working
popuz Jan 28, 2025
1771d0b
Compute shaders moved into current branch
GBirch33 Jan 29, 2025
bf0bc44
refactoring data gathering: collected same LODGroups data in one buff…
popuz Jan 30, 2025
b9c62a0
Improvements to Scene shader for GPU Indirect Instancing
GBirch33 Jan 30, 2025
d8cb310
Merge branch 'feat/gpu-instancing/roads_indirect' of github.com:decen…
GBirch33 Jan 30, 2025
ca19791
adding missing meta file
GBirch33 Jan 30, 2025
8b351cb
wip on new playground
popuz Jan 30, 2025
78a3eaf
texcoord changes for colour tinting
GBirch33 Jan 30, 2025
e6b384a
Merge branch 'feat/gpu-instancing/roads_indirect' of github.com:decen…
GBirch33 Jan 30, 2025
5238332
fixed old playground
popuz Jan 30, 2025
e2f7067
Merge branch 'refs/heads/dev' into feat/gpu-instancing/roads_indirect
popuz Jan 30, 2025
dde07e1
new Playground for debugging one Prefab working both Instanced and In…
popuz Jan 30, 2025
9491e90
all prefab meshes Indirect rendering
popuz Jan 30, 2025
978e7e8
limit lod in debug
popuz Jan 31, 2025
6f0c040
- add division to Indirect and Direct instances based on shader; - ad…
popuz Jan 31, 2025
2d349cc
add RoadsShift
popuz Jan 31, 2025
dcc0bdc
handled sharedMaterial problem
popuz Jan 31, 2025
d4b8453
- rendering all lods levels; - direct and indirect split for full Layout
popuz Jan 31, 2025
4e6c0fc
hiding roads visual
popuz Feb 3, 2025
fb6354e
extracted Service
popuz Feb 3, 2025
7a7441e
add support for Direct instancing
popuz Feb 4, 2025
bce9b03
update the grass shader
GBirch33 Feb 4, 2025
6c8c44e
Moving LOD groups to prefab root, turned off shadow casting on Road p…
meshr00m Feb 4, 2025
97cfbf7
clean-up from old classes
popuz Feb 4, 2025
a0da1e6
moved shader check inside candidate; simplified GPUInsRenderer
popuz Feb 5, 2025
03b40ae
moved instancing to its folder
popuz Feb 5, 2025
1f83e4f
injected and working GPU Instanced roads with Worlds handling
popuz Feb 5, 2025
639e919
Reverted Road tiles to original LOD grouping, turned off shadow casti…
meshr00m Feb 6, 2025
a60a1dd
changed distance cull settings
meshr00m Feb 6, 2025
4c75c20
disabled roads visuals
popuz Feb 6, 2025
dd1f583
Merge remote-tracking branch 'origin/feat/gpu-instancing/roads_indire…
popuz Feb 6, 2025
2efe0b0
removed check for shader
popuz Feb 6, 2025
7989c03
Fix grass shader errors
GBirch33 Feb 6, 2025
7686afe
prefab collection Staged version (not working)
popuz Feb 7, 2025
ec9b294
GrassShader package update
GBirch33 Feb 7, 2025
21f91b4
Long Roads fix
meshr00m Feb 7, 2025
a3d8b01
fixed data collection for shader
popuz Feb 7, 2025
0467120
Merge remote-tracking branch 'origin/feat/gpu-instancing/roads_indire…
popuz Feb 7, 2025
dee9ea1
added whitelisted shaders to the PrefabData components
popuz Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions Explorer/Assets/DCL/LOD/Systems/LODContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
using DCL.Roads.Systems;
using ECS;
using Global;
using Global.Dynamic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using DCL.Optimization.Pools;
using DCL.ResourcesUnloading;
using DCL.Roads.GPUInstancing;
using DCL.Roads.GPUInstancing.Playground;
using UnityEngine;
using UnityEngine.AddressableAssets;

Expand All @@ -34,6 +34,8 @@ public class LODContainer : DCLWorldContainer<LODContainer.LODContainerSettings>
private ProvidedAsset<RoadSettingsAsset> roadSettingsAsset;
private List<GameObject> roadAssetsPrefabList;
private ProvidedAsset<LODSettingsAsset> lodSettingsAsset;
private GPUInstancingService gpuInstancingService;
private RealmData realmData;

public LODPlugin LODPlugin { get; private set; } = null!;

Expand All @@ -48,18 +50,20 @@ private LODContainer(IAssetsProvisioner assetsProvisioner)
this.assetsProvisioner = assetsProvisioner;
}

public static async UniTask<(LODContainer? container, bool success)> CreateAsync(
IAssetsProvisioner assetsProvisioner,
public static async UniTask<(LODContainer? container, bool success)> CreateAsync(IAssetsProvisioner assetsProvisioner,
IDecentralandUrlsSource decentralandUrlsSource,
StaticContainer staticContainer,
IPluginSettingsContainer settingsContainer,
RealmData realmData,
TextureArrayContainerFactory textureArrayContainerFactory,
IDebugContainerBuilder debugBuilder,
bool lodEnabled,
GPUInstancingService gpuInstancingService,
CancellationToken ct)
{
var container = new LODContainer(assetsProvisioner);
container.gpuInstancingService = gpuInstancingService;
container.realmData = realmData;

return await container.InitializeContainerAsync<LODContainer, LODContainerSettings>(settingsContainer, ct, c =>
{
Expand Down Expand Up @@ -104,6 +108,8 @@ public override void Dispose()
{
roadSettingsAsset.Dispose();
lodSettingsAsset.Dispose();

realmData.RealmType.OnUpdate -= SwitchRoadsInstancedRendering;
}

protected override async UniTask InitializeInternalAsync(LODContainerSettings lodContainerSettings, CancellationToken ct)
Expand All @@ -113,7 +119,21 @@ protected override async UniTask InitializeInternalAsync(LODContainerSettings lo
roadAssetsPrefabList = new List<GameObject>();

foreach (AssetReferenceGameObject? t in roadSettingsAsset.Value.RoadAssetsReference)
roadAssetsPrefabList.Add((await assetsProvisioner.ProvideMainAssetAsync(t, ct: ct)).Value);
{
var prefab = await assetsProvisioner.ProvideMainAssetAsync(t, ct: ct);
prefab.Value.GetComponent<GPUInstancingPrefabData>().HideVisuals();
roadAssetsPrefabList.Add(prefab.Value);
}

realmData.RealmType.OnUpdate += SwitchRoadsInstancedRendering;
}

private void SwitchRoadsInstancedRendering(RealmKind realmKind)
{
if (realmKind == RealmKind.GenesisCity)
gpuInstancingService.AddToIndirect(roadSettingsAsset.Value.IndirectCandidates);
else
gpuInstancingService.Remove(roadSettingsAsset.Value.IndirectCandidates);
}

[Serializable]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,6 @@ public static class ReportCategory
public const string IN_WORLD_CAMERA = nameof(IN_WORLD_CAMERA);

public const string FRIENDS = nameof(FRIENDS);
public const string GPU_INSTANCING = nameof(GPU_INSTANCING);
}
}
3 changes: 2 additions & 1 deletion Explorer/Assets/DCL/PluginSystem/DCL.Plugins.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@
"GUID:e9db755bba99425db4ca5d30e48b7429",
"GUID:5462d37de7d4344df8aade58a45b403e",
"GUID:f3634757d00dab2429c6c11e69404e97",
"GUID:09c5cab4669b047398fa5d42a2f64f5b"
"GUID:09c5cab4669b047398fa5d42a2f64f5b",
"GUID:422b93d277444c569adf5e25ecef10ba"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
3 changes: 3 additions & 0 deletions Explorer/Assets/DCL/Rendering/GPUInstanceBatcher.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include "Includes/PlatformSpecifications.cginc"

#pragma kernel CSArgsBufferDoubleInstanceCount

uniform RWStructuredBuffer<uint> argsBuffer;
uniform uint count;

[numthreads(GPUI_THREADS, 1, 1)]
void CSArgsBufferDoubleInstanceCount(uint2 id : SV_DispatchThreadID)
{
if (id.x >= count)
return;

argsBuffer[id.x * 5 + 1] *= 2;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef _DCL_BOUNDS_FUNCTIONS_
#define _DCL_BOUNDS_FUNCTIONS_

// Bounding Box
// uniform float3 boundsCenter;
// uniform float3 boundsExtents;

// Bounding Sphere

// Bounding Capsule (Avatar)
// uniform float4x4 modifierTransform;
// uniform float modifierRadius;
// uniform float modifierHeight;
// uniform float3 modifierAxis;

#include "MathematicsFunctions.cginc"

inline bool TestBoundingBox(float3 position, float3 boundsCentre, float3 boundsExtents)
{
float3 Min = boundsCentre - boundsExtents;
float3 Max = boundsCentre + boundsExtents;

bool bIsInside = position.x >= Min.x &&
position.x <= Max.x &&
position.y >= Min.y &&
position.y <= Max.y &&
position.z >= Min.z &&
position.z <= Max.z;

return bIsInside;
}

inline bool TestBoundingSphere(float3 position, float3 boundsCentre, float radius)
{
return distance(boundsCentre, position) <= radius;
}

inline bool TestBoundingCapsule(float3 position, float boundsCentre, float height, float radius, float3 axis, float4x4 transform)
{
float pointChange = 0;
if (height / 2 > radius)
{
pointChange = (height / 2) - radius;
float4x4 scaled = SetScaleOfMatrix(transform, 1);

// https://math.stackexchange.com/questions/1905533/find-perpendicular-distance-from-point-to-line-in-3d
float3 A = position;
float3 B = mul(scaled, float4(boundsCentre.x + axis.x * pointChange, boundsCentre.y + axis.y * pointChange, boundsCentre.z + axis.z * pointChange, 1.0)).xyz;
float3 C = mul(scaled, float4(boundsCentre.x - axis.x * pointChange, boundsCentre.y - axis.y * pointChange, boundsCentre.z - axis.z * pointChange, 1.0)).xyz;

float3 d = (C - B) / distance(C, B);
float3 v = A - B;
float t = dot(v, d);
float3 P = B + t * d;

float distPB = distance(P, B);
float distPC = distance(P, C);
float distBC = distance(B, C);

if (abs(distBC - (distPB + distPC)) < 0.1)
return distance(P, A) <= radius;
else if (distPB < distPC)
return distance(B, A) <= radius;
else
return distance(C, A) <= radius;
}
else
return distance(boundsCentre + transform._14_24_34, position) <= radius;
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef _DCL_CAMERA_FUNCTIONS_
#define _DCL_CAMERA_FUNCTIONS_

#include "InputParams.cginc"

inline void CalculateDistanceAndScreenSize(float4x4 instanceMatrix, out float dist, out float maxViewSize)
{
dist = 0;
maxViewSize = 0;

float3 scale = float3(length(instanceMatrix._11_12_13), length(instanceMatrix._21_22_23), length(instanceMatrix._31_32_33));
dist = abs(distance(instanceMatrix._14_24_34, camPos));
maxViewSize = max(max(boundsExtents.x * scale.x, boundsExtents.y * scale.y), boundsExtents.z * scale.z) / (dist * halfAngle * 2);
}

inline uint4 CalculateRequiredLODs(uint nLODCount, float4x4 mLODScreenSpaceSizes, float fShadowDistance, float fScreenSpaceSize, float fDistance, bool bIsVisible, bool bIsShadowVisible)
{
uint4 output = uint4(8,8,0,8);
for (uint i = 0; i < nLODCount; ++i)
{
if (bIsVisible)
{
if (fScreenSpaceSize > mLODScreenSpaceSizes[i] && fScreenSpaceSize < mLODScreenSpaceSizes[i+8])
{
output[0] = i;
if (i < nLODCount-1)
{
if (fScreenSpaceSize > mLODScreenSpaceSizes[i+1] && fScreenSpaceSize < mLODScreenSpaceSizes[i+8+1])
{
output[1] = i+1;
output[2] = uint((fScreenSpaceSize - mLODScreenSpaceSizes[i+1]) / (mLODScreenSpaceSizes[i] - mLODScreenSpaceSizes[i+1]) * 255);
}
}
else
{
float fPercentageOfFinalLOD = (fScreenSpaceSize / (mLODScreenSpaceSizes[i] - mLODScreenSpaceSizes[i+8]));
float fLastTenPercentDither = clamp((fPercentageOfFinalLOD - 0.9f), 0.0f, 0.1f) * 10.0f;
output[2] = uint(fLastTenPercentDither * 255);
}
if (bIsShadowVisible)
{
if (fDistance < fShadowDistance)
output[3] = i;
}
break;
}
}
}
return output;
}


#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading