Show HN: Metashade – a Pythonic GPU shading/compute EDSL

47 points
1/20/1970
12 days ago
by ppenenko

Comments


leguminous

I was using Taichi recently to prototype some fragment shaders (I just mocked out texelFetch, texture, etc and wrote a for loop over each pixel). I found it convenient to be able to run on the CPU and also not have to deal with any OpenGL or Vulkan setup.

One thing that was frustrating was that I couldn't template a function with another function (as far as I know). That lead to some copied and pasted code when I had multiple implementations that I wanted to compare. Can Metashade do that?

11 days ago

ppenenko

Agreed that the ability to just run on the CPU is valuable. Metashade doesn't support that yet and its codegen syntax doesn't look like regular Python code (everything codegen-related is prefixed with `sh.` etc.) but it's certainly possible to write a generator that would just execute the code "in the immediate mode" or generate C/C++ code for the CPU.

Regarding templating functions with functions - in Metashade you can just specialize the generated code however you see fit, with Python as the meta language. E.g. Python's `if` statements can act like `#ifdef`s or `if constexpr`, and you can certainly pass around callables to parameterize behavior.

10 days ago

raytopia

Super interesting project. Been looking into template generation and uber shaders to make coding shaders easier for the game engine Ursina. I'm going to bring this to their attention.

Would it be easy to add another target like glsl?

11 days ago

ppenenko

Thanks!

Multiple targets is definitely the plan, and GLSL is my next priority. Metashade can currently generate HLSL for the DX12 version of https://github.com/ppenenko/glTFSample/tree/metashade_demo, and there's also a Vulkan version of that demo using GLSL. So implementing GLSL generation for that would be a great proof of concept and a test bed: HLSL and GLSL generated from single source and producing identical rendering results.

The diagram on slide 31 of my presentation shows how implementation is currently inherited between the Metashade packages. The future GLSL generators should inherit common functionality from the "rtsl" package, just like the existing HLSL generators. However, I expect heavy refactoring to be necessary because I was initially targeting just one language and so not all code is implemented at the appropriate level.

BTW, here's a poll where you can vote for a target language you'd like to see prioritized: https://github.com/ppenenko/metashade/discussions/17

9 days ago

kookamamie

Wouldn't jinja templates would have sufficed for this? I'm not sure it's a good idea to create a whole new language just for getting templating capabilities to shaders.

11 days ago

ppenenko

No, I don't believe Jinja would suffice. For starters, how would you abstract out the syntax differences between, say, HLSL and WGSL? Jinja's approach seems to be taking the syntax of the target language and embedding templating into it. But with Metashade, we're replacing the different syntaxes of individual target languages with that of Python. Since this approach is largely agnostic of the target language syntax, new targets can be added in the future without rewriting target-independent, polymorphic code implementing rendering or compute techniques.

10 days ago

pixelpoet

Hi Pavlo, nice to see you here and thanks for the presentation at Maxon :)

11 days ago

ppenenko

Cheers Thomas!

10 days ago

2genders28507

[dead]

11 days ago

SEXMCNIGGA28386

[dead]

11 days ago

SEXMCNIGGA47335

[dead]

11 days ago

SEXMCNIGGA3291

[dead]

11 days ago

SEXMCNIGGA19574

[dead]

11 days ago

SEXMCNIGGA43215

[dead]

11 days ago

SEXMCNIGGA20543

[dead]

11 days ago

SEXMCNIGGA28322

[dead]

11 days ago

SEXMCNIGGA6701

[dead]

11 days ago

SEXMCNIGGA26902

[dead]

11 days ago

SEXMCNIGGA28854

[dead]

11 days ago

2genders14637

[dead]

11 days ago

SEXMCNIGGA14392

[dead]

11 days ago

2genders8765

[dead]

11 days ago

SEXMCNIGGA16009

[dead]

11 days ago

SEXMCNIGGA36472

[dead]

11 days ago

2genders22238

[dead]

11 days ago

2genders777

[dead]

11 days ago

2genders26709

[dead]

11 days ago

2genders5056

[dead]

11 days ago

2genders9531

[flagged]

11 days ago

2genders22942

[flagged]

11 days ago

2genders1206

[flagged]

11 days ago

2genders47148

[flagged]

11 days ago

2genders839

[flagged]

11 days ago

sexmc25878

[flagged]

11 days ago

sexmc22797

[flagged]

11 days ago

sexmc22464

[flagged]

11 days ago

sexmc7261

[flagged]

11 days ago

sexmc44307

[flagged]

11 days ago

sexmc31107

[flagged]

11 days ago

indianmilf23186

[flagged]

11 days ago

indianmilf40012

[flagged]

11 days ago

sexmc12681

[flagged]

11 days ago

sexmc34063

[flagged]

11 days ago

indianmilf7474

[flagged]

11 days ago

sexmc33282

[flagged]

11 days ago

sexmc47940

[flagged]

11 days ago

sexmc34509

[flagged]

11 days ago

SEXMCNIGGA36557

[flagged]

11 days ago

2genders32561

[flagged]

11 days ago

2genders47272

[flagged]

11 days ago

2genders38930

[flagged]

11 days ago

2genders23289

[flagged]

11 days ago

2genders25297

[flagged]

11 days ago

2genders45764

[flagged]

11 days ago

2genders3617

[flagged]

11 days ago

sexmc33999

[flagged]

11 days ago

2genders44794

[flagged]

11 days ago

2genders40320

[flagged]

11 days ago

2genders20975

[flagged]

11 days ago

2genders26728

[flagged]

11 days ago

sexmc17979

[flagged]

11 days ago

sexmc114

[flagged]

11 days ago

sexmc17627

[flagged]

11 days ago

2genders2516

[flagged]

11 days ago

2genders23779

[flagged]

11 days ago

2genders12074

[flagged]

11 days ago

SEXMCNIGGA29133

[flagged]

11 days ago

SEXMCNIGGA32816

[flagged]

11 days ago

2genders37574

[flagged]

11 days ago

2genders3648

[flagged]

11 days ago

2genders30187

[flagged]

11 days ago

2genders13469

[flagged]

11 days ago

2genders49246

[flagged]

11 days ago

2genders44424

[flagged]

11 days ago

SEXMCNIGGA5453

[flagged]

11 days ago

2genders28563

[flagged]

11 days ago

2genders14678

[flagged]

11 days ago

2genders31161

[flagged]

11 days ago

SEXMCNIGGA45535

[flagged]

11 days ago

SEXMCNIGGA7323

[flagged]

11 days ago

2genders34771

[flagged]

11 days ago

2genders9639

[flagged]

11 days ago