**RE: The adventures of building a web renderer**

2019-01-04, 1:23 (This post was last modified: 2021-02-20, 23:06 by Lasse Deleuran.)

I promised to delete those 7 days of work with conditional lines. I that code I reduced the 1.181.960 conditional lines to 61.401 to be evaluated in each draw call. How about going the other way and evaluate 1.363.920 in each call?

That is essentially what happened when I moved this code to:

Custom Shaders

The idea is to perform the costly calculations on the GPU instead of the CPU. The control points (and opposite line point) are pushed to the GPU and the calculation is performed for each of the two end points of each conditional line (hence the doubling of the calculations).

The technical details behind this is to write 'RawMaterials' which use custom WebGL shaders written in the shader language GLSL.

Moving the code was not pain free:

But after fixing the initial rendering problems I was able to get it to work. The main piece of code is in the vertex shader where the alpha component in the color of the conditional line is used to determine if the line should be shown or not:

vColor.a *= sign(dot(d12, d13)*dot(d12, d14));

Here 'd12' is the vector difference between points 1 and 2 for the conditional line, 'd13' is between points 1 and 3, and so forth. The dot-operator computes the dot product of two vectors and the sign-operator returns '1', '0' or '-1' depending on the product.

The 'fragment shader' can now discard conditional lines that should not be shown:

if(vColor.a <= 0.001)

discard;

gl_FragColor = vColor;

The results on the test models are:

Psych

- Memory usage: 9.2MB

- Rendering time: 2.519ms

Executor

- Memory usage: 15.2MB

- Rendering time: 47.794ms

That is essentially what happened when I moved this code to:

Custom Shaders

The idea is to perform the costly calculations on the GPU instead of the CPU. The control points (and opposite line point) are pushed to the GPU and the calculation is performed for each of the two end points of each conditional line (hence the doubling of the calculations).

The technical details behind this is to write 'RawMaterials' which use custom WebGL shaders written in the shader language GLSL.

Moving the code was not pain free:

But after fixing the initial rendering problems I was able to get it to work. The main piece of code is in the vertex shader where the alpha component in the color of the conditional line is used to determine if the line should be shown or not:

vColor.a *= sign(dot(d12, d13)*dot(d12, d14));

Here 'd12' is the vector difference between points 1 and 2 for the conditional line, 'd13' is between points 1 and 3, and so forth. The dot-operator computes the dot product of two vectors and the sign-operator returns '1', '0' or '-1' depending on the product.

The 'fragment shader' can now discard conditional lines that should not be shown:

if(vColor.a <= 0.001)

discard;

gl_FragColor = vColor;

The results on the test models are:

Psych

- Memory usage: 9.2MB

- Rendering time: 2.519ms

Executor

- Memory usage: 15.2MB

- Rendering time: 47.794ms