Skip to content

Commit e8f313a

Browse files
committed
Adjusted framebuffer size and rotation shader variables and functions.
Framebuffer size, both pre and post rotation, and framebuffer rotation are now available through dsInstanceTransformData and dsViewFramebufferData to allow them to change based on the bound framebuffer. dsInstanceTransformData allows it to be accessed for the common case without additional variables, dsViewFramebufferData maintains one copy of the values for all instances for a group that is drawn together. Added separate uniforms for dsShadowInstanceTransformData that omits the framebuffer elements. Renamed various transformation functions to be more consistent. Shared implementations are now provided in CoordinateHelpers.mslh where parameters may come from different uniforms. Going from clip to view space no longer adjusts the clip. This avoids duplicate adjustments, which can also differ based on where the coordinates came from. (projection matrix or framebuffer position) Adjusted the internal uniforms for OpenGL to reduce the amount of work required to adjust the fragment coordinates and have more info available for adjusting the clips. This now requires two vec4 uniforms, but should be faster at runtime and provide more flexibility. Removed DS_ADJUST_TEXTURE_CLIP() macro and removed the dsViewTransformData variable for the texture transform. Defines are now used for the projection texture transforms for standard situations for shadow mapping and SSAO.
1 parent 7a063bd commit e8f313a

178 files changed

Lines changed: 2331 additions & 899 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/Render/Render/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ DeepSea uses [Modular Shader Language](https://github.com/akb825/ModularShaderLa
2727
* gl_FragCoord is always in the upper-left.
2828
* Use the `DS_ADJUST_CLIP(x)` macro with the clip position (such as that assigned to gl_Position) to ensure the clip position x is transformed to the correct space.
2929
* If using a direct clip position without a projection matrix, use `DS_ADJUST_DIRECT_CLIP(x)` instead.
30-
* If used as a guaranteed texture target, such as a texture projection, use `DS_ADJUST_TEXTURE_CLIP(x)` instead.
3130
* When support for older hardware with OpenGL 2.x, use the `DS_RG_SWZL` macro for RG-format textures. (e.g. `color.DS_RG_SWZL`) This ensures compatibility with targets that use luminance-alpha textures instead.
3231
* Subpass inputs may always be used. Defines are provided to fall back to a standard texture lookup on targets that don't support them directly.
3332
* When declaring subpass inputs, instead of using `layout(input_attachment_index = i) uniform subpassInput input`, declare as `inputAttachment(i) input`.
@@ -75,6 +74,8 @@ Most render states are set by declaring the render state in the shader pipeline.
7574
The following built-in defines are available when writing shaders:
7675

7776
* `DS_MIN_CLIP_Z`: The minimum Z value in clip space. In most cases this is 0.0, but for OpenGL it may be -1.0 or 0.0 depending on whether the `preferHalfDepthRange` renderer option is enabled and the device supports adjusting the clip range.
77+
* `DS_CLIP_Z_TO_DEPTH_MUL` and `DS_CLIP_Z_TO_DEPTH_ADD`: The multiplication and addition factors to convert from clip z to depth values, applied as `z*DS_CLIP_Z_TO_DEPTH_MUL + DS_CLIP_Z_TO_DEPTH_ADD`.
78+
* `DS_DEPTH_TO_CLIP_Z_MUL` and `DS_DEPTH_TO_CLIP_Z_ADD`: The multiplication and addition factors to convert from depth values to clip z, applied as `z*DS_DEPTH_TO_CLIP_Z_MUL + DS_DEPTH_TO_CLIP_Z_ADD`.
7879
* `DS_ADJUST_CLIP(v)`: Call when assigning `gl_Position` at the end of the vertex shader to adjust for the current system's clip space. It's assumed that the position was computed with the projection matrix.
7980
* `DS_ADJUST_DIRECT_CLIP(v)`: Same as `DS_ADJUST_CLIP`, except for creating the position directly in clip space rather than using the projection matrix.
8081
* `DS_RG_SWZL`: Swizzle for RG channel textures. This takes into account older versions of OpenGL that use lumanance alpha for two-channel textures, which requires `ra` rather than `rg`.

modules/Render/Render/include/DeepSea/Render/RenderSurface.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ DS_RENDER_EXPORT bool dsRenderSurface_makeRotationMatrix44(
6666
* @param viewport The viewport to rotate. This may be the same as result.
6767
* @param width The width of the physical surface.
6868
* @param height The height of the physical surface.
69+
* @param rotation The rotation to apply.
6970
* @return False if the parameters are invalid.
7071
*/
7172
DS_RENDER_EXPORT bool dsRenderSurface_rotateViewport(dsAlignedBox3f* result,
@@ -76,9 +77,10 @@ DS_RENDER_EXPORT bool dsRenderSurface_rotateViewport(dsAlignedBox3f* result,
7677
* @brief Rotates a scissor box for the render surface rotation.
7778
* @remark errno will be set on failure.
7879
* @param[out] result The rotated scissor.
80+
* @param scissor The scissor to rotate. This may be the same as result.
7981
* @param width The width of the physical surface.
8082
* @param height The height of the physical surface.
81-
* @param scissor The scissor to rotate. This may be the same as result.
83+
* @param rotation The rotation to apply.
8284
* @return False if the parameters are invalid.
8385
*/
8486
DS_RENDER_EXPORT bool dsRenderSurface_rotateScissor(dsAlignedBox2f* result,

modules/Render/Render/include/DeepSea/Render/Shaders/CoordinateHelpers.mslh

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Aaron Barany
2+
* Copyright 2022-2026 Aaron Barany
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,45 @@
2121
* @brief Functions to help with different coordinate transformations.
2222
*/
2323

24+
#if SPIRV_VERSION
25+
/**
26+
* @brief Multiplier factor from clip coordinates to projected texture coordinates.
27+
*/
28+
#define DS_CLIP_TO_PROJ_TEX_COORDS_MUL vec3(0.5, 0.5, DS_CLIP_Z_TO_DEPTH_MUL)
29+
30+
/**
31+
* @brief Addition factor from clip coordinates to projected texture coordinates.
32+
*/
33+
#define DS_CLIP_TO_PROJ_TEX_COORDS_ADD vec3(0.5, 0.5, DS_CLIP_Z_TO_DEPTH_ADD)
34+
#else
35+
#define DS_CLIP_TO_PROJ_TEX_COORDS_MUL vec3(0.5, -0.5, DS_CLIP_Z_TO_DEPTH_MUL)
36+
#define DS_CLIP_TO_PROJ_TEX_COORDS_ADD vec3(0.5, 0.5, DS_CLIP_Z_TO_DEPTH_ADD)
37+
#endif
38+
2439
/**
25-
* @brief Transforms from clip coordinates in the common [-1, -1] as lower left to texture
40+
* @brief Array with the multiplier and addition afctors from clip coordinates to projected
41+
* texture coordinates.
42+
*/
43+
#define DS_CLIP_TO_PROJ_TEX_COORDS \
44+
vec3[2](DS_CLIP_TO_PROJ_TEX_COORDS_MUL, DS_CLIP_TO_PROJ_TEX_COORDS_ADD)
45+
46+
/**
47+
* @brief Toggles between common clip coordinates, where [-1, -1] is the lower-left, and direct
48+
* clip space where the origin depends on the current graphics environment.
49+
* @param clipCoords The clip coordinates.
50+
* @return The adjusted clip coordinates, toggling between common and device-specific coordinates.
51+
*/
52+
vec4 dsToggleCommonClip(vec4 clipCoords)
53+
{
54+
#if SPIRV_VERSION
55+
return vec4(clipCoords.x, -clipCoords.y, clipCoords.zw);
56+
#else
57+
return clipCoords;
58+
#endif
59+
}
60+
61+
/**
62+
* @brief Transforms from common clip coordinates, where [-1, -1] is the lower-left, to texture
2663
* coordinates.
2764
*
2865
* This is typically used for cases where the clip coordinates are passed directly as vertex
@@ -34,7 +71,7 @@
3471
*/
3572
vec2 dsCommonClipToTexCoords(vec2 clipCoords)
3673
{
37-
return vec2(clipCoords.x, -clipCoords.y)*vec2(0.5) + vec2(0.5);
74+
return clipCoords*vec2(0.5, -0.5) + vec2(0.5);
3875
}
3976

4077
/**
@@ -45,11 +82,44 @@ vec2 dsCommonClipToTexCoords(vec2 clipCoords)
4582
vec2 dsClipToTexCoords(vec2 clipCoords)
4683
{
4784
#if SPIRV_VERSION
48-
// Avoid having to call DS_ADJUST_DIRECT_CLIP and have duplicate operations.
85+
// Avoid having to call dsToggleCommonClip() and have duplicate operations.
4986
return clipCoords*vec2(0.5) + vec2(0.5);
5087
#else
51-
return vec2(clipCoords.x, -clipCoords.y)*vec2(0.5) + vec2(0.5);
88+
return clipCoords*vec2(0.5, -0.5) + vec2(0.5);
89+
#endif
90+
}
91+
92+
/**
93+
* @brief Converts a depth value to a clip Z coordinate.
94+
* @param depth The depth value in the range [0, 1].
95+
* @return The z coordinate for a clip position.
96+
*/
97+
float dsDepthToClipZ(float depth)
98+
{
99+
#if GLSL_VERSION || GLSLES_VERSION
100+
// Only OpenGL may potentially have a different clip space.
101+
return depth*DS_DEPTH_TO_CLIP_Z_MUL + DS_DEPTH_TO_CLIP_Z_ADD;
102+
#else
103+
return depth;
104+
#endif
105+
}
106+
107+
/**
108+
* @brief Transforms the current pixel's framebuffer coordinates to clip coordinates.
109+
* @param framebufferSize The size of the framebuffer.
110+
* @return The clip position for the current framebuffer position as would have been set on
111+
* gl_Position after multiplying by a projection matrix.
112+
*/
113+
[[fragment]]
114+
vec2 dsFramebufferToClip(vec2 framebufferSize)
115+
{
116+
// gl_FragCoord origin follows native texture coordinate origin.
117+
vec2 framebufferPos = gl_FragCoord.xy/framebufferSize;
118+
framebufferPos = framebufferPos*vec2(2.0) - vec2(1.0);
119+
#if METAL_VERSION
120+
framebufferPos.y = -framebufferPos.y;
52121
#endif
122+
return framebufferPos;
53123
}
54124

55125
/**

modules/Render/Render/include/DeepSea/Render/Shaders/Shadows/CascadedShadowMap.mslh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ float DS_CASCADED_SHADOW_FUNCTION_NAME(sampler2DArrayShadow shadowMap, vec3 view
8484
float fadeAmount = fadeFactors.x/fadeFactors.y;
8585

8686
mat4 projection = INSTANCE(DS_SHADOW_UNIFORM).matrices[split];
87-
vec4 shadowPos = DS_ADJUST_TEXTURE_CLIP(projection*vec4(viewPos, 1.0));
87+
vec4 shadowPos = projection*vec4(viewPos, 1.0);
8888
shadowPos /= shadowPos.w;
8989
vec3 shadowTexCoords = shadowPos.xyz*texCoordTransform[0] + texCoordTransform[1];
9090
shadowTexCoords.z = clamp(shadowTexCoords.z, 0.0, 1.0);

modules/Render/Render/include/DeepSea/Render/Shaders/Shadows/PointShadowMap.mslh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ float DS_POINT_SHADOW_FUNCTION_NAME(sampler2DArrayShadow shadowMap, vec3 viewPos
9090
surface = lightToPos.z > 0 ? 4 : 5;
9191

9292
mat4 projection = INSTANCE(DS_SHADOW_UNIFORM).matrices[surface];
93-
vec4 shadowPos = DS_ADJUST_TEXTURE_CLIP(projection*vec4(viewPos, 1.0));
93+
vec4 shadowPos = projection*vec4(viewPos, 1.0);
9494
shadowPos /= shadowPos.w;
9595
vec3 shadowTexCoords = shadowPos.xyz*texCoordTransform[0] + texCoordTransform[1];
9696
shadowTexCoords.z = clamp(shadowTexCoords.z, 0.0, 1.0);

modules/Render/Render/include/DeepSea/Render/Types.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,11 +2217,6 @@ struct dsRenderer
22172217
*/
22182218
bool hasFragmentInputs;
22192219

2220-
/**
2221-
* @brief Whether or not projected texture coordinates have an inverted T coordinate.
2222-
*/
2223-
bool projectedTexCoordTInverted;
2224-
22252220
/**
22262221
* @brief Whether or not render passes require strictly all commands in separate command buffers
22272222
* when using secondary command buffers.

modules/Render/RenderMetal/msl-config/metal-ios-1.0.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ target = metal-ios
22
version = 1.0
33
define = subpassInput(i)=layout(input_attachment_index = i) subpassInput
44
define = DS_MIN_CLIP_Z=0.0
5+
define = DS_CLIP_Z_TO_DEPTH_MUL=1.0
6+
define = DS_CLIP_Z_TO_DEPTH_ADD=0.0
7+
define = DS_DEPTH_TO_CLIP_Z_MUL=1.0
8+
define = DS_DEPTH_TO_CLIP_Z_ADD=0.0
59
define = DS_ADJUST_CLIP(v)=(v)
610
define = DS_ADJUST_DIRECT_CLIP(v)=(v)
7-
define = DS_ADJUST_TEXTURE_CLIP(v)=(v)
811
define = DS_RG_SWZL=rg

modules/Render/RenderMetal/msl-config/metal-ios-1.1.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ target = metal-ios
22
version = 1.1
33
define = subpassInput(i)=layout(input_attachment_index = i) subpassInput
44
define = DS_MIN_CLIP_Z=0.0
5+
define = DS_CLIP_Z_TO_DEPTH_MUL=1.0
6+
define = DS_CLIP_Z_TO_DEPTH_ADD=0.0
7+
define = DS_DEPTH_TO_CLIP_Z_MUL=1.0
8+
define = DS_DEPTH_TO_CLIP_Z_ADD=0.0
59
define = DS_ADJUST_CLIP(v)=(v)
610
define = DS_ADJUST_DIRECT_CLIP(v)=(v)
7-
define = DS_ADJUST_TEXTURE_CLIP(v)=(v)
811
define = DS_RG_SWZL=rg

modules/Render/RenderMetal/msl-config/metal-ios-1.2.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ target = metal-ios
22
version = 1.2
33
define = subpassInput(i)=layout(input_attachment_index = i) subpassInput
44
define = DS_MIN_CLIP_Z=0.0
5+
define = DS_CLIP_Z_TO_DEPTH_MUL=1.0
6+
define = DS_CLIP_Z_TO_DEPTH_ADD=0.0
7+
define = DS_DEPTH_TO_CLIP_Z_MUL=1.0
8+
define = DS_DEPTH_TO_CLIP_Z_ADD=0.0
59
define = DS_ADJUST_CLIP(v)=(v)
610
define = DS_ADJUST_DIRECT_CLIP(v)=(v)
7-
define = DS_ADJUST_TEXTURE_CLIP(v)=(v)
811
define = DS_RG_SWZL=rg

modules/Render/RenderMetal/msl-config/metal-ios-2.0.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ target = metal-ios
22
version = 2.0
33
define = subpassInput(i)=layout(input_attachment_index = i) subpassInput
44
define = DS_MIN_CLIP_Z=0.0
5+
define = DS_CLIP_Z_TO_DEPTH_MUL=1.0
6+
define = DS_CLIP_Z_TO_DEPTH_ADD=0.0
7+
define = DS_DEPTH_TO_CLIP_Z_MUL=1.0
8+
define = DS_DEPTH_TO_CLIP_Z_ADD=0.0
59
define = DS_ADJUST_CLIP(v)=(v)
610
define = DS_ADJUST_DIRECT_CLIP(v)=(v)
7-
define = DS_ADJUST_TEXTURE_CLIP(v)=(v)
811
define = DS_RG_SWZL=rg

0 commit comments

Comments
 (0)