Skip to content

Shader optimizations #26

@TokisanGames

Description

@TokisanGames

Performance

  • 1. Precalculate this in C++ whenever terrain_size is changed and pass in the uniform: terrain_size_inv rather than in the shader. WIll save millions of divides per second.
    code += " vec2 ps = vec2(1.0) / terrain_size;\n"; // TODO can be precalculated

e.g.


  • 3. Lookup region only once per vertex. Move into vertex() and save as a varrying for the colormap in fragment. - Looks bad on lower LODs when using this to calculate pixel data as the vertices are far apart.


  • 5. As above remove all derivative functions and at least get them out of branches. Test optimizing branches

https://community.khronos.org/t/if-statements-in-shaders-confused-by-results/73518/15


  • 6. Replace 16x16 Region map with an array. No need for a texture lookup

  • 7. Replace 512x512 region blend map w/ a zoomed UV lookup of the region map, or a mathematical version from an array.

  • 8. Since we're blending 4 adjacent vertices, move lookups to vertex() and weight in fragment. - Doesn't look good on lower LODs as the vertices are farther apart, but see redesign below.


  • 10. Instead of sampling base/overlay texture lookups for each vertex, bilinear interpolate the UV for the texture lookup and sample albedo/normal once.

  • 11. light() is wasteful for mobile according to lfxu. Rewrite it to do only what is needed.

Functional

Sometimes I've had the need for antialiasing and have typically faded out (vertex grid, macrovariation) instead of aliasing. Perhaps anti-aliasing is better.
https://stackoverflow.com/questions/20204561/advanced-moir%C3%A9-a-pattern-reduction-in-hlsl-glsl-procedural-textures-shader-a
https://www.yaldex.com/open-gl/ch17lev1sec4.html#ch17fig04

Redesign

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    1.x

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions