<p>You tile idea seems to be a good one to cut down on local complexity. You can't reasonably evaluate every primitive everywhere. Not if you want good performance anyway. So you need a simplified version for each tile, where you remove elements which have insignificant contribution. This is an interesting problem of its own. A simple version of this would be to:</p>

<ul>
<li>Assign each primitive "usefulness bounds" beyond which it is considered 0. What exactly those are will depend on the curves you're using to map distance to world height. To make your life easier, pick something which actually goes to 0 over a finite distance. Or alter your favorite curve so it does.</li>
<li>For each tile, trim out primitives for which the bounds don't intersect the tile. You can do this with a simple loop to begin with.</li>
<li>Further simplify the operation tree. This is a little vague because you did not provide details on the kind of operations which will be in your function. But essentially, you want to do the like of replacing <span class="math-container">$x \times 0$</span> by <span class="math-container">$0$</span> and <span class="math-container">$x + 0$</span> by <span class="math-container">$x$</span>, until you can't anymore. The zeroes come from the previous step where you removed some primitives.</li>
</ul>

<p>You may need spatial hierarchies if you want this to scale to a world with millions of primitives. This can quickly become a complex topic so I'll leave it at that for now. Ask again when you get there :-)</p>

<p>For your more immediate problem, I suggest using the CPU to rewrite your SDF using <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation" rel="nofollow noreferrer">reverse polish notation</a>. This will allow you to write your shader as a simple interpreter, with two large buffers as inputs:</p>

<ul>
<li>An integer buffer with commands (union, add, multiply, point primitive, segment primitive, etc).</li>
<li>A float buffer with operands and transforms for the primitives.</li>
</ul>

<p>The shader is then just a loop over commands which consumes operands as needed. You'll need different command/operand buffers for every tile, obviously. Depending on the typical nature of your SDF, you may find it more efficient to have your primitives always perform union and have an explicit push command for the rare exceptions. There are a lot of ways to go about this really.</p>

<p>You should take care to make sure the resulting SDF can be evaluated with a reasonably small stack as this will be fixed in your shader. This is another problem by itself, perhaps well suited for the computer science SE, but probably not too difficult to solve on your own either.</p>

<p>I'm sorry if I have not provided enough OpenGL specific details but GPUs are not my specialty.</p>

CC BY-SA 4.0
2020-04-22T20:40:55.943Z
Thanks for this response! The RPN idea especially sounds like a good fit. I was just recently trying to think of good ways to do grouping in my current system that only does left-to-right evaluation, and that might be just the thing.  Right now I am rendering 1 quad per SDF object, rather than tiling, but I imagine there's some tipping point for "too much overlapping stuff per pixel" where the tiling will win.
jwd
CC BY-SA 4.0
2020-04-23T00:52:18.227Z
@jwd if your operations are such that you can manage one quad per primitive (eg. don't need too many temporary whole screen buffers), it may not be such a bad idea either. You could certainly write your own answer with it. It seems optimal in terms of how many times each primitive is evaluated. Perhaps not at keeping all the GPU cores busy in some cases. It makes it easy to use a space partition to trim the function in a huge world. And it seems a lot simpler.
Olivier
CC BY-SA 4.0
2020-04-22T17:55:29.370Z
2020-04-22T17:55:29.370Z
Olivier
<p>There are lots of cool things you can do with SDFs to render implicit surfaces. Most of the examples I've seen have been “all in the shader”, where the entire distance function is encoded in the shader source code.</p>

<p>Examples:</p>

<ul>
<li><a href="https://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm" rel="nofollow noreferrer">https://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm</a></li>
<li><a href="https://www.shadertoy.com/view/3lsSzf" rel="nofollow noreferrer">https://www.shadertoy.com/view/3lsSzf</a></li>
</ul>

<p>The problem is that all of those examples assume the shader has total knowledge of the entire world. It evaluates a single distance function, which is complex, but complete.</p>

<p>I want to make a large world (too large to fit in a shader, certainly), and am looking for approaches to render it with the SDF approach.</p>

<p>Does anyone have experience with this? It is 2D-only, if it helps.</p>

<p>The stumbling block seems to be: Since the rendering happens in the fragment shader, I somehow have to transfer “game world” information into that shader. But there do not seem to be good ways to send bulk data to the fragment shader.</p>

<p>Some ideas I have had:</p>

<ul>
<li>Write one generic shader that can draw, say, a combination of 500 SDFs.

<ul>
<li>The input to the shader (maybe a UBO?) would contain an encoded version of a piece of the world, with commands like “put a circle at (x,y,radius), do a union with the next object, …” to build the total SDF for that piece of the world.</li>
<li>On the CPU side, I'd have to split the world into chunks that could be rendered by that shader, and populate the input data appropriately for each draw call.</li>
<li>So for 2D, I might do a tile-based render of the screen, where each tile has “small” amount of data, enough to be handled in the shader.</li>
</ul></li>
<li>Generate shaders on-the-fly, depending on the part of the world I want to render.

<ul>
<li>Here, the shader code would look a lot like the “everything baked in” shaders, but I'd just be generating the code on the CPU.</li>
<li>This approach seems bad though, since compiling shaders is a pretty heavy process, in my experience. I did a test with an SDF that was the union of a few thousand circles, and it took 30sec+ to compile.</li>
</ul></li>
</ul>

<p>Any other ideas?</p>

<shader><fragment-shader>
SDF rendering for large world
jwd
# SDF rendering for large world

(Asked by: username_0 on 2020-04-10)

There are lots of cool things you can do with SDFs to render implicit surfaces. Most of the examples I've seen have been “all in the shader”, where the entire distance function is encoded in the shader source code.

Examples:

* <https://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm>
* <https://www.shadertoy.com/view/3lsSzf>

The problem is that all of those examples assume the shader has total knowledge of the entire world. It evaluates a single distance function, which is complex, but complete.

I want to make a large world (too large to fit in a shader, certainly), and am looking for approaches to render it with the SDF approach.

Does anyone have experience with this? It is 2D-only, if it helps.

The stumbling block seems to be: Since the rendering happens in the fragment shader, I somehow have to transfer “game world” information into that shader. But there do not seem to be good ways to send bulk data to the fragment shader.

Some ideas I have had:

* Write one generic shader that can draw, say, a combination of 500 SDFs.
  + The input to the shader (maybe a UBO?) would contain an encoded version of a piece of the world, with commands like “put a circle at (x,y,radius), do a union with the next object, …” to build the total SDF for that piece of the world.
  + On the CPU side, I'd have to split the world into chunks that could be rendered by that shader, and populate the input data appropriately for each draw call.
  + So for 2D, I might do a tile-based render of the screen, where each tile has “small” amount of data, enough to be handled in the shader.
* Generate shaders on-the-fly, depending on the part of the world I want to render.
  + Here, the shader code would look a lot like the “everything baked in” shaders, but I'd just be generating the code on the CPU.
  + This approach seems bad though, since compiling shaders is a pretty heavy process, in my experience. I did a test with an SDF that was the union of a few thousand circles, and it took 30sec+ to compile.

Any other ideas?

**Tags:** shader, fragment-shader


---

## 1 answers

### Answer by username_1 on 2020-04-22. Score: 4 | Accepted answer

You tile idea seems to be a good one to cut down on local complexity. You can't reasonably evaluate every primitive everywhere. Not if you want good performance anyway. So you need a simplified version for each tile, where you remove elements which have insignificant contribution. This is an interesting problem of its own. A simple version of this would be to:

* Assign each primitive "usefulness bounds" beyond which it is considered 0. What exactly those are will depend on the curves you're using to map distance to world height. To make your life easier, pick something which actually goes to 0 over a finite distance. Or alter your favorite curve so it does.
* For each tile, trim out primitives for which the bounds don't intersect the tile. You can do this with a simple loop to begin with.
* Further simplify the operation tree. This is a little vague because you did not provide details on the kind of operations which will be in your function. But essentially, you want to do the like of replacing $x \times 0$ by $0$ and $x + 0$ by $x$, until you can't anymore. The zeroes come from the previous step where you removed some primitives.

You may need spatial hierarchies if you want this to scale to a world with millions of primitives. This can quickly become a complex topic so I'll leave it at that for now. Ask again when you get there :-)

For your more immediate problem, I suggest using the CPU to rewrite your SDF using [reverse polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation). This will allow you to write your shader as a simple interpreter, with two large buffers as inputs:

* An integer buffer with commands (union, add, multiply, point primitive, segment primitive, etc).
* A float buffer with operands and transforms for the primitives.

The shader is then just a loop over commands which consumes operands as needed. You'll need different command/operand buffers for every tile, obviously. Depending on the typical nature of your SDF, you may find it more efficient to have your primitives always perform union and have an explicit push command for the rare exceptions. There are a lot of ways to go about this really.

You should take care to make sure the resulting SDF can be evaluated with a reasonably small stack as this will be fixed in your shader. This is another problem by itself, perhaps well suited for the computer science SE, but probably not too difficult to solve on your own either.

I'm sorry if I have not provided enough OpenGL specific details but GPUs are not my specialty.

> **Comments:**
> **username_0**: Thanks for this response! The RPN idea especially sounds like a good fit. I was just recently trying to think of good ways to do grouping in my current system that only does left-to-right evaluation, and that might be just the thing.  Right now I am rendering 1 quad per SDF object, rather than tiling, but I imagine there's some tipping point for "too much overlapping stuff per pixel" where the tiling will win.  
> **username_1**: @username_0 if your operations are such that you can manage one quad per primitive (eg. don't need too many temporary whole screen buffers), it may not be such a bad idea either. You could certainly write your own answer with it. It seems optimal in terms of how many times each primitive is evaluated. Perhaps not at keeping all the GPU cores busy in some cases. It makes it easy to use a space partition to trim the function in a huge world. And it seems a lot simpler.  

---

