Skip to content

Commit f0aa0e9

Browse files
committed
Make it actually work
Signed-off-by: Isaac Marovitz <isaacryu@icloud.com>
1 parent e418b35 commit f0aa0e9

5 files changed

Lines changed: 63 additions & 55 deletions

File tree

MarathonRecomp/gpu/shader/hlsl/enhanced_burnout_blur_ps.hlsl

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22

33
#ifdef __spirv__
44

5-
#define g_SampleCount vk::RawBufferLoad<int>(g_PushConstants.PixelShaderConstants + 0)
6-
7-
#define s0_TextureDescriptorIndex vk::RawBufferLoad<int>(g_PushConstants.SharedConstants + 0)
5+
#define s0_TextureDescriptorIndex vk::RawBufferLoad<uint>(g_PushConstants.SharedConstants + 0)
86
#define s0_SamplerDescriptorIndex vk::RawBufferLoad<uint>(g_PushConstants.SharedConstants + 192)
97

108
#else
119

12-
cbuffer PixelShaderConstants : register(b1, space1)
13-
{
14-
int g_SampleCount;
15-
};
16-
1710
cbuffer SharedConstants : register(b2, space4)
1811
{
1912
uint s0_Texture2DDescriptorIndex : packoffset(c0.x);
@@ -32,24 +25,23 @@ float4 shaderMain(
3225
Texture2D<float4> texture = g_Texture2DDescriptorHeap[s0_TextureDescriptorIndex];
3326
SamplerState samplerState = g_SamplerDescriptorHeap[s0_SamplerDescriptorIndex];
3427

35-
if (g_SampleCount == 1)
36-
return texture.SampleLevel(samplerState, oTexCoord0, 0);
37-
3828
float velocityMag = sqrt(dot(oVelocity, oVelocity));
3929
float blurAmount = saturate(velocityMag - 0.25);
4030

4131
if (blurAmount == 0.0)
4232
return texture.SampleLevel(samplerState, oTexCoord0, 0);
4333

4434
blurAmount = min(blurAmount * blurAmount, 0.25);
35+
36+
float blurStrength = blurAmount * velocityMag;
37+
int g_SampleCount = clamp((int)(blurStrength * 256.0), 4, 64);
38+
float2 scaledVelStep = oVelScale / (float)g_SampleCount;
39+
4540
float4 result = float4(0.0, 0.0, 0.0, 0.0);
4641

47-
UNROLL
4842
for (int i = 0; i < g_SampleCount; i++)
4943
{
50-
float t = ((float)i / (float)(g_SampleCount - 1)) * 2.0 - 1.0;
51-
52-
float2 offset = blurAmount * oVelScale * t * g_SampleCount;
44+
float2 offset = blurAmount * scaledVelStep * (float)i;
5345
float2 samplePos = oTexCoord0 + offset;
5446

5547
result += texture.SampleLevel(samplerState, samplePos, 0);

MarathonRecomp/gpu/shader/hlsl/enhanced_burnout_blur_vs.hlsl

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

33
#ifdef __spirv__
44

5-
#define g_SampleCount vk::RawBufferLoad<int>(g_PushConstants.VertexShaderConstants + 0)
65
#define g_Velocity vk::RawBufferLoad<float4>(g_PushConstants.VertexShaderConstants + 3360, 0x10)
76

87
#else
98

109
cbuffer VertexShaderConstants : register(b1, space4)
1110
{
12-
int g_SampleCount;
1311
float4 g_Velocity : packoffset(c210);
1412
};
1513

1614
#endif
1715

1816
void shaderMain(
1917
[[vk::location(0)]] in float4 iPosition0 : POSITION0,
20-
[[vk::location(8)]] in float2 iTexCoord0 : TEXCOORD0,
18+
[[vk::location(13)]] in float2 iTexCoord0 : TEXCOORD0,
2119
out float4 oPos : SV_Position,
2220
out float2 oTexCoord0 : TEXCOORD0,
2321
out float2 oVelocity : TEXCOORD1,
@@ -26,22 +24,14 @@ void shaderMain(
2624
oPos = iPosition0;
2725
oTexCoord0 = iTexCoord0;
2826

29-
if (g_SampleCount == 1)
30-
{
31-
oVelocity = float2(0.0, 0.0);
32-
oVelScale = float2(0.0, 0.0);
33-
return;
34-
}
35-
36-
float2 centeredUV = iTexCoord0 * 2.0 - 1.0;
27+
float2 centeredUV;
28+
centeredUV.x = iTexCoord0.y * 2.0 - 1.0;
29+
centeredUV.y = iTexCoord0.x * 2.0 - 1.0;
3730

3831
oVelocity.x = -g_Velocity.x - centeredUV.y;
3932
oVelocity.y = centeredUV.x - g_Velocity.y;
4033

41-
float sampleScaleX = 0.00002 / (float)g_SampleCount;
42-
float sampleScaleY = 0.00001 / (float)g_SampleCount;
43-
4434
float2 scaledVec = oVelocity * g_Velocity.w;
45-
oVelScale.x = scaledVec.x * sampleScaleX;
46-
oVelScale.y = scaledVec.y * sampleScaleY;
35+
oVelScale.x = scaledVec.x * 0.00002;
36+
oVelScale.y = -scaledVec.y * 0.00001;
4737
}

MarathonRecomp/gpu/shader/msl/enhanced_burnout_blur_ps.metal

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "../../../../tools/XenosRecomp/XenosRecomp/shader_common.h"
22

3-
#define g_SampleCount (*(reinterpret_cast<device int*>(g_PushConstants.PixelShaderConstants + 0)))
4-
53
#define s0_TextureDescriptorIndex (*(reinterpret_cast<device int*>(g_PushConstants.SharedConstants + 0)))
64
#define s0_SamplerDescriptorIndex (*(reinterpret_cast<device uint*>(g_PushConstants.SharedConstants + 192)))
75

@@ -22,27 +20,28 @@ float4 shaderMain(Interpolators input [[stage_in]],
2220
texture2d<float> texture = g_Texture2DDescriptorHeap[s0_TextureDescriptorIndex].tex;
2321
sampler samp = g_SamplerDescriptorHeap[s0_SamplerDescriptorIndex].samp;
2422

25-
if (g_SampleCount == 1)
26-
return texture.sample(samp, input.oTexCoord0, level(0));
27-
2823
float velocityMag = sqrt(dot(input.oVelocity, input.oVelocity));
2924
float blurAmount = saturate(velocityMag - 0.25);
3025

3126
if (blurAmount == 0.0)
3227
return texture.sample(samp, input.oTexCoord0, level(0));
3328

3429
blurAmount = min(blurAmount * blurAmount, 0.25);
30+
31+
float blurStrength = blurAmount * velocityMag;
32+
int g_SampleCount = clamp((int)(blurStrength * 256.0), 4, 64);
33+
float2 scaledVelStep = input.oVelScale / (float)g_SampleCount;
34+
3535
float4 result = float4(0.0);
3636

3737
for (int i = 0; i < g_SampleCount; i++)
3838
{
39-
float t = ((float)i / (float)(g_SampleCount - 1)) * 2.0 - 1.0;
40-
41-
float2 offset = blurAmount * input.oVelScale * t * g_SampleCount;
39+
float2 offset = blurAmount * scaledVelStep * (float)i;
4240
float2 samplePos = input.oTexCoord0 + offset;
4341

4442
result += texture.sample(samp, samplePos, level(0));
4543
}
4644

45+
return float4(1.0 / (float)(64 - g_SampleCount));
4746
return result * (1.0 / (float)g_SampleCount);
4847
}
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "../../../../tools/XenosRecomp/XenosRecomp/shader_common.h"
22

3-
#define g_SampleCount (*(reinterpret_cast<device int*>(g_PushConstants.VertexShaderConstants + 0)))
3+
// #define g_SampleCount (*(reinterpret_cast<device int*>(g_PushConstants.VertexShaderConstants + 0)))
44
#define g_Velocity (*(reinterpret_cast<device float4*>(g_PushConstants.VertexShaderConstants + 3360)))
55

66
struct VertexShaderInput
77
{
88
float4 iPosition0 [[attribute(0)]];
9-
float2 iTexCoord0 [[attribute(4)]];
9+
float2 iTexCoord0 [[attribute(13)]];
1010
};
1111

1212
struct Interpolators
@@ -26,24 +26,16 @@ Interpolators shaderMain(VertexShaderInput input [[stage_in]],
2626
output.oPos = input.iPosition0;
2727
output.oTexCoord0 = input.iTexCoord0;
2828

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;
29+
float2 centeredUV;
30+
centeredUV.x = input.iTexCoord0.y * 2.0 - 1.0;
31+
centeredUV.y = input.iTexCoord0.x * 2.0 - 1.0;
3732

3833
output.oVelocity.x = -g_Velocity.x - centeredUV.y;
3934
output.oVelocity.y = centeredUV.x - g_Velocity.y;
4035

41-
float sampleScaleX = 0.00002 / (float)g_SampleCount;
42-
float sampleScaleY = 0.00001 / (float)g_SampleCount;
43-
4436
float2 scaledVec = output.oVelocity * g_Velocity.w;
45-
output.oVelScale.x = scaledVec.x * sampleScaleX;
46-
output.oVelScale.y = scaledVec.y * sampleScaleY;
37+
output.oVelScale.x = scaledVec.x * 0.00002;
38+
output.oVelScale.y = -scaledVec.y * 0.00001;
4739

4840
return output;
4941
}

MarathonRecomp/gpu/video.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
#include "shader/hlsl/csd_filter_ps.hlsl.dxil.h"
5151
#include "shader/hlsl/csd_no_tex_vs.hlsl.dxil.h"
5252
#include "shader/hlsl/csd_vs.hlsl.dxil.h"
53+
#include "shader/hlsl/enhanced_burnout_blur_vs.hlsl.dxil.h"
54+
#include "shader/hlsl/enhanced_burnout_blur_ps.hlsl.dxil.h"
5355
#include "shader/hlsl/enhanced_motion_blur_ps.hlsl.dxil.h"
5456
#include "shader/hlsl/gamma_correction_ps.hlsl.dxil.h"
5557
#include "shader/hlsl/gaussian_blur_3x3.hlsl.dxil.h"
@@ -76,6 +78,8 @@
7678
#include "shader/msl/csd_filter_ps.metal.metallib.h"
7779
#include "shader/msl/csd_no_tex_vs.metal.metallib.h"
7880
#include "shader/msl/csd_vs.metal.metallib.h"
81+
#include "shader/msl/enhanced_burnout_blur_vs.metal.metallib.h"
82+
#include "shader/msl/enhanced_burnout_blur_ps.metal.metallib.h"
7983
#include "shader/msl/enhanced_motion_blur_ps.metal.metallib.h"
8084
#include "shader/msl/gamma_correction_ps.metal.metallib.h"
8185
#include "shader/msl/gaussian_blur_3x3.metal.metallib.h"
@@ -101,6 +105,8 @@
101105
#include "shader/hlsl/csd_filter_ps.hlsl.spirv.h"
102106
#include "shader/hlsl/csd_no_tex_vs.hlsl.spirv.h"
103107
#include "shader/hlsl/csd_vs.hlsl.spirv.h"
108+
#include "shader/hlsl/enhanced_burnout_blur_vs.hlsl.spirv.h"
109+
#include "shader/hlsl/enhanced_burnout_blur_ps.hlsl.spirv.h"
104110
#include "shader/hlsl/enhanced_motion_blur_ps.hlsl.spirv.h"
105111
#include "shader/hlsl/gamma_correction_ps.hlsl.spirv.h"
106112
#include "shader/hlsl/gaussian_blur_3x3.hlsl.spirv.h"
@@ -1466,6 +1472,8 @@ static std::unique_ptr<GuestShader> g_gaussianBlurShaders[GAUSSIAN_BLUR_COUNT];
14661472
static std::unique_ptr<GuestShader> g_csdFilterShader;
14671473
static GuestShader* g_csdShader;
14681474

1475+
static std::unique_ptr<GuestShader> g_enhancedBurnoutBlurVSShader;
1476+
static std::unique_ptr<GuestShader> g_enhancedBurnoutBlurPSShader;
14691477
static std::unique_ptr<GuestShader> g_enhancedMotionBlurShader;
14701478

14711479
#if defined(MARATHON_RECOMP_D3D12)
@@ -2205,6 +2213,12 @@ bool Video::CreateHostDevice(const char *sdlVideoDriver, bool graphicsApiRetry)
22052213
g_csdFilterShader = std::make_unique<GuestShader>(ResourceType::PixelShader);
22062214
g_csdFilterShader->shader = CREATE_SHADER(csd_filter_ps);
22072215

2216+
g_enhancedBurnoutBlurVSShader = std::make_unique<GuestShader>(ResourceType::VertexShader);
2217+
g_enhancedBurnoutBlurVSShader->shader = CREATE_SHADER(enhanced_burnout_blur_vs);
2218+
2219+
g_enhancedBurnoutBlurPSShader = std::make_unique<GuestShader>(ResourceType::PixelShader);
2220+
g_enhancedBurnoutBlurPSShader->shader = CREATE_SHADER(enhanced_burnout_blur_ps);
2221+
22082222
g_enhancedMotionBlurShader = std::make_unique<GuestShader>(ResourceType::PixelShader);
22092223
g_enhancedMotionBlurShader->shader = CREATE_SHADER(enhanced_motion_blur_ps);
22102224

@@ -5483,7 +5497,18 @@ static void SetVertexShader(GuestDevice* device, GuestShader* shader)
54835497

54845498
static void ProcSetVertexShader(const RenderCommand& cmd)
54855499
{
5486-
SetDirtyValue(g_dirtyStates.pipelineState, g_pipelineState.vertexShader, cmd.setVertexShader.shader);
5500+
GuestShader* shader = cmd.setVertexShader.shader;
5501+
5502+
if (shader != nullptr &&
5503+
shader->shaderCacheEntry != nullptr)
5504+
{
5505+
if (shader->shaderCacheEntry->hash == 0x3687D038CE7D0BEA || shader->shaderCacheEntry->hash == 0xB4DA7A442DBB16CC)
5506+
{
5507+
shader = g_enhancedBurnoutBlurVSShader.get();
5508+
}
5509+
}
5510+
5511+
SetDirtyValue(g_dirtyStates.pipelineState, g_pipelineState.vertexShader, shader);
54875512
}
54885513

54895514
static void SetStreamSource(GuestDevice* device, uint32_t index, GuestBuffer* buffer, uint32_t offset, uint32_t stride)
@@ -5549,6 +5574,16 @@ static void SetPixelShader(GuestDevice* device, GuestShader* shader)
55495574
static void ProcSetPixelShader(const RenderCommand& cmd)
55505575
{
55515576
GuestShader* shader = cmd.setPixelShader.shader;
5577+
5578+
if (shader != nullptr &&
5579+
shader->shaderCacheEntry != nullptr)
5580+
{
5581+
if (shader->shaderCacheEntry->hash == 0xDA58F0110A8595D9 || shader->shaderCacheEntry->hash == 0x845A4EF989446C01)
5582+
{
5583+
shader = g_enhancedBurnoutBlurPSShader.get();
5584+
}
5585+
}
5586+
55525587
SetDirtyValue(g_dirtyStates.pipelineState, g_pipelineState.pixelShader, shader);
55535588
}
55545589

0 commit comments

Comments
 (0)