Lenia: remove texture sampler, reduce gpu usage by ~45%

This commit is contained in:
Crispy 2023-07-22 20:24:12 +02:00
parent 13b30711ca
commit 6e1fe4d9fc
9 changed files with 94 additions and 97 deletions

View file

@ -20,6 +20,8 @@
#include "UnityCG.cginc"
#define WIDTH 512
struct appdata
{
float4 vertex : POSITION;
@ -33,7 +35,7 @@
float4 vertex : SV_POSITION;
};
sampler2D _LastFrame;
texture2D _LastFrame;
float _GrowtCenter;
float _GrowthWidth;
float _Speed;
@ -74,43 +76,42 @@
return exp(-((u-mu) * (u-mu)) / (2 * sigma * sigma)) * 2.0 - 1.0;
}
inline half value(float2 center, float x, float y) {
return tex2D(_LastFrame, center + float2(x, y)).r;
inline float value(uint2 p, int dx, int dy) {
const uint x = (p.x + dx) % WIDTH;
const uint y = (p.y + dy) % WIDTH;
return _LastFrame[uint2(x, y)].r;
}
fixed4 frag (v2f i) : SV_Target
{
if(_ProjectionParams.z > 1) discard;
const float resolution = 512.0;
const float d = 1.0 / resolution;
// Defines RADIUS Kernel total_max
#include "lenia_generated_kernel.cginc"
const uint2 p = i.uv * WIDTH;
float total = 0.0;
[unroll(RADIUS)]
for (int y = 0; y < RADIUS; y++) {
[unroll(RADIUS)]
for (int x = 1; x <= RADIUS; x++) {
const float xx = (float)x * d;
const float yy = (float)y * d;
total += value(i.uv, xx, yy) * Kernel[y][x-1];
total += value(i.uv, -yy, xx) * Kernel[y][x-1];
total += value(i.uv, -xx, -yy) * Kernel[y][x-1];
total += value(i.uv, yy, -xx) * Kernel[y][x-1];
total += value(p, x, y) * Kernel[y][x - 1];
total += value(p, -y, x) * Kernel[y][x - 1];
total += value(p, -x, -y) * Kernel[y][x - 1];
total += value(p, y, -x) * Kernel[y][x - 1];
}
}
float old_state = value(i.uv, 0.0, 0.0) ;
float old_state = value(p, 0, 0) ;
float count = total / total_max;
const float step = _Speed * unity_DeltaTime.x;
float state = activation(count) * step + old_state;
state = clamp(state, 0, 1);
// kernel visualization: lookup table (SLOW)
// kernel visualization: lookup table (VERY SLOW)
// float k = 0;
// {
// float2 p = (i.uv - 0.5 ) * resolution;
// float2 p = (i.uv - 0.5 ) * WIDTH;
// p = floor(p);
// if (p.x > 0 && p.y >= 0) {
// k = Kernel[p.y][p.x-1];
@ -124,7 +125,7 @@
// }
// kernel visualisation: real size
// float2 p = (i.uv - 0.5) * resolution;
// float2 p = (i.uv - 0.5) * WIDTH;
// float k = kernel(length(p)) * (max(abs(p.x), abs(p.y)) <= RADIUS);
// kernel visualisation: fill square

View file

@ -24,3 +24,5 @@ const half Kernel[21][20] = {
{0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
};
const float total_max = 356.63977;
// Total texture lookups: 206 * 4 + 1 = 825
// (lookups multiplied by 0.0 get optimised away by the shader compiler, and this giant table generally only exists at compile time)

View file

@ -8,15 +8,14 @@ Material:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: lenia_out
m_Shader: {fileID: 211, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 4800000, guid: 2e618ea97d394cb79a97f3f69d29807a, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 0
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses:
- ALWAYS
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
@ -71,6 +70,7 @@ Material:
- _DistortionStrength: 1
- _DistortionStrengthScaled: 0
- _DstBlend: 0
- _Emission: 0.5
- _EmissionEnabled: 0
- _FlipbookMode: 0
- _GlossMapScale: 1