Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for compiling to C/C++ header #214

Closed
Themaister opened this issue May 8, 2016 · 9 comments
Closed

Support for compiling to C/C++ header #214

Themaister opened this issue May 8, 2016 · 9 comments
Assignees

Comments

@Themaister
Copy link

For many applications, it would be very convenient to be able to compile GLSL directly to a SPIR-V encoded as a static const uint32_t[].

In smaller projects, and especially for embedded/mobile, it is very useful to bake the SPIR-V shaders directly into the binary.

@dekimir
Copy link

dekimir commented May 9, 2016

Shaderc follows closely the glslang design here, and glslang only has functions to compile into a vector<unsigned int> for now.

@Themaister
Copy link
Author

True, but emitting a C header is essentially just a different way of dumping the u32 vector to disk, so it shouldn't affect glslang at all. It is standalone enough that this could even be a separate binary.

@dekimir
Copy link

dekimir commented May 9, 2016

Sorry, I thought you were proposing a libshaderc interface extension. But it sounds like you just want to take the spv_binary and print its elements into a .h file? That's doable in ~4 lines of code, I think. Or are you proposing that we add a flag to glslc to do it automatically?

@Themaister
Copy link
Author

Themaister commented May 9, 2016

I was thinking about adding some flags to glslc to dump out a header instead of raw .spv.
Again, trivial to do, but it's very convenient.

@dekimir
Copy link

dekimir commented May 9, 2016

@Themaister: makes sense. We'll come up with a suitable flag name and implement.

@dneto0
Copy link
Collaborator

dneto0 commented May 11, 2016

I think we should mimic GCC's -masm=dialect option.
https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#index-masm_003d_0040var_007bdialect_007d-2832

So you'd use -S to emit assembly and -masm=c to say you want C source.

One difficulty is in saying what variable name you want for the result. You might want to do this with several binaries.
So how about we just emit a brace-initialization list of values, and it's up to you to wrap the result.
eg. the output could be

  {  0x07230203, 0x00010000, 0x00080000, 1, 0 }

And you'd put that into a "binary.inc"
Then in your C code (or C++ code) you'd say

static uint32_t empty_binary[] = 
#include "binary.inc"
;

The point is that all the decisions about integrating it into the rest of your program are left to you, rather than adding lots of config parameters to glslc.

In this case I'd be tempted to use -masm=c-init-list as the option.

@Themaister
Copy link
Author

Themaister commented May 12, 2016

That's a solid solution. It is indeed nice to avoid having to specify the array name on CLI, and it's also nice to avoid having to specify static/constness of the array in question. It can also be used as an initializer list in C++11 that way too.

@antiagainst
Copy link
Contributor

#218 is landed. Closing.

@Themaister
Copy link
Author

Works great. Thanks :)

Kangz pushed a commit to Kangz/shaderc that referenced this issue Sep 3, 2019
I know there was some churn on which version of this shader was correct - but right now, this goes from broken on all 4 backends to working on all 4 backends. So this seems better.

The old code (accidentally?) uses descriptor arrays (I think), which are not trivial to support on all backends, so we won't use them for now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants