korge icon indicating copy to clipboard operation
korge copied to clipboard

Compute Shaders - Shader Compilation from Kotlin code (no DSL)

Open soywiz opened this issue 2 years ago • 0 comments

Instead of using a DSL to declare functions, why not use a reduced Kotlin runtime and use Kotlin functions directly?

C# have expression trees: https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/expression-trees/

That can use to have access to some of the code AST at runtime, that allows to generate code from that AST, but still be able to execute that code directly.

In the case of Compute Shaders this would be specially useful. So for example:

@AST
fun add(a: Vector3, b: Vector3): Vector3 {
  return a + b
}

Would be awesome if kotlin supported this, but we could implement this via Kotlin Compiler plugin. If we expose the AST to people, this scenario can be viable, and would allow other people from benefit from it too.

This can also be used in other scenarios like for example LINQ in C# that allows to generate SQL code directly from code without workarounds like pseudo-DSLs.

With Compute Shaders we could convert referenced code into glsl or the relevant shading language metal, webgpu or whatever, but also execute it in the CPU for both debugging, troubleshooting but also to execute them in platforms not supporting compute shaders.

soywiz avatar Jun 14 '23 02:06 soywiz