Atlas - Metal_Blit.metal
Home / ext / SDL / src / gpu / metal Lines: 1 | Size: 4223 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1#include <metal_stdlib> 2using namespace metal; 3 4struct VertexToFragment { 5 float2 tex; 6 float4 pos [[position]]; 7}; 8 9struct SourceRegion { 10 float2 UVLeftTop; 11 float2 UVDimensions; 12 uint MipLevel; 13 float LayerOrDepth; 14}; 15 16#if COMPILE_FullscreenVert 17vertex VertexToFragment FullscreenVert(uint vI [[vertex_id]]) { 18 float2 inTex = float2((vI << 1) & 2, vI & 2); 19 VertexToFragment out; 20 out.tex = inTex; 21 out.pos = float4(inTex * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f); 22 return out; 23} 24#endif 25 26#if COMPILE_BlitFrom2D 27fragment float4 BlitFrom2D( 28 VertexToFragment input [[stage_in]], 29 constant SourceRegion &sourceRegion [[buffer(0)]], 30 texture2d<float> sourceTexture [[texture(0)]], 31 sampler sourceSampler [[sampler(0)]]) 32{ 33 float2 newCoord = sourceRegion.UVLeftTop + sourceRegion.UVDimensions * input.tex; 34 return sourceTexture.sample(sourceSampler, newCoord, level(sourceRegion.MipLevel)); 35} 36#endif 37 38#if COMPILE_BlitFrom2DArray 39fragment float4 BlitFrom2DArray( 40 VertexToFragment input [[stage_in]], 41 constant SourceRegion &sourceRegion [[buffer(0)]], 42 texture2d_array<float> sourceTexture [[texture(0)]], 43 sampler sourceSampler [[sampler(0)]]) 44{ 45 float2 newCoord = sourceRegion.UVLeftTop + sourceRegion.UVDimensions * input.tex; 46 return sourceTexture.sample(sourceSampler, newCoord, (uint)sourceRegion.LayerOrDepth, level(sourceRegion.MipLevel)); 47} 48#endif 49 50#if COMPILE_BlitFrom3D 51fragment float4 BlitFrom3D( 52 VertexToFragment input [[stage_in]], 53 constant SourceRegion &sourceRegion [[buffer(0)]], 54 texture3d<float> sourceTexture [[texture(0)]], 55 sampler sourceSampler [[sampler(0)]]) 56{ 57 float2 newCoord = sourceRegion.UVLeftTop + sourceRegion.UVDimensions * input.tex; 58 return sourceTexture.sample(sourceSampler, float3(newCoord, sourceRegion.LayerOrDepth), level(sourceRegion.MipLevel)); 59} 60#endif 61 62#if COMPILE_BlitFromCube 63fragment float4 BlitFromCube( 64 VertexToFragment input [[stage_in]], 65 constant SourceRegion &sourceRegion [[buffer(0)]], 66 texturecube<float> sourceTexture [[texture(0)]], 67 sampler sourceSampler [[sampler(0)]]) 68{ 69 // Thanks, Wikipedia! https://en.wikipedia.org/wiki/Cube_mapping 70 float2 scaledUV = sourceRegion.UVLeftTop + sourceRegion.UVDimensions * input.tex; 71 float u = 2.0 * scaledUV.x - 1.0; 72 float v = 2.0 * scaledUV.y - 1.0; 73 float3 newCoord; 74 switch ((uint)sourceRegion.LayerOrDepth) { 75 case 0: newCoord = float3(1.0, -v, -u); break; // POSITIVE X 76 case 1: newCoord = float3(-1.0, -v, u); break; // NEGATIVE X 77 case 2: newCoord = float3(u, 1.0, -v); break; // POSITIVE Y 78 case 3: newCoord = float3(u, -1.0, v); break; // NEGATIVE Y 79 case 4: newCoord = float3(u, -v, 1.0); break; // POSITIVE Z 80 case 5: newCoord = float3(-u, -v, -1.0); break; // NEGATIVE Z 81 default: newCoord = float3(0, 0, 0); break; // silences warning 82 } 83 return sourceTexture.sample(sourceSampler, newCoord, level(sourceRegion.MipLevel)); 84} 85#endif 86 87#if COMPILE_BlitFromCubeArray 88fragment float4 BlitFromCubeArray( 89 VertexToFragment input [[stage_in]], 90 constant SourceRegion &sourceRegion [[buffer(0)]], 91 texturecube_array<float> sourceTexture [[texture(0)]], 92 sampler sourceSampler [[sampler(0)]]) 93{ 94 // Thanks, Wikipedia! https://en.wikipedia.org/wiki/Cube_mapping 95 float2 scaledUV = sourceRegion.UVLeftTop + sourceRegion.UVDimensions * input.tex; 96 float u = 2.0 * scaledUV.x - 1.0; 97 float v = 2.0 * scaledUV.y - 1.0; 98 float3 newCoord; 99 switch (((uint)sourceRegion.LayerOrDepth) % 6) { 100 case 0: newCoord = float3(1.0, -v, -u); break; // POSITIVE X 101 case 1: newCoord = float3(-1.0, -v, u); break; // NEGATIVE X 102 case 2: newCoord = float3(u, 1.0, -v); break; // POSITIVE Y 103 case 3: newCoord = float3(u, -1.0, v); break; // NEGATIVE Y 104 case 4: newCoord = float3(u, -v, 1.0); break; // POSITIVE Z 105 case 5: newCoord = float3(-u, -v, -1.0); break; // NEGATIVE Z 106 default: newCoord = float3(0, 0, 0); break; // silences warning 107 } 108 return sourceTexture.sample(sourceSampler, newCoord, (uint)sourceRegion.LayerOrDepth / 6, level(sourceRegion.MipLevel)); 109} 110#endif 111[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.