Wednesday, 6 November 2013

Dynamic Compilation (Part 3)

Here we are... Again!

So in previous post I explained about reflecting functions, now one common thing I have is some form of post processors.

For most of them, they are pretty straightforward, a few render pass and bit of rebinding. Having a generic host is nice, but sometimes I need more sandboxing (so you can have a bit better internal optimization).

So if we take this basic shader (Yes my copyashtml is broken again ;)

Simple, just displace a texture from some FFT data, nothing fancy.

Now here is the c# code to glue this, here is the property class.

And here is the glue:

Nothing fancy, but when you have to bind this code for every shader you write, it quickly becomes cumbersome.

Please also note that this new post processing format it independent from both my tool and vvvv (technically I use another small code generator to build nodes from those class in my tool, I could do it as easily in vvvv but since my API is built in .NET 4.5 and vvvv prefers to support WindowsXP instead, it's not gonna happen right now ;)

As you can notice, this is again a lot of boilerplate code, that can easily be generated.

So instead, I have a small exe, you send the fx file to it, and it builds pretty much the same code from reflection (and give me the compiled shader on the way, so i can store it to speed up load time).

Here is a shader example (does nothing of any use):

And here is generated code

So basically I generate code from a shader file, then this code generates node code, uff ;)

If you are wondering, I'm not all in full for generating everything, there's a lot of cases where you don't want to.

Funnily, generating code can also make debugging easier, compared to a generic host, since most of your case handling has already be pre processed, you only end up with the very core logic (no crazy for loops/crazy dictionaries, if/then else to check a condition...), so You have a reasonable performance boost, which is not as big as if you do that "dream case cbuffer god of all", but we like dynamic languages no?

And for vvvv readers don't forget to bitch about .NET 4.5, so SharpDX in vvvv gets some more progress ;)