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 0->1 depth range in GLSL #76

Open
mellinoe opened this issue May 21, 2018 · 2 comments
Open

Support 0->1 depth range in GLSL #76

mellinoe opened this issue May 21, 2018 · 2 comments

Comments

@mellinoe
Copy link
Owner

mellinoe commented May 21, 2018

In order to support the reverse-depth-buffer optimization, I would like to support a 0->1 depth range, even in GLSL. Given that the glClipControl extension isn't universally supported, and given that folks will still likely use the default depth range in OpenGL, we should support both depth ranges somehow. Also, OpenGL ES and WebGL do not support glClipControl at all. Perhaps the best way to accomplish this is conditional compilation -- if DEPTH_RANGE_ZERO_TO_ONE is defined, we will not transform the depth range. Otherwise we will, as we do today.

We should think about how such a preprocessor define could be utilized in other intrinsic functions that could allow you to write code that is portable to both depth range schemes.

@tgjones Any thoughts here?

@tgjones
Copy link
Contributor

tgjones commented May 21, 2018

With DEPTH_RANGE_ZERO_TO_ONE, did you mean that would be a C# #define?

A more discoverable API might be to have it as a property on the [VertexShader] attribute? It could be a DepthRangeTransform enum, defaulting to the current behaviour.

I'm trying to think of other intrinsic functions that could make use of this and coming up blank. That's probably just because it's late :) It does seem like a useful piece of metadata to have.

@mellinoe
Copy link
Owner Author

mellinoe commented May 21, 2018

With DEPTH_RANGE_ZERO_TO_ONE, did you mean that would be a C# #define?

In my mind, it would be a preprocessor condition that is used but not defined in the shader code, e.g.:

gl_Position = output_.Position;
#if !DEPTH_RANGE_ZERO_TO_ONE
gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;
#endif

By default, nothing would be change, but you could do something like this on the C# side:

string defines = string.Empty;
if (GraphicsDevice.IsDepthRangeZeroToOne)
{
    defines = "DEPTH_RANGE_ZERO_TO_ONE";
}

byte[] shaderBytes = LoadShader("myShader.vert", defines); // Add defines into the text and return bytes

(Prototype of that API in this Veldrid branch)

A more discoverable API might be to have it as a property on the [VertexShader] attribute? It could be a DepthRangeTransform enum, defaulting to the current behaviour.

I'm trying to think of a way that you can write your shader in a "portable" way, such that it will work under both depth range schemes. If we had a more robust "configuration system", perhaps you could just tell ShaderGen to spit out both versions of the shader -- but that's much more complicated.

I'm trying to think of other intrinsic functions that could make use of this and coming up blank.

I think there might need to be some kind of builtin function that does depth comparisons for you, based on whether or not that define is set.

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

2 participants