SDL

Форк
0
/
Metal_Blit.metal 
85 строк · 3.0 Кб
1
#include <metal_stdlib>
2
using namespace metal;
3

4
struct VertexToFragment {
5
    float2 tex;
6
    float4 pos [[position]];
7
};
8

9
struct SourceRegion {
10
    float2 UVLeftTop;
11
    float2 UVDimensions;
12
    uint MipLevel;
13
    float LayerOrDepth;
14
};
15

16
#if COMPILE_FullscreenVert
17
vertex 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
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)]])
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
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)]])
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
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)]])
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
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)]])
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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.