yoshisuga / MAME4iOS

Multiple Arcade Machine Emulator for iOS, iPadOS, tvOS, macOS (Catalyst)
Other
674 stars 105 forks source link

add Shader FXAA #445

Closed LiuSky closed 1 year ago

LiuSky commented 1 year ago

include

include "ShaderTypes.h"

using namespace metal;

constant float4 FXAA_CONSTANTS = {1.0/float4(128.0, 128.0, 128.0, 128.0), 8.0, 0.125, 0.25};

fragment float4 fxaa_fragment_shader(FxaaVertexIO in [[stage_in]], texture2d colorTexture [[texture(0)]]) { float3 rgbNW = colorTexture.sample(colorTexture.sampler, in.texCoord + (float2(-1.0, -1.0) FXAA_CONSTANTS.zw)).rgb; float3 rgbNE = colorTexture.sample(colorTexture.sampler, in.texCoord + (float2( 1.0, -1.0) FXAA_CONSTANTS.zw)).rgb; float3 rgbSW = colorTexture.sample(colorTexture.sampler, in.texCoord + (float2(-1.0, 1.0) FXAA_CONSTANTS.zw)).rgb; float3 rgbSE = colorTexture.sample(colorTexture.sampler, in.texCoord + (float2( 1.0, 1.0) FXAA_CONSTANTS.zw)).rgb; float3 rgbM = colorTexture.sample(colorTexture.sampler, in.texCoord).rgb;

float3 luma = float3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM  = dot(rgbM,  luma);

float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));

float2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));

float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_CONSTANTS.w), FXAA_CONSTANTS.y);

float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);

dir = min(float2(FXAA_CONSTANTS.x,  FXAA_CONSTANTS.x * (dir.y / dir.x)), 
          float2(FXAA_CONSTANTS.x * (dir.x / dir.y), FXAA_CONSTANTS.x));

float4 rgbA = (1.0/2.0) * 
    (colorTexture.sample(colorTexture.sampler, in.texCoord.xy + dir * (1.0/3.0 - 0.5)).xyz +
     colorTexture.sample(colorTexture.sampler, in.texCoord.xy + dir * (2.0/3.0 - 0.5)).xyz);

float4 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * 
    (colorTexture.sample(colorTexture.sampler, in.texCoord.xy + dir * (0.0/3.0 - 0.5)).xyz +
     colorTexture.sample(colorTexture.sampler, in.texCoord.xy + dir * (3.0/3.0 - 0.5)).xyz);

float lumaB = dot(rgbB.rgb, luma);

if ((lumaB < lumaMin) || (lumaB > lumaMax))
{
    return float4(rgbA, 1.0);
}
else
{
    return float4(rgbB, 1.0);
}

}

ToddLa commented 1 year ago

What does dumping a bunch of code into an issue mean? Is this a feature, ie a pull request?

LiuSky commented 1 year ago

Okay, I'll make a push request

LiuSky commented 1 year ago

Sorry, after 3 days, I still don't know how to solve this shader problem @ToddLa

ToddLa commented 1 year ago

Describe the problem in words, not just dumping code.

What are you trying to do? What features do you want?

LiuSky commented 1 year ago

I'm sorry, I shouldn't have just posted the code on it. I see that the mame core library has a bgfx folder, which has a chains folder, and then there is an xbr-lv2-fast.json function under the xbr file, which can make the rendering screen without serrated. Specific address at https://github.com/mamedev/mame/blob/master/bgfx/chains/xbr/xbr-lv2-fast.json. @ToddLa

LiuSky commented 1 year ago

The effect was very clear, but I did not know how to do it, and then I went to look up some information and said that using FXAA

截屏2023-06-17 00 51 57
ToddLa commented 1 year ago

MAME4iOS does not use BGFX (or OpenGL) it has its own Metal only renderer.

So no BGFX or shader chains info is gonna apply.

The Shaders it does use are hard coded, a plug-in Shader system is being investigated

LiuSky commented 1 year ago

Thank you very much for your explanation