4
struct VertexToFragment {
6
float4 pos [[position]];
16
#if COMPILE_FullscreenVert
17
vertex VertexToFragment FullscreenVert(uint vI [[vertex_id]]) {
18
float2 inTex = float2((vI << 1) & 2, vI & 2);
21
out.pos = float4(inTex * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);
27
fragment float4 BlitFrom2D(
28
VertexToFragment input [[stage_in]],
29
constant SourceRegion &sourceRegion [[buffer(0)]],
30
texture2d<float> sourceTexture [[texture(0)]],
31
sampler sourceSampler [[sampler(0)]])
33
float2 newCoord = sourceRegion.UVLeftTop + sourceRegion.UVDimensions * input.tex;
34
return sourceTexture.sample(sourceSampler, newCoord, level(sourceRegion.MipLevel));
38
#if COMPILE_BlitFrom2DArray
39
fragment 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)]])
45
float2 newCoord = sourceRegion.UVLeftTop + sourceRegion.UVDimensions * input.tex;
46
return sourceTexture.sample(sourceSampler, newCoord, (uint)sourceRegion.LayerOrDepth, level(sourceRegion.MipLevel));
51
fragment float4 BlitFrom3D(
52
VertexToFragment input [[stage_in]],
53
constant SourceRegion &sourceRegion [[buffer(0)]],
54
texture3d<float> sourceTexture [[texture(0)]],
55
sampler sourceSampler [[sampler(0)]])
57
float2 newCoord = sourceRegion.UVLeftTop + sourceRegion.UVDimensions * input.tex;
58
return sourceTexture.sample(sourceSampler, float3(newCoord, sourceRegion.LayerOrDepth), level(sourceRegion.MipLevel));
62
#if COMPILE_BlitFromCube
63
fragment float4 BlitFromCube(
64
VertexToFragment input [[stage_in]],
65
constant SourceRegion &sourceRegion [[buffer(0)]],
66
texturecube<float> sourceTexture [[texture(0)]],
67
sampler sourceSampler [[sampler(0)]])
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;
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
83
return sourceTexture.sample(sourceSampler, newCoord, level(sourceRegion.MipLevel));