Skip to content

Hull, Domain and Geometry Shaders for DirectX and OpenGL#7352

Closed
cpt-max wants to merge 62 commits intoMonoGame:developfrom
cpt-max:glshaderstages
Closed

Hull, Domain and Geometry Shaders for DirectX and OpenGL#7352
cpt-max wants to merge 62 commits intoMonoGame:developfrom
cpt-max:glshaderstages

Conversation

@cpt-max
Copy link
Copy Markdown
Contributor

@cpt-max cpt-max commented Sep 9, 2020

This PR is based on my previous PR #7242 which was DirectX only. Now those new shader stages also work in OpenGL. A switch from MojoShader to ShaderConductor was neccessary to make this happen, so PR #7345 is also included.

Here you find a sample project containing a simple Hull, Domain and Geometry shader.
This shader migration document explains what changes need to be made to old shader code.
Here are some more details about the shader model support in OpenGL.

Anyone wanting to try out this branch, please use #7533 instead. It fully contains this branch and is kept up to date.

cpt-max and others added 30 commits July 20, 2020 05:26
…arameter updating when uniform buffers are not available.
@minimalism
Copy link
Copy Markdown
Contributor

minimalism commented Mar 18, 2021

Thought I should mention I couldn't get this to build shaders on my minspec GPUs (an Intel HD4000 and a Radeon M9 370X, the Macbook 2015 one) even though they ostensibly support OpenGL>4.0, but in reality have poor driver support. I wasn't able to get any relevant error messages.
I also struggled to downgrade my shaders to SM3, repeatedly running into various unimplemented features in SPIRV - for example, the % operator wouldn't build because it insisted on translating them to the unimplemented Frem function instead of just falling back to Fmod. I got the impression that SPIRV is a bit lacking in supporting legacy GL/SM, but there's been a lot of work done since then so it might be time to try again.

Apologies for the unconstructive reply but I imagine most folks will be focusing on testing the newer shader models and GL4 features which SPIRV appears to handle well, but I think many monogame devs want to support older hardware too. I wasn't able to do so without reverting back to mojoshader for those devices, which is quite reliable when it comes to building legacy shaders. I'll give this another try with the latest ShaderConductor and SPIRV builds soon and hopefully come up with some solutions.

@cpt-max
Copy link
Copy Markdown
Contributor Author

cpt-max commented Mar 19, 2021

Thought I should mention I couldn't get this to build shaders on my minspec GPUs (an Intel HD4000 and a Radeon M9 370X, the Macbook 2015 one)

It fails at runtime, not during content building, right? I assume you get an exception in Shader.GetShaderHandle() in Shader.OpenGL.cs. You really get nothing useful from GL.GetShaderInfoLog()?

I got the impression that SPIRV is a bit lacking in supporting legacy GL/SM

You got the right impression. With ShaderConductor you really want to use shader model 4 or higher. Shader model 2 and 3 will map to OpenGL 2, which is just not that well supported.
I managed to get the default shaders working for OpenGL 2, so I guess it's not completely useless, but you definitely don't want to use it for extensive shader development.

I think many monogame devs want to support older hardware too.

All you really need is OpenGL 3.3 (released in 2010), you really think there's a lot of demand for even older hardware?

@ThomasFOG ThomasFOG added this to the 3.9 Release milestone May 24, 2021
cpt-max added 3 commits June 27, 2021 13:26
Fixed NullReferenceException in EffectParameter's  SetMatrixArray and SetMatrixArrayTransposed methods.
@cpt-max
Copy link
Copy Markdown
Contributor Author

cpt-max commented Jun 27, 2021

A little update, as I finally have some time for MonoGame stuff again. I addressed a few issues mentioned above:

  • Updated to newest MonoGame version.
  • Improved error and warning output.
  • Merged @eylvisaker fix for matrix array parameters (sorry, I totally missed this PR when you first made it).
  • Updated MGCB editor template effects to work with ShaderConductor.
  • Looked into the float4x4 issue with vertex shader inputs:

The root of the float4x4 problem is this:
The C# vertex structure contains multiple separate vectors to hold the entire matrix. If you do the same in the vertex shader (use multiple vectors instead of a single matrix) everything is fine, because now there is match between vertex elements and shader inputs.
However, if you use a single matrix parameter for the vertex shader, it won't work, because now vertex elements and shader inputs don't match. This worked with MojoShader, because it would split the matrix parameter into vectors automatically. ShaderConductor doesn't do that, it will create a single matrix parameter:

layout(location = 3) in mat4 in_var_BLENDWEIGHT0;

The easiest solution would be if ShaderConductor could flatten the matrix, just like MojoShader does. I made a request on the SPIRV-Cross GitHub.

@cpt-max
Copy link
Copy Markdown
Contributor Author

cpt-max commented Jun 28, 2021

I just noticed that ShaderConductor actually has a flag to switch between row-major and column-major matrices, this allowed me to remove the manual matrix transposing for OpenGL. I could slap myself for not seeing this earlier.

@cpt-max
Copy link
Copy Markdown
Contributor Author

cpt-max commented Jun 30, 2021

Matrices should work now as vertex shader inputs. I had to store the matrix size in the effects file for this, so effects need to be recompiled.

Unfortunately I found a problem with the row-major/column-major switch I made recently (7fc7d9b)
It only works with square matrices currently (KhronosGroup/SPIRV-Cross#1702). I may have to revert this.
SkinnedEffect.fx from the default shaders fails to compile because of this, it uses a 4x3 matrix.

@cpt-max
Copy link
Copy Markdown
Contributor Author

cpt-max commented Jul 12, 2021

Apparently the square matrix limitation only applies to OpenGL 2 (shader models 2 and 3).
Support for those shader models isn't that great anyway right now, so I'm not planning to look for a solution to this. I added the limitation to the migration document, and changed the SkinnedEffect shader, so it doesn't use non-square matrices anymore.

@ThomasFOG
Copy link
Copy Markdown
Contributor

Thank you again for all this work! It's a big and important chunk which will also have effects on the console implementations.

3.8.1 should be releasing soon, so one of the next goals will be reviewing this PR.

@cpt-max cpt-max mentioned this pull request Jul 15, 2021
@MrScautHD
Copy link
Copy Markdown

i can not wait anymore :( it looks so amazing!

@ThomasFOG ThomasFOG modified the milestones: 3.9.0, 5.0 Apr 23, 2024
@cpt-max
Copy link
Copy Markdown
Contributor Author

cpt-max commented May 12, 2024

Closing this PR, as it's contained in #7533

@cpt-max cpt-max closed this May 12, 2024
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

Successfully merging this pull request may close these issues.

7 participants