gboisse / gfx

A minimalist and easy to use graphics API.
MIT License
502 stars 36 forks source link

Pass shader defines in gfxCreate**Kernel #41

Closed sayan1an closed 2 years ago

sayan1an commented 2 years ago

Hi,

This seems very trivial but I cannot set the value of a define when passing as compiler argument.

For example, how would you pass the following define as a createKernelargument?

#define MYDEFINE 64

sayan1an commented 2 years ago

Found a hacky way to achieve this. Passing "MYDEFINE 64 //" seems to achieve what I was looking for.

const char *defines[] = {"MYDEFINE 64 //"};
kernel = create**Kernel(context, program, "entryMain", defines, 1);
gboisse commented 2 years ago

Glad you got it figured out 🙂

Is the // required out of curiosity? Seems a bit strange that you'd have to put those in...

Under the hood, gfx does nothing more than passing those arguments to DXC: https://github.com/microsoft/DirectXShaderCompiler

gboisse commented 2 years ago

Here specifically: https://github.com/gboisse/gfx/blob/68b78ae6def0267a85f794bfb468e388789d8e5f/gfx.h#L7168-L7188

Note the call to dxc_compiler_->Compile() at the end.

sayan1an commented 2 years ago

I think the problem I'm having is the compiler is automatically assigning a value to my defines. E.g. if I pass const char *defines[] = {"MYDEFINE 64"};

as the compiler argument, the compiler defines it as:

#define MYDEFINE 64 1

in the shader. Bizarre! So putting a "//" in the end makes the issue go away, i.e.

#define MYDEFINE 64// 1

gboisse commented 2 years ago

Pushed a fix directly inside gfx for this: https://github.com/gboisse/gfx/commit/0550bbd7b91643baf33d18e4015042f7cf93b728

DXC adds a 1 automatically to the define (so #define MY_VAR 1). Now it'll be #define MY_VAR// 1 and users can supply their own values without that weird workaround.

Thanks for pointing that one out 🙂