From 0de70f74584648b738b4e76c8e8090cf3942178b Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Sun, 16 Mar 2025 14:08:36 -0400 Subject: [PATCH] MSL Shader Common Signed-off-by: Isaac Marovitz --- XenosRecomp/shader_common_msl.metal | 248 ++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 XenosRecomp/shader_common_msl.metal diff --git a/XenosRecomp/shader_common_msl.metal b/XenosRecomp/shader_common_msl.metal new file mode 100644 index 0000000..ecc377e --- /dev/null +++ b/XenosRecomp/shader_common_msl.metal @@ -0,0 +1,248 @@ +#include + +using namespace metal; + +constant uint G_SPEC_CONSTANT [[function_constant(0)]]; + +uint g_SpecConstants() { + return G_SPEC_CONSTANT; +} + +constant uint SPEC_CONSTANT_R11G11B10_NORMAL = (1 << 0); +constant uint SPEC_CONSTANT_ALPHA_TEST = (1 << 1); +constant uint SPEC_CONSTANT_BICUBIC_GI_FILTER = (1 << 2); +constant uint SPEC_CONSTANT_ALPHA_TO_COVERAGE = (1 << 3); +constant uint SPEC_CONSTANT_REVERSE_Z = (1 << 4); + +struct PushConstants +{ + ulong vertexShaderConstants; + ulong pixelShaderConstants; + ulong sharedConstants; +}; + +uint g_Booleans(constant PushConstants& constants) +{ + return *(reinterpret_cast(constants.sharedConstants + 256)); +} + +uint g_SwappedTexcoords(constant PushConstants& constants) +{ + return *(reinterpret_cast(constants.sharedConstants + 260)); +} + +float g_AlphaThreshold(constant PushConstants& constants) +{ + return *(reinterpret_cast(constants.sharedConstants + 264)); +} + +struct Texture2DDescriptorHeap +{ + array, 1> g [[id(0)]]; +}; + +struct Texture3DDescriptorHeap +{ + array, 1> g [[id(0)]]; +}; + +struct TextureCubeDescriptorHeap +{ + array, 1> g [[id(0)]]; +}; + +struct SamplerDescriptorHeap +{ + array g [[id(0)]]; +}; + +uint2 getTexture2DDimensions(texture2d texture) +{ + return uint2(texture.get_width(), texture.get_height()); +} + +float4 tfetch2D(constant Texture2DDescriptorHeap& textureHeap, + constant SamplerDescriptorHeap& samplerHeap, + uint resourceDescriptorIndex, + uint samplerDescriptorIndex, + float2 texCoord, float2 offset) +{ + texture2d texture = textureHeap.g[resourceDescriptorIndex]; + sampler sampler = samplerHeap.g[samplerDescriptorIndex]; + return texture.sample(sampler, texCoord + offset / (float2)getTexture2DDimensions(texture)); +} + +float2 getWeights2D(constant Texture2DDescriptorHeap& textureHeap, + constant SamplerDescriptorHeap& samplerHeap, + uint resourceDescriptorIndex, + uint samplerDescriptorIndex, + float2 texCoord, float2 offset) +{ + texture2d texture = textureHeap.g[resourceDescriptorIndex]; + return select(fract(texCoord * (float2)getTexture2DDimensions(texture) + offset - 0.5), 0.0, isnan(texCoord)); +} + +float w0(float a) +{ + return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f); +} + +float w1(float a) +{ + return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f); +} + +float w2(float a) +{ + return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f); +} + +float w3(float a) +{ + return (1.0f / 6.0f) * (a * a * a); +} + +float g0(float a) +{ + return w0(a) + w1(a); +} + +float g1(float a) +{ + return w2(a) + w3(a); +} + +float h0(float a) +{ + return -1.0f + w1(a) / (w0(a) + w1(a)) + 0.5f; +} + +float h1(float a) +{ + return 1.0f + w3(a) / (w2(a) + w3(a)) + 0.5f; +} + +float4 tfetch2DBicubic(constant Texture2DDescriptorHeap& textureHeap, + constant SamplerDescriptorHeap& samplerHeap, + uint resourceDescriptorIndex, + uint samplerDescriptorIndex, + float2 texCoord, float2 offset) +{ + texture2d texture = textureHeap.g[resourceDescriptorIndex]; + sampler sampler = samplerHeap.g[samplerDescriptorIndex]; + uint2 dimensions = getTexture2DDimensions(texture); + + float x = texCoord.x * dimensions.x + offset.x; + float y = texCoord.y * dimensions.y + offset.y; + + x -= 0.5f; + y -= 0.5f; + float px = floor(x); + float py = floor(y); + float fx = x - px; + float fy = y - py; + + float g0x = g0(fx); + float g1x = g1(fx); + float h0x = h0(fx); + float h1x = h1(fx); + float h0y = h0(fy); + float h1y = h1(fy); + + float4 r = + g0(fy) * (g0x * texture.sample(sampler, float2(px + h0x, py + h0y) / float2(dimensions)) + + g1x * texture.sample(sampler, float2(px + h1x, py + h0y) / float2(dimensions))) + + g1(fy) * (g0x * texture.sample(sampler, float2(px + h0x, py + h1y) / float2(dimensions)) + + g1x * texture.sample(sampler, float2(px + h1x, py + h1y) / float2(dimensions))); + + return r; +} + +float4 tfetch3D(constant Texture3DDescriptorHeap& textureHeap, + constant SamplerDescriptorHeap& samplerHeap, + uint resourceDescriptorIndex, + uint samplerDescriptorIndex, + float3 texCoord) +{ + texture3d texture = textureHeap.g[resourceDescriptorIndex]; + sampler sampler = samplerHeap.g[samplerDescriptorIndex]; + return texture.sample(sampler, texCoord); +} + +struct CubeMapData +{ + float3 cubeMapDirections[2]; + uint cubeMapIndex; +}; + +float4 tfetchCube(constant TextureCubeDescriptorHeap& textureHeap, + constant SamplerDescriptorHeap& samplerHeap, + uint resourceDescriptorIndex, + uint samplerDescriptorIndex, + float3 texCoord, thread CubeMapData* cubeMapData) +{ + texturecube texture = textureHeap.g[resourceDescriptorIndex]; + sampler sampler = samplerHeap.g[samplerDescriptorIndex]; + return texture.sample(sampler, cubeMapData->cubeMapDirections[(uint)texCoord.z]); +} + +float4 tfetchR11G11B10(uint4 value) +{ + if (g_SpecConstants() & SPEC_CONSTANT_R11G11B10_NORMAL) + { + return float4( + (value.x & 0x00000400 ? -1.0 : 0.0) + ((value.x & 0x3FF) / 1024.0), + (value.x & 0x00200000 ? -1.0 : 0.0) + (((value.x >> 11) & 0x3FF) / 1024.0), + (value.x & 0x80000000 ? -1.0 : 0.0) + (((value.x >> 22) & 0x1FF) / 512.0), + 0.0); + } + else + { + return (float4)value; + } +} + +float4 tfetchTexcoord(uint swappedTexcoords, float4 value, uint semanticIndex) +{ + return (swappedTexcoords & (1ull << semanticIndex)) != 0 ? value.yxwz : value; +} + +float4 cube(float4 value, thread CubeMapData* cubeMapData) +{ + uint index = cubeMapData->cubeMapIndex; + cubeMapData->cubeMapDirections[index] = value.xyz; + ++cubeMapData->cubeMapIndex; + + return float4(0.0, 0.0, 0.0, index); +} + +float4 dst(float4 src0, float4 src1) +{ + float4 dest; + dest.x = 1.0; + dest.y = src0.y * src1.y; + dest.z = src0.z; + dest.w = src1.w; + return dest; +} + +float4 max4(float4 src0) +{ + return max(src0.x, max3(src0.y, src0.z, src0.w)); +} + +float2 getPixelCoord(constant Texture2DDescriptorHeap& textureHeap, + uint resourceDescriptorIndex, + float2 texCoord) +{ + texture2d texture = textureHeap.g[resourceDescriptorIndex]; + return (float2)getTexture2DDimensions(texture) * texCoord; +} + +float computeMipLevel(float2 pixelCoord) +{ + float2 dx = dfdx(pixelCoord); + float2 dy = dfdy(pixelCoord); + float deltaMaxSqr = max(dot(dx, dx), dot(dy, dy)); + return max(0.0, 0.5 * log2(deltaMaxSqr)); +}