diff options
author | Chip Davis <cdavis@codeweavers.com> | 2020-11-21 00:41:46 +0300 |
---|---|---|
committer | Chip Davis <cdavis@codeweavers.com> | 2020-11-23 19:30:24 +0300 |
commit | fd738e3387535b4e18104336ff04a0c000ab5f45 (patch) | |
tree | 91102c719da448dbb1875b6082627e188f8bf7bc /reference/opt/shaders-msl | |
parent | 782916a7971bbbd450f288c939ea838da0b2e6f1 (diff) |
MSL: Adjust FragCoord for sample-rate shading.
In Metal, the `[[position]]` input to a fragment shader remains at
fragment center, even at sample rate, like OpenGL and Direct3D. In
Vulkan, however, when the fragment shader runs at sample rate, the
`FragCoord` builtin moves to the sample position in the framebuffer,
instead of the fragment center. To account for this difference, adjust
the `FragCoord`, if present, by the sample position. The -0.5 offset is
because the fragment center is at (0.5, 0.5).
Also, add an option to force sample-rate shading in a fragment shader.
Since Metal has no explicit control for this, this is done by adding a
dummy `[[sample_id]]` which is otherwise unused, if none is already
present. This is intended to be used from e.g. MoltenVK when a
pipeline's `minSampleShading` value is nonzero.
Instead of checking if any `Input` variables have `Sample`
interpolation, I've elected to check that the `SampleRateShading`
capability is present. Since `SampleId`, `SamplePosition`, and the
`Sample` interpolation decoration require this cap, this should be
equivalent for any valid SPIR-V module. If this isn't acceptable, let me
know.
Diffstat (limited to 'reference/opt/shaders-msl')
8 files changed, 106 insertions, 0 deletions
diff --git a/reference/opt/shaders-msl/frag/basic.force-sample.frag b/reference/opt/shaders-msl/frag/basic.force-sample.frag new file mode 100644 index 00000000..b9706b73 --- /dev/null +++ b/reference/opt/shaders-msl/frag/basic.force-sample.frag @@ -0,0 +1,23 @@ +#include <metal_stdlib> +#include <simd/simd.h> + +using namespace metal; + +struct main0_out +{ + float4 FragColor [[color(0)]]; +}; + +struct main0_in +{ + float4 vColor [[user(locn0)]]; + float2 vTex [[user(locn1)]]; +}; + +fragment main0_out main0(main0_in in [[stage_in]], texture2d<float> uTex [[texture(0)]], sampler uTexSmplr [[sampler(0)]], uint gl_SampleID [[sample_id]]) +{ + main0_out out = {}; + out.FragColor = in.vColor * uTex.sample(uTexSmplr, in.vTex); + return out; +} + diff --git a/reference/opt/shaders-msl/frag/input-attachment-ms.arrayed-subpass.msl21.frag b/reference/opt/shaders-msl/frag/input-attachment-ms.arrayed-subpass.msl21.frag index 5f137b04..52a78cf9 100644 --- a/reference/opt/shaders-msl/frag/input-attachment-ms.arrayed-subpass.msl21.frag +++ b/reference/opt/shaders-msl/frag/input-attachment-ms.arrayed-subpass.msl21.frag @@ -11,6 +11,7 @@ struct main0_out fragment main0_out main0(texture2d_ms_array<float> uSubpass0 [[texture(0)]], texture2d_ms_array<float> uSubpass1 [[texture(1)]], uint gl_SampleID [[sample_id]], float4 gl_FragCoord [[position]], uint gl_Layer [[render_target_array_index]]) { main0_out out = {}; + gl_FragCoord.xy += get_sample_position(gl_SampleID) - 0.5; out.FragColor = (uSubpass0.read(uint2(gl_FragCoord.xy), gl_Layer, 1) + uSubpass1.read(uint2(gl_FragCoord.xy), gl_Layer, 2)) + uSubpass0.read(uint2(gl_FragCoord.xy), gl_Layer, gl_SampleID); return out; } diff --git a/reference/opt/shaders-msl/frag/input-attachment-ms.frag b/reference/opt/shaders-msl/frag/input-attachment-ms.frag index 906cabbf..0c47348d 100644 --- a/reference/opt/shaders-msl/frag/input-attachment-ms.frag +++ b/reference/opt/shaders-msl/frag/input-attachment-ms.frag @@ -11,6 +11,7 @@ struct main0_out fragment main0_out main0(texture2d_ms<float> uSubpass0 [[texture(0)]], texture2d_ms<float> uSubpass1 [[texture(1)]], uint gl_SampleID [[sample_id]], float4 gl_FragCoord [[position]]) { main0_out out = {}; + gl_FragCoord.xy += get_sample_position(gl_SampleID) - 0.5; out.FragColor = (uSubpass0.read(uint2(gl_FragCoord.xy), 1) + uSubpass1.read(uint2(gl_FragCoord.xy), 2)) + uSubpass0.read(uint2(gl_FragCoord.xy), gl_SampleID); return out; } diff --git a/reference/opt/shaders-msl/frag/input-attachment-ms.multiview.msl21.frag b/reference/opt/shaders-msl/frag/input-attachment-ms.multiview.msl21.frag index 2e4ca8a7..e27b24ad 100644 --- a/reference/opt/shaders-msl/frag/input-attachment-ms.multiview.msl21.frag +++ b/reference/opt/shaders-msl/frag/input-attachment-ms.multiview.msl21.frag @@ -11,6 +11,7 @@ struct main0_out fragment main0_out main0(constant uint* spvViewMask [[buffer(24)]], texture2d_ms_array<float> uSubpass0 [[texture(0)]], texture2d_ms_array<float> uSubpass1 [[texture(1)]], uint gl_SampleID [[sample_id]], float4 gl_FragCoord [[position]], uint gl_ViewIndex [[render_target_array_index]]) { main0_out out = {}; + gl_FragCoord.xy += get_sample_position(gl_SampleID) - 0.5; gl_ViewIndex += spvViewMask[0]; out.FragColor = (uSubpass0.read(uint2(gl_FragCoord.xy), gl_ViewIndex, 1) + uSubpass1.read(uint2(gl_FragCoord.xy), gl_ViewIndex, 2)) + uSubpass0.read(uint2(gl_FragCoord.xy), gl_ViewIndex, gl_SampleID); return out; diff --git a/reference/opt/shaders-msl/frag/sample-rate-frag-coord-sample-id.frag b/reference/opt/shaders-msl/frag/sample-rate-frag-coord-sample-id.frag new file mode 100644 index 00000000..5df60f90 --- /dev/null +++ b/reference/opt/shaders-msl/frag/sample-rate-frag-coord-sample-id.frag @@ -0,0 +1,19 @@ +#include <metal_stdlib> +#include <simd/simd.h> + +using namespace metal; + +struct main0_out +{ + float4 FragColor [[color(0)]]; +}; + +fragment main0_out main0(texture2d_array<float> tex [[texture(0)]], sampler texSmplr [[sampler(0)]], float4 gl_FragCoord [[position]], uint gl_SampleID [[sample_id]]) +{ + main0_out out = {}; + gl_FragCoord.xy += get_sample_position(gl_SampleID) - 0.5; + float3 _28 = float3(gl_FragCoord.xy, float(gl_SampleID)); + out.FragColor = tex.sample(texSmplr, _28.xy, uint(round(_28.z))); + return out; +} + diff --git a/reference/opt/shaders-msl/frag/sample-rate-frag-coord-sample-input.frag b/reference/opt/shaders-msl/frag/sample-rate-frag-coord-sample-input.frag new file mode 100644 index 00000000..386230ef --- /dev/null +++ b/reference/opt/shaders-msl/frag/sample-rate-frag-coord-sample-input.frag @@ -0,0 +1,24 @@ +#include <metal_stdlib> +#include <simd/simd.h> + +using namespace metal; + +struct main0_out +{ + float4 FragColor [[color(0)]]; +}; + +struct main0_in +{ + float foo [[user(locn0), sample_perspective]]; +}; + +fragment main0_out main0(main0_in in [[stage_in]], texture2d_array<float> tex [[texture(0)]], sampler texSmplr [[sampler(0)]], float4 gl_FragCoord [[position]], uint gl_SampleID [[sample_id]]) +{ + main0_out out = {}; + gl_FragCoord.xy += get_sample_position(gl_SampleID) - 0.5; + float3 _26 = float3(gl_FragCoord.xy, in.foo); + out.FragColor = tex.sample(texSmplr, _26.xy, uint(round(_26.z))); + return out; +} + diff --git a/reference/opt/shaders-msl/frag/sample-rate-frag-coord-sample-pos.frag b/reference/opt/shaders-msl/frag/sample-rate-frag-coord-sample-pos.frag new file mode 100644 index 00000000..f8f357fe --- /dev/null +++ b/reference/opt/shaders-msl/frag/sample-rate-frag-coord-sample-pos.frag @@ -0,0 +1,19 @@ +#include <metal_stdlib> +#include <simd/simd.h> + +using namespace metal; + +struct main0_out +{ + float4 FragColor [[color(0)]]; +}; + +fragment main0_out main0(texture2d<float> tex [[texture(0)]], sampler texSmplr [[sampler(0)]], float4 gl_FragCoord [[position]], uint gl_SampleID [[sample_id]]) +{ + main0_out out = {}; + gl_FragCoord.xy += get_sample_position(gl_SampleID) - 0.5; + float2 gl_SamplePosition = get_sample_position(gl_SampleID); + out.FragColor = tex.sample(texSmplr, (gl_FragCoord.xy - gl_SamplePosition)); + return out; +} + diff --git a/reference/opt/shaders-msl/frag/sample-rate-frag-coord.force-sample.frag b/reference/opt/shaders-msl/frag/sample-rate-frag-coord.force-sample.frag new file mode 100644 index 00000000..1ed8148d --- /dev/null +++ b/reference/opt/shaders-msl/frag/sample-rate-frag-coord.force-sample.frag @@ -0,0 +1,18 @@ +#include <metal_stdlib> +#include <simd/simd.h> + +using namespace metal; + +struct main0_out +{ + float4 FragColor [[color(0)]]; +}; + +fragment main0_out main0(texture2d<float> tex [[texture(0)]], sampler texSmplr [[sampler(0)]], float4 gl_FragCoord [[position]], uint gl_SampleID [[sample_id]]) +{ + main0_out out = {}; + gl_FragCoord.xy += get_sample_position(gl_SampleID) - 0.5; + out.FragColor = tex.sample(texSmplr, gl_FragCoord.xy); + return out; +} + |