The open source OpenXR runtime
1// Copyright 2021-2023, Collabora Ltd.
2// Author: Jakob Bornecrantz <jakob@collabora.com>
3// SPDX-License-Identifier: BSL-1.0
4
5#version 460
6#extension GL_GOOGLE_include_directive : require
7
8#include "srgb.inc.glsl"
9
10layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
11
12layout(set = 0, binding = 0) uniform sampler2D source;
13layout(set = 0, binding = 1, rgba8) uniform writeonly restrict image2D target;
14layout(push_constant) uniform Config
15{
16 vec4 src_rect;
17 ivec4 target_view;
18} push;
19
20vec2 position_to_uv(ivec2 extent, uint ix, uint iy)
21{
22 // Turn the index into floating point.
23 vec2 xy = vec2(float(ix), float(iy));
24
25 // The inverse of the extent of the target image is the pixel size in [0 .. 1] space.
26 vec2 extent_pixel_size = vec2(1.0 / float(extent.x), 1.0 / float(extent.y));
27
28 // Per-target pixel we move the size of the pixels.
29 vec2 uv = xy * extent_pixel_size;
30
31 // Emulate a triangle sample position by offset half target pixel size.
32 uv = uv + extent_pixel_size / 2.0;
33
34 // Transform with the normalized sample rect.
35 uv = push.src_rect.xy + (uv * push.src_rect.zw);
36
37 return uv;
38}
39
40void main()
41{
42 uint ix = gl_GlobalInvocationID.x;
43 uint iy = gl_GlobalInvocationID.y;
44
45 ivec2 offset = ivec2(push.target_view.xy);
46 ivec2 extent = ivec2(push.target_view.zw);
47
48 if (ix >= extent.x || iy >= extent.y) {
49 return;
50 }
51
52 // Get the UV we should sample from.
53 vec2 uv = position_to_uv(extent, ix, iy);
54
55 // Do the sample.
56 vec4 rgba = texture(source, uv);
57
58 // Do colour correction here since there are no automatic conversion in hardware available.
59 rgba = vec4(from_linear_to_srgb(rgba.rgb), rgba.a);
60
61 // And finally write out.
62 imageStore(target, ivec2(offset.x + ix, offset.y + iy), rgba);
63}