Skip to content

Add compiled shaders #23

Closed
4 of 4 issues completed
Closed

Description

@Sekqies

It's time for us to have "the talk."

The talk about how using an interpreter to evaluate our functions at runtime isn't nearly as efficient as I thought it was! This project was made within the assumption that the performance loss from interpreting operators as a stack was not noticeable but, guess what? It is.

I noticed this yesterday when messing around with the textbox and tried to write the expression z^327 in the form of z*z*z*z... (as one does) and came across terrible performance

Video

Yes, we expect bad performance for evaluating 327 multiplications regardless of whether it's interpreted or compiled, but how bad? To test this, i replaced this line in the fragment shader:

vec2 func_value = run_stack(operator_stack,constants_stack,z);

with this

vec2 func_value = mult(z,mult(z,mult(z,mult(z,mult(z,mult(z,mult(z,mult(z,mult(z,mult(z,mult( [...]

The results speak for themselves:
Video

A compiler step would do just that. We could somewhat simply convert our reverse-polish notation back to infix, create a shader string, compile it and boom! Extra performance. Are we axing the interpreter, then? No way!

Compiling takes time, and the instant function preview we get right now is impossible with a compiler. What we are doing is using what we currently got as a preview. The user can then press a key to compile the function, giving them a "real deal" program that's far, far faster.

This compile step could also help us with the precision thing. I've been looking a lot on how to improve our shader precision, but almost all responses point to very performance-heavy solutions, that may be excusable if we have a compiled, faster way of doing things.

Here are the steps we'll have to take for this:

  • Converting a TokenOperator into it's corresponding GLSL function
  • Converting our operator_stack into an infix string
  • Creating a new Shader class for dynamically recompiling shaders
  • Switching between shaders at runtime
    • (Optional) Asynchronously waiting for the compiled shader to finish compiling before showing it

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions