Skip to content

Commit

Permalink
Removed branching from inner for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
nt314p committed Nov 26, 2021
1 parent 38b064d commit 8e76946
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -9839,7 +9839,7 @@ LightingSettings:
m_Name: Settings.lighting
serializedVersion: 3
m_GIWorkflowMode: 1
m_EnableBakedLightmaps: 1
m_EnableBakedLightmaps: 0
m_EnableRealtimeLightmaps: 0
m_RealtimeEnvironmentLighting: 1
m_BounceScale: 1
Expand Down
22 changes: 8 additions & 14 deletions Assets/Shaders/NBodyCompute.compute
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

#define threadsX 512 // threads per group
#define G_u 0.0000000000667408 // universal gravitational constant
#define a_b 0.06637 // acceleration due to a 10^30 kg mass at 1 exameter away (10^15 m)
#define particleRadius 6

struct PointMass
{
float mass; // planned [in 10^30 kilograms]
float3 position; // planned [in petameters (10^15 meters)]
float mass;
float3 position;
};

struct Motion
Expand Down Expand Up @@ -51,31 +50,26 @@ void StepSimulation(uint3 id : SV_DispatchThreadID, uint groupIndex : SV_GroupIn
float3 vel = m.velocity;
float3 accel = 0;

for (uint i = 0; i < numMasses; i += threadsX) // calculate net acceleration WORKS BETTER
for (uint i = 0; i < numMasses; i += threadsX)
{
uint index = groupIndex + i;
threadGroupPointMasses[groupIndex] = masses[index]; // each thread copies global data into thread memory
GroupMemoryBarrierWithGroupSync();

for (uint i2 = 0; i2 < threadsX; ++i2)
{
PointMass p = threadGroupPointMasses[i2]; // faster read from thread memory
float mass = p.mass;
float3 deltaPos = p.position - pos;
float sqrMagnitude = dot(deltaPos, deltaPos);
float invDist = rsqrt(sqrMagnitude);
float invDist = rsqrt(dot(deltaPos, deltaPos)); // 1/sqrt(mag^2) = 1/mag
invDist = id.x == index ? 0 : invDist;
float cubeInverse = invDist * invDist * invDist;
if (id.x != index && sqrMagnitude != 0)
{
accel += deltaPos * (mass * cubeInverse);
}
accel += deltaPos * (p.mass * cubeInverse);
}
}

accel *= G_u;

GroupMemoryBarrierWithGroupSync(); // sync threads before translating masses

accel *= G_u;
pos += vel * deltaTime + m.acceleration * halfDeltaTime * deltaTime; // updating position using velocity Verlet integration
vel += (m.acceleration + accel) * halfDeltaTime; // updating velocity using velocity Verlet integration

Expand Down
7 changes: 2 additions & 5 deletions Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{
"dependencies": {
"com.unity.2d.sprite": "1.0.0",
"com.unity.collab-proxy": "1.5.7",
"com.unity.collab-proxy": "1.9.0",
"com.unity.ide.rider": "2.0.7",
"com.unity.ide.visualstudio": "2.0.8",
"com.unity.ide.vscode": "1.2.3",
"com.unity.test-framework": "1.1.24",
"com.unity.test-framework": "1.1.30",
"com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.4.8",
"com.unity.ugui": "1.0.0",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
Expand Down
43 changes: 3 additions & 40 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
"dependencies": {}
},
"com.unity.collab-proxy": {
"version": "1.5.7",
"version": "1.9.0",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.nuget.newtonsoft-json": "2.0.0"
},
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.ext.nunit": {
Expand All @@ -31,31 +29,8 @@
},
"url": "https://packages.unity.com"
},
"com.unity.ide.visualstudio": {
"version": "2.0.8",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.test-framework": "1.1.9"
},
"url": "https://packages.unity.com"
},
"com.unity.ide.vscode": {
"version": "1.2.3",
"depth": 0,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.newtonsoft-json": {
"version": "2.0.0",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.test-framework": {
"version": "1.1.24",
"version": "1.1.30",
"depth": 0,
"source": "registry",
"dependencies": {
Expand All @@ -74,18 +49,6 @@
},
"url": "https://packages.unity.com"
},
"com.unity.timeline": {
"version": "1.4.8",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.modules.director": "1.0.0",
"com.unity.modules.animation": "1.0.0",
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.particlesystem": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.ugui": {
"version": "1.0.0",
"depth": 0,
Expand Down
13 changes: 12 additions & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ PlayerSettings:
androidRenderOutsideSafeArea: 1
androidUseSwappy: 0
androidBlitType: 0
androidResizableWindow: 0
androidDefaultWindowWidth: 1920
androidDefaultWindowHeight: 1080
androidMinimumWindowWidth: 400
androidMinimumWindowHeight: 300
androidFullscreenMode: 1
defaultIsNativeResolution: 1
macRetinaSupport: 1
runInBackground: 1
Expand Down Expand Up @@ -121,6 +127,7 @@ PlayerSettings:
vulkanEnableSetSRGBWrite: 0
vulkanEnablePreTransform: 0
vulkanEnableLateAcquireNextImage: 0
vulkanEnableCommandBufferRecycling: 1
m_SupportedAspectRatios:
4:3: 1
5:4: 1
Expand Down Expand Up @@ -236,6 +243,7 @@ PlayerSettings:
useCustomGradlePropertiesTemplate: 0
useCustomProguardFile: 0
AndroidTargetArchitectures: 1
AndroidTargetDevices: 0
AndroidSplashScreenScale: 0
androidSplashScreen: {fileID: 0}
AndroidKeystoreName:
Expand All @@ -252,6 +260,7 @@ PlayerSettings:
height: 180
banner: {fileID: 0}
androidGamepadSupportLevel: 0
chromeosInputEmulation: 1
AndroidMinifyWithR8: 0
AndroidMinifyRelease: 0
AndroidMinifyDebug: 0
Expand Down Expand Up @@ -358,6 +367,7 @@ PlayerSettings:
cameraUsageDescription:
locationUsageDescription:
microphoneUsageDescription:
bluetoothUsageDescription:
switchNMETAOverride:
switchNetLibKey:
switchSocketMemoryPoolSize: 6144
Expand Down Expand Up @@ -567,6 +577,7 @@ PlayerSettings:
ps4videoRecordingFeaturesUsed: 0
ps4contentSearchFeaturesUsed: 0
ps4CompatibilityPS5: 0
ps4AllowPS5Detection: 0
ps4GPU800MHz: 1
ps4attribEyeToEyeDistanceSettingVR: 0
ps4IncludedModules: []
Expand Down Expand Up @@ -595,7 +606,7 @@ PlayerSettings:
additionalCompilerArguments: {}
platformArchitecture: {}
scriptingBackend:
Standalone: 0
Standalone: 1
il2cppCompilerConfiguration: {}
managedStrippingLevel: {}
incrementalIl2cppBuild: {}
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2020.3.12f1
m_EditorVersionWithRevision: 2020.3.12f1 (b3b2c6512326)
m_EditorVersion: 2020.3.18f1
m_EditorVersionWithRevision: 2020.3.18f1 (a7d1c678663c)

0 comments on commit 8e76946

Please sign in to comment.