Skip to content

Commit

Permalink
[Metal] update shaders to fix lidar near plane clipping
Browse files Browse the repository at this point in the history
- Update Metal shaders to match changes made in PR gazebosim#356
- Update CrossPlatformSettings_piece to #define outVs_gl_ClipDistance
- Update Ign_piece_vs_any to use cross platform version of gl_ClipDistance

- cherry-pick a2a3760

Signed-off-by: Rhys Mainwaring <[email protected]>
  • Loading branch information
srmainwaring committed Oct 23, 2021
1 parent 7d40f42 commit 465b861
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@

#define outVs_Position gl_Position
#define outVs_viewportIndex gl_ViewportIndex
#define outVs_clipDistance gl_ClipDistance
#define outVs_clipDistance0 gl_ClipDistance[0]

#define gl_SampleMaskIn0 gl_SampleMaskIn[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

#define outVs_Position outVs.gl_Position
#define outVs_viewportIndex outVs.gl_ViewportIndex
#define outVs_clipDistance outVs.gl_ClipDistance
#define outVs_clipDistance0 outVs.gl_ClipDistance0.x

#define gl_SampleMaskIn0 gl_SampleMask
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ inline float3x3 toMat3x3( float3x4 m )

#define outVs_Position outVs.gl_Position
#define outVs_viewportIndex outVs.gl_ViewportIndex
#define outVs_clipDistance outVs.gl_ClipDistance
#define outVs_clipDistance0 outVs.gl_ClipDistance[0]

#define gl_SampleMaskIn0 gl_SampleMask
Expand Down
2 changes: 1 addition & 1 deletion ogre2/src/media/Hlms/Ignition/Ign_piece_vs.any
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Ogre 2.2 should use outVs_clipDistanceN for compatibility with all
// APIs
// Rare case of geometry without normals.
gl_ClipDistance[@value(ign_spherical_clip_idx)] =
outVs_clipDistance[@value(ign_spherical_clip_idx)] =
distance( worldPos.xyz, passBuf.ignCameraPos.xyz ) -
passBuf.ignMinClipDistance;
@end
Expand Down
16 changes: 16 additions & 0 deletions ogre2/src/media/materials/programs/Metal/plain_color_vs.metal
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ struct VS_INPUT
struct PS_INPUT
{
float4 gl_Position [[position]];
float gl_ClipDistance [[clip_distance]] [1];
};

struct Params
{
float4x4 worldViewProj;
float4x4 worldView;
float ignMinClipDistance;
};

vertex PS_INPUT main_metal
Expand All @@ -43,5 +46,18 @@ vertex PS_INPUT main_metal

outVs.gl_Position = ( p.worldViewProj * input.position ).xyzw;

if( p.ignMinClipDistance > 0.0f )
{
// Frustum is rectangular; but the minimum lidar distance is spherical,
// so we can't rely on near plane to clip geometry that is too close,
// we have to do it by hand
float3 viewSpacePos = (p.worldView * input.position).xyz;
outVs.gl_ClipDistance[0] = length( viewSpacePos.xyz ) - p.ignMinClipDistance;
}
else
{
outVs.gl_ClipDistance[0] = 1.0f;
}

return outVs;
}
3 changes: 3 additions & 0 deletions ogre2/src/media/materials/scripts/gpu_rays.material
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,13 @@ fragment_program laser_retro_fs_GLSL glsl
vertex_program laser_retro_vs_Metal metal
{
source plain_color_vs.metal
num_clip_distances 1

default_params
{
param_named_auto worldViewProj worldviewproj_matrix
param_named_auto worldView worldview_matrix
param_named ignMinClipDistance float 0.0
}
}

Expand Down
3 changes: 3 additions & 0 deletions ogre2/src/media/materials/scripts/picker.material
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ fragment_program plaincolor_fs_GLSL glsl
vertex_program plaincolor_vs_Metal metal
{
source plain_color_vs.metal
num_clip_distances 1

default_params
{
param_named_auto worldViewProj worldviewproj_matrix
param_named_auto worldView worldview_matrix
param_named ignMinClipDistance float 0.0
}
}

Expand Down
3 changes: 3 additions & 0 deletions ogre2/src/media/materials/scripts/thermal.material
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ fragment_program heat_source_fs_GLSL glsl
vertex_program heat_source_vs_Metal metal
{
source plain_color_vs.metal
num_clip_distances 1

default_params
{
param_named_auto worldViewProj worldviewproj_matrix
param_named_auto worldView worldview_matrix
param_named ignMinClipDistance float 0.0
}
}

Expand Down

0 comments on commit 465b861

Please sign in to comment.