Skip to content

Commit

Permalink
Allow post-process materials to update the vertex position
Browse files Browse the repository at this point in the history
we add a `position` field to PostProcessVertexInputs which the user
code can modify if needed.
  • Loading branch information
pixelflinger committed Dec 12, 2023
1 parent 111ad96 commit dedf276
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
23 changes: 14 additions & 9 deletions shaders/src/post_process.vs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ void main() {

inputs.normalizedUV = position.xy * 0.5 + 0.5;

vec4 position = getPosition();
inputs.position = getPosition();

// GL convention to inverted DX convention
position.z = position.z * -0.5 + 0.5;
// Invoke user code
postProcessVertex(inputs);

// (vertex domain) GL convention to inverted DX convention
inputs.position.z = inputs.position.z * -0.5 + 0.5;

#if defined(TARGET_VULKAN_ENVIRONMENT)
// In Vulkan, clip space is Y-down. In OpenGL and Metal, clip space is Y-up.
inputs.position.y = -inputs.position.y;
#endif

// Adjust clip-space
#if !defined(TARGET_VULKAN_ENVIRONMENT) && !defined(TARGET_METAL_ENVIRONMENT)
// This is not needed in Vulkan or Metal because clipControl is always (1, 0)
// (We don't use a dot() here because it workaround a spirv-opt optimization that in turn
// (We don't use a dot() here because it works around a spirv-opt optimization that in turn
// causes a crash on PowerVR, see #5118)
position.z = position.z * frameUniforms.clipControl.x + position.w * frameUniforms.clipControl.y;
inputs.position.z = inputs.position.z * frameUniforms.clipControl.x + inputs.position.w * frameUniforms.clipControl.y;
#endif

// Invoke user code
postProcessVertex(inputs);

// Handle user-defined interpolated attributes
#if defined(VARIABLE_CUSTOM0)
VARIABLE_CUSTOM_AT0 = inputs.VARIABLE_CUSTOM0;
Expand All @@ -36,5 +41,5 @@ void main() {
#endif

// some PowerVR drivers crash when gl_Position is written more than once
gl_Position = position;
gl_Position = inputs.position;
}
9 changes: 1 addition & 8 deletions shaders/src/post_process_getters.vs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
/** @public-api */
vec4 getPosition() {
vec4 pos = position;

// In Vulkan, clip space is Y-down. In OpenGL and Metal, clip space is Y-up.
#if defined(TARGET_VULKAN_ENVIRONMENT)
pos.y = -pos.y;
#endif

return pos;
return position;
}
3 changes: 3 additions & 0 deletions shaders/src/post_process_inputs.vs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ struct PostProcessVertexInputs {
// We provide normalized texture coordinates to custom vertex shaders.
vec2 normalizedUV;

// vertex position, can be modified by the user code
vec4 position;

#ifdef VARIABLE_CUSTOM0
vec4 VARIABLE_CUSTOM0;
#endif
Expand Down

0 comments on commit dedf276

Please sign in to comment.