-
Notifications
You must be signed in to change notification settings - Fork 862
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
Call for input on HLSL -> SPIR-V translation #200
Comments
This picture shows the basic architectural plumbing of the new branch: |
Currently the architecture of glslang prevents 2 libraries in the same process from using glslang at the same time. (Each would have to be aware of the other in order to safely call Initialize/Finalize Process). This can cause enormously difficult to track down bugs in any programs that include, for example, glslang and shaderc. Can we prevent the hlsl backend from falling into the same pit. Specifically glslang/hlsl/hlslScanContext.cpp Line 80 in 48882ef
furthers this problem. Potential Solutions:
Which would initialize the KeywordMap only once, and then only destroy it at program exit. This way programs never have to deal with it.
May be preferable given that it has zero initialization cost and (this is just a gut feeling, would need to measure) the performance difference of doing binary search on a 100 item list vs hash + lookup would probably be virtually zero. At the very least
|
You can also create a perfect hash and then store it in a 128 sized static array. |
A third solution is to use a context object that is explicitly created by the client, and explicitly destroyed by the client. The context object owns all its state, never mixing with the state of another context. This is the preferred solution for APIs in LLVM, and OpenCL. It's very explicit, and hence has no surprises. |
HLSL has an #include feature. My team has been building out an #include feature for GLSL, adding needed bits to the Glslang preprocessor. It's nearly feature complete on the Glslang side with the latest pull request being #198. The client supplies an Includer object to resolve include requests to fully-resolved pathnames and file contents. We're building out the client side in various layers in https://github.com/google/shaderc We've only done #include "file" support, but no #include <stdfile> support. The hooks are there in the interfaces, but no Glslang preprocessor work has been done for it. |
Seems Microsoft announed at GDC 2016 that they are migrating from fxc shader compiler to one based on Clang/LLVM supporting HLSL SM5.x and 6.0 and they will open source the effort.. so perhaps better to work from there? coming fall 2016 |
If this includes a parser for HLSL, it might be valuable to this effort, except for being late.
As with GLSL, there may be small source language tweaks desired to communicate correctly with the Vulkan API. Hence, the desire to control our own parser. |
In that case wouldn't it just be better to fork Microsoft's HLSL parser? Correct me if I'm wrong but there currently doesn't seem to be any sort of reasonable LLVM backend for SPIR-V at the moment. Both LunarGLASS and SPIRV-LLVM have non-trivial shortcomings and a lack of a robust, easy to use toolchain for compiling to optimized SPIR-V seems like a major barrier to Vulkan adoption. With Microsoft apparently investing heavily in a new open source LLVM based HLSL compiler I would think that represents an opportunity to standardize on a common framework. Of course it would be better if somebody could convince them to open source sooner rather than later... |
Yes. Is it available?
Will that include the parser, or just post-parsed components? |
https://www.youtube.com/watch?v=dcDDvoauaz0&feature=youtu.be In that video he mentions that they will open source the new LLVM HLSL compiler for everyone within a year or 2, he also says within the 2nd half of this year, which is obviously not a particularly great timeline for anyone interested in adopting Vulkan using HLSL shaders sooner rather than later. That being said it sounds like it's already in active development so theoretically all you'd have to do is plugin a SPIR-V backend and problem solved. The engine that my company maintains has both D3D11/HLSL and OpenGL/GLSL rendering backends. We prefer working with HLSL and it would be great if we could move to Vulkan with HLSL regardless of whether or not it's using a custom parser or a fork of Microsoft's compiler - whenever that becomes generally available. However the bigger issue for us with Vulkan, regardless of HLSL vs GLSL, is that there is no easy way to go from source to optimized SPIR-V like there is with D3D12 and DXBC. |
Okay, thanks.
Yes, the SPIR-V ecosystem generally needs SPIR-V modules that can do SPIR-V -> SPIR-V transforms, orthogonally to what the source language was. I suspect a good way of creating such an optimizing module is So, I expect |
So is the plan here to modify the GLSL compiler to accept HLSL syntax? Is anything functional at the moment or is it just planning stages? |
The plan is to modify glslang to accept the HLSL syntax. The GLSL front end in glslang needs to stay as it is, with the specification grammar, to be the reference front end. However, the new recursive decent front end, aimed at HLSL, can be made to accept a hybrid/superset language, so that SPIR-V features can be properly controlled from the HLL.
It's past the planning stages (unless this conversation uncovers something), but only works for trivial shaders. Next step is broadening the functionality. Recap: Basic plan is to provide, in glslang, a |
I see. Is there a grammer defined somewhere? How is it being implemented? HLSL uses (or used to) use yacc/bison for parsing. I think I still remember most of the grammer for it. I'll try to setup a git client and take a look soon myself. |
If we had a real grammar definition that was a correct superset of what we want to do today plus what is coming next, that would be great, and I would use it. Barring that, a guess at a bison grammar risks being painted into a corner. I found, so far, the recursive decent I set up is very nice to work with, without reduce-reduce conflicts to resolve, and theoretically no constraints on looking ahead, and being able to give meaning error messages rather than "syntax error". I would highly value any input you can freely give on the correct syntax and (especially) semantics of HLSL. Even a short bulleted list of the major differences in GLSL and HLSL semantics, that a normal developer would know, for the same syntax would be useful. But, whatever you can share would be great. One important note here: because there is no goal to reject invalid HLSL shaders, we can skip all the validation of syntax, so the grammar problem is also greatly simplified due to that. |
Ok, I'll start by taking a look at what's there. I haven't much experience with recursive decent parsers so it will prob take me some time to wrap my head around it. GLSL and HLSL are not very different in grammer, honestly. The big difference is HLSL has a more general type definition, (e.g. float1s are essentially the same as floats), more casting promotion/demotions, and the sampler/texture stuff is pretty different. I think if you have something accepting GLSL code it wouldn't take more then a month or 2 of mods to accept most HLSL code. |
Is there a good (complete and precise) spec for HLSL? Microsoft's site has a lot of tutorial-style documentation but it's not quite to level where I'd be confident writing a compiler for it. |
@dneto0 one quick google search later: there seems to be a spread out grammar description at https://msdn.microsoft.com/en-us/library/windows/desktop/bb509615%28v=vs.85%29.aspx# how complete it is I don't know. |
@ratchetfreak Agreed. That and some other pages gives grammar level information. I'm also concerned about stuff like implicit conversions. What conversions are automatic? Where do they take place? e.g. assignment, arithmetic expressions, in parameter passing. Lack of that kind of detail makes me less confident I'd be able to write a correct compiler for HLSL. A big help would be to see a statement like: "It's just like C except for ....". Then you can build on all the reference material for C or whatever base language. |
HLSL Grammer is basically c Grammer rules when possible. I could write out a bnf of the expression parsing if people needed it.
|
My guess of the most difficult area to get right is selection of the right overloaded function under implicit conversion when there is no exact match. But, the front end could issue a request to disambiguate rather than guessing wrong. For normal expressions, knowing the graph of implicit conversions and their priority is enough to get it right. In GLSL it's an unambiguous dag. In C++ it's more complex and cyclic. |
Yup, that the area that gets tricky. But it's also not a super critical There are just lots of little details that can get you such as * is always At any rate, I do remember the rules for the finding the best fit On Thu, Apr 21, 2016 at 11:51 AM, John Kessenich notifications@github.com
|
I recently did a few ad-hoc experiments around that. It appears to me that floats and float1s are silently implicitly convertible in both directions, but are different underlying types. That is to say, myfunc(float) and myfunc(float1) act like different function signatures, and both overloads can exist simultaneously in the same source without error. (I do not know if that behavior is shader model specific). However, attempting to call one of the functions resulted in an "ambiguous function" error at the call site, presumably due to the implicit conversions. I don't know enough about HLSL to know whether a way to explicitly disambiguate them exists. Of course, subtleties abound, and it's easy for an experiment to show something different than it seems like it does. |
Thanks, this will be a great help. |
Hello. Can anybody describe current status of hlsl support? Do you plan support modern hlsl syntax or only legacy DX9-style? Thank you. |
Hi Aiprog, There is work happening on HLSL support in multiple areas from different people. About textures and samplers in particular: I am working on that area now, but have just started and do not have anything ready yet. I plan to add the DX10 "method" style first with separate declarations of samplers and textures, although most likely this will come with limitations. However I don't know of any reason DX9 style cannot also be added. |
Sounds good. Thank you! |
What are people currently working on? I am ready to drive into Vulkan support this week. Getting close to the point of looking at shader conversion, but I don't want to work in an area where others have made significant progress. I haven't yet downloaded this project and compiled it, is there a link or readme on how to do that? From looking through what's implemented, I think the #1 thing that will hold us back right now is the method based texture sampling. Thanks, |
I have a pending PR #380 (code review changes happening at the moment) which has the underpinnings for method based texture sampling. It is a narrow vertical path, but once that is in place it should be possible to broaden it fairly easily. I have some of that broadening work on my disk currently. In a few cases I know of nothing to map a feature to. E.g: immediate sampler state, such as:
AFAIK, the immediates require API based setup, and there's no defined path for this. The above PR parses the immediate state list, ignores it, and issues a warning in the compilation log. Thoughts about this are welcome... |
See issue #362. It makes some sense to ask or "claiming" one of those with a comment on that issue. Also, do you have any input about features you want to see that are missing from the list? Overall, the project will get driven from a perspective of what's in real workloads, so contributing from that direction makes sense. I think @steve-lunarg has a good handle on texturing-related stuff, and I've got the grammar covered, but other things are not yet claimed. Definitely read the readme before starting. |
About literal samplers, would be interested in hearing about needs for this, as SPIR-V currently only supports that for OpenCL, not for Vulkan. |
In terms of literal samplers, some of the 'HLSL' syntax is actually part of the D3D effects systems syntax, which is a library add-on. It's mostly deprecated at this point. Thus, it would be ignored by the main D3D11 runtime, or handled awkwardly (I can't recall how it's handled off hand, but I think it will just show up as reflection data). D3D12 doesn't have this system at all AFAIK. I have an entire application of shaders to try (maybe ~10,000 lines of shader code), my plan was to try to get some shaders through and see where there are missing areas. |
@dankbaker, I am interested in hearing about this:
Note that we have an open issue for the GLSL 4.0 variant of this question in issue #142, and one question is whether those rules would also be consistent with HLSL. |
Btw, is anyone around at siggraph for a f2f?
|
Hey all HLSL Shader Model 6 has been released: https://msdn.microsoft.com/en-us/library/windows/desktop/mt733232(v=vs.85).aspx |
So I was trying to commit a tiny change just to see if I had write permission, but it didn't work. Is there anything I have to do? |
@dankbaker You should make your own fork and then submit a Pull Request. |
Ok, I took a stab at it. Let me know if it works. It's a tiny change just to test out my GIT skills, I have next to no experience with GIT. My whole career has been p4. |
@dankbaker Looks good so far... it's currently getting run through some bots to make sure it's not breaking something. (Change looks good to me too, on code reading.) When the bots turn green, I'll merge it in. |
Is there a standalone app included that generates an output SPIRV similar to fxc or do I have to make my own? All I see is the glslangvalidator |
|
Any updates? |
I thought this would continue to be discussed here along with the development process. Keep up the good work! |
Hi, I'm quite interested in the topic that glslang could support HLSL. As I tried to make a simple HLSL shader to replace my GLSL shader, it seems that currently the key issue has not been ready even for a very simple shader. The shader output could not be placed to the correct slot according to the semantics. For example: layout (location = 0) out vec2 texcoord; out gl_PerVertex void main()
} SPV: and my corresponding HLSL shader: Firstly, I had found that currently glslang could not parse the semantic correctly, so I simply modified the source code to satisfy my requirement:
} Thus, the SPV of the HLSL shader becomes: It is almost as same as the GLSL version, except that Even I used "out", the SPV instructions are still input. |
Closing this, as issues can now be handled as specific individual issues. Thanks for all the input! |
@johnkslang |
Yes, this should work. If you can build glslangValidator, HLSL support is included automatically, accessed via the -D option. There are many other options, see the usage statement. There are some pending features, but lots of HLSL shaders now work. |
@johnkslang |
See issue #362. |
…ce-odr-spv-khr-expect-assume Support SPV_KHR_linkonce_odr and SPV_KHR_expect_assume
There have been multiple requests for HLSL -> SPIR-V translation. Given the likelihood of multiple ways of doing this and multiple parties interested in it, this is a place to discuss.
One possible way of doing it is introduced in the hlsl-frontend branch. If this method looks good, it needs to be finished by completing all the HLSL grammar details, translating it into the same AST that GLSL is translated into. The grammar -> AST does not have much functionality yet, but is otherwise set up with relatively complete infrastructure throughout glslang -> SPIR-V.
See the commits at https://github.com/KhronosGroup/glslang/commits/hlsl-frontend, summarized below. The initial commits are less HLSL-specific and might be good steps to take regardless.
The text was updated successfully, but these errors were encountered: