Skip to content

Commit a9bccfb

Browse files
committed
Metal Shaders
Signed-off-by: Isaac Marovitz <isaacryu@icloud.com>
1 parent 9426a4c commit a9bccfb

4 files changed

Lines changed: 118 additions & 20 deletions

File tree

MarathonRecomp/gpu/shader/hlsl/enhanced_burnout_blur_ps.hlsl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
#ifdef __spirv__
44

5-
#define g_SampleCount vk::RawBufferLoad<int>(g_PushConstants.SharedConstants + 4)
5+
#define g_SampleCount vk::RawBufferLoad<int>(g_PushConstants.PixelShaderConstants + 0)
66

77
#define s0_TextureDescriptorIndex vk::RawBufferLoad<int>(g_PushConstants.SharedConstants + 0)
88
#define s0_SamplerDescriptorIndex vk::RawBufferLoad<uint>(g_PushConstants.SharedConstants + 192)
99

1010
#else
1111

12+
cbuffer PixelShaderConstants : register(b1, space1)
13+
{
14+
int g_SampleCount;
15+
};
16+
1217
cbuffer SharedConstants : register(b2, space4)
1318
{
14-
int g_SampleCount : packoffset(c0.y);
1519
uint s0_Texture2DDescriptorIndex : packoffset(c0.x);
1620
uint s0_SamplerDescriptorIndex : packoffset(c12.x);
1721
DEFINE_SHARED_CONSTANTS();
@@ -25,19 +29,20 @@ float4 shaderMain(
2529
in float2 oVelocity : TEXCOORD1,
2630
in float2 oVelScale : TEXCOORD2) : SV_Target
2731
{
32+
Texture2D<float4> texture = g_Texture2DDescriptorHeap[s0_TextureDescriptorIndex];
33+
SamplerState samplerState = g_SamplerDescriptorHeap[s0_SamplerDescriptorIndex];
34+
2835
if (g_SampleCount == 1)
29-
return texture.SampleLevel(SAMPLER, oTexCoord0, 0);
36+
return texture.SampleLevel(samplerState, oTexCoord0, 0);
3037

3138
float velocityMag = sqrt(dot(oVelocity, oVelocity));
3239
float blurAmount = saturate(velocityMag - 0.25);
3340

3441
if (blurAmount == 0.0)
35-
return texture.SampleLevel(SAMPLER, oTexCoord0, 0);
42+
return texture.SampleLevel(samplerState, oTexCoord0, 0);
3643

3744
blurAmount = min(blurAmount * blurAmount, 0.25);
38-
float result = float4(0.0);
39-
40-
Texture2D<float> texture = g_Texture2DDescriptorHeap[s0_TextureDescriptorIndex];
45+
float4 result = float4(0.0, 0.0, 0.0, 0.0);
4146

4247
UNROLL
4348
for (int i = 0; i < g_SampleCount; i++)
@@ -47,7 +52,7 @@ float4 shaderMain(
4752
float2 offset = blurAmount * oVelScale * t * g_SampleCount;
4853
float2 samplePos = oTexCoord0 + offset;
4954

50-
result += texture.SampleLevel(s0_SamplerDescriptorIndex, samplePos, 0);
55+
result += texture.SampleLevel(samplerState, samplePos, 0);
5156
}
5257

5358
return result * (1.0 / (float)g_SampleCount);

MarathonRecomp/gpu/shader/hlsl/enhanced_burnout_blur_vs.hlsl

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
#ifdef __spirv__
44

5-
#define g_Velocity = vk::RawBufferLoad<float4>(g_PushConstants.VertexShaderConstants + 3360, 0x10);
6-
#define g_SampleCount vk::RawBufferLoad<int>(g_PushConstants.SharedConstants + 4)
5+
#define g_SampleCount vk::RawBufferLoad<int>(g_PushConstants.VertexShaderConstants + 0)
6+
#define g_Velocity vk::RawBufferLoad<float4>(g_PushConstants.VertexShaderConstants + 3360, 0x10)
77

88
#else
99

1010
cbuffer VertexShaderConstants : register(b1, space4)
1111
{
12+
int g_SampleCount;
1213
float4 g_Velocity : packoffset(c210);
1314
};
1415

15-
cbuffer SharedConstants : register(b2, space4)
16-
{
17-
int g_SampleCount : packoffset(c0.y);
18-
};
19-
2016
#endif
2117

2218
void shaderMain(
@@ -32,20 +28,20 @@ void shaderMain(
3228

3329
if (g_SampleCount == 1)
3430
{
35-
oVelocity = float2(0.0);
36-
oVelScale = float2(0.0);
31+
oVelocity = float2(0.0, 0.0);
32+
oVelScale = float2(0.0, 0.0);
3733
return;
3834
}
3935

4036
float2 centeredUV = iTexCoord0 * 2.0 - 1.0;
4137

4238
oVelocity.x = -g_Velocity.x - centeredUV.y;
43-
oVelocity.y = centeredUV.x - centeredUV.y;
39+
oVelocity.y = centeredUV.x - g_Velocity.y;
4440

4541
float sampleScaleX = 0.00002 / (float)g_SampleCount;
4642
float sampleScaleY = 0.00001 / (float)g_SampleCount;
4743

4844
float2 scaledVec = oVelocity * g_Velocity.w;
49-
output oVelScale.x = scaledVec * sampleScaleX;
50-
output oVelScale.y = scaledVec * sampleScaleY;
45+
oVelScale.x = scaledVec.x * sampleScaleX;
46+
oVelScale.y = scaledVec.y * sampleScaleY;
5147
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "../../../../tools/XenosRecomp/XenosRecomp/shader_common.h"
2+
3+
#define g_SampleCount (*(reinterpret_cast<device int*>(g_PushConstants.PixelShaderConstants + 0)))
4+
5+
#define s0_TextureDescriptorIndex (*(reinterpret_cast<device int*>(g_PushConstants.SharedConstants + 0)))
6+
#define s0_SamplerDescriptorIndex (*(reinterpret_cast<device uint*>(g_PushConstants.SharedConstants + 192)))
7+
8+
struct Interpolators
9+
{
10+
float4 oPos [[position]];
11+
float2 oTexCoord0 [[user(TEXCOORD0)]];
12+
float2 oVelocity [[user(TEXCOORD1)]];
13+
float2 oVelScale [[user(TEXCOORD2)]];
14+
};
15+
16+
[[fragment]]
17+
float4 shaderMain(Interpolators input [[stage_in]],
18+
constant Texture2DDescriptorHeap* g_Texture2DDescriptorHeap [[buffer(0)]],
19+
constant SamplerDescriptorHeap* g_SamplerDescriptorHeap [[buffer(3)]],
20+
constant PushConstants& g_PushConstants [[buffer(8)]])
21+
{
22+
texture2d<float> texture = g_Texture2DDescriptorHeap[s0_TextureDescriptorIndex].tex;
23+
sampler samp = g_SamplerDescriptorHeap[s0_SamplerDescriptorIndex].samp;
24+
25+
if (g_SampleCount == 1)
26+
return texture.sample(samp, input.oTexCoord0, level(0));
27+
28+
float velocityMag = sqrt(dot(input.oVelocity, input.oVelocity));
29+
float blurAmount = saturate(velocityMag - 0.25);
30+
31+
if (blurAmount == 0.0)
32+
return texture.sample(samp, input.oTexCoord0, level(0));
33+
34+
blurAmount = min(blurAmount * blurAmount, 0.25);
35+
float4 result = float4(0.0);
36+
37+
for (int i = 0; i < g_SampleCount; i++)
38+
{
39+
float t = ((float)i / (float)(g_SampleCount - 1)) * 2.0 - 1.0;
40+
41+
float2 offset = blurAmount * input.oVelScale * t * g_SampleCount;
42+
float2 samplePos = input.oTexCoord0 + offset;
43+
44+
result += texture.sample(samp, samplePos, level(0));
45+
}
46+
47+
return result * (1.0 / (float)g_SampleCount);
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "../../../../tools/XenosRecomp/XenosRecomp/shader_common.h"
2+
3+
#define g_SampleCount (*(reinterpret_cast<device int*>(g_PushConstants.VertexShaderConstants + 0)))
4+
#define g_Velocity (*(reinterpret_cast<device float4*>(g_PushConstants.VertexShaderConstants + 3360)))
5+
6+
struct VertexShaderInput
7+
{
8+
float4 iPosition0 [[attribute(0)]];
9+
float2 iTexCoord0 [[attribute(4)]];
10+
};
11+
12+
struct Interpolators
13+
{
14+
float4 oPos [[position]];
15+
float2 oTexCoord0 [[user(TEXCOORD0)]];
16+
float2 oVelocity [[user(TEXCOORD1)]];
17+
float2 oVelScale [[user(TEXCOORD2)]];
18+
};
19+
20+
[[vertex]]
21+
Interpolators shaderMain(VertexShaderInput input [[stage_in]],
22+
constant PushConstants& g_PushConstants [[buffer(8)]])
23+
{
24+
Interpolators output;
25+
26+
output.oPos = input.iPosition0;
27+
output.oTexCoord0 = input.iTexCoord0;
28+
29+
if (g_SampleCount == 1)
30+
{
31+
output.oVelocity = float2(0.0);
32+
output.oVelScale = float2(0.0);
33+
return output;
34+
}
35+
36+
float2 centeredUV = input.iTexCoord0 * 2.0 - 1.0;
37+
38+
output.oVelocity.x = -g_Velocity.x - centeredUV.y;
39+
output.oVelocity.y = centeredUV.x - g_Velocity.y;
40+
41+
float sampleScaleX = 0.00002 / (float)g_SampleCount;
42+
float sampleScaleY = 0.00001 / (float)g_SampleCount;
43+
44+
float2 scaledVec = output.oVelocity * g_Velocity.w;
45+
output.oVelScale.x = scaledVec.x * sampleScaleX;
46+
output.oVelScale.y = scaledVec.y * sampleScaleY;
47+
48+
return output;
49+
}

0 commit comments

Comments
 (0)