| 1 |
#[versions] |
| 2 |
|
| 3 |
version_float = "#define VER_FLOAT"; |
| 4 |
version_half = "#define VER_HALF"; |
| 5 |
version_unorm8 = "#define VER_UINT8"; |
| 6 |
version_unorm16 = "#define VER_UINT16"; |
| 7 |
|
| 8 |
#[compute] |
| 9 |
#version 450 |
| 10 |
|
| 11 |
#VERSION_DEFINES |
| 12 |
|
| 13 |
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; |
| 14 |
|
| 15 |
layout(std430, binding = 0) buffer Source { |
| 16 |
#if defined(VER_FLOAT) |
| 17 |
float data[]; |
| 18 |
#else |
| 19 |
uint data[]; |
| 20 |
#endif |
| 21 |
} |
| 22 |
source; |
| 23 |
|
| 24 |
#if defined(VER_FLOAT) |
| 25 |
layout(binding = 1, rgba32f) uniform writeonly image2D dest; |
| 26 |
#elif defined(VER_HALF) |
| 27 |
layout(binding = 1, rgba16f) uniform writeonly image2D dest; |
| 28 |
#elif defined(VER_UINT8) |
| 29 |
layout(binding = 1, rgba8) uniform writeonly image2D dest; |
| 30 |
#elif defined(VER_UINT16) |
| 31 |
layout(binding = 1, rgba16) uniform writeonly image2D dest; |
| 32 |
#endif |
| 33 |
|
| 34 |
layout(push_constant, std430) uniform Params { |
| 35 |
uint p_width; |
| 36 |
uint p_height; |
| 37 |
uint p_padding[2]; |
| 38 |
} |
| 39 |
params; |
| 40 |
|
| 41 |
void main() { |
| 42 |
// gl_GlobalInvocationID is equivalent to the current texel coordinates. |
| 43 |
if (gl_GlobalInvocationID.x >= params.p_width || gl_GlobalInvocationID.y >= params.p_height) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
// The index of a texel in the source buffer, NOT an index of source.data[] |
| 48 |
const int texel_index = int(gl_GlobalInvocationID.y * params.p_width + gl_GlobalInvocationID.x); |
| 49 |
|
| 50 |
#if defined(VER_FLOAT) |
| 51 |
// Since 32-bit floats are aligned with RGBF texel data, just retrieve the values from the array. |
| 52 |
// Multiply by 3 to align with the components. |
| 53 |
|
| 54 |
int data_index = texel_index * 3; |
| 55 |
vec3 color_rgb = vec3(source.data[data_index], source.data[data_index + 1], source.data[data_index + 2]); |
| 56 |
|
| 57 |
#elif defined(VER_UINT8) |
| 58 |
// RGB8 texel data and 32-bit uints are not aligned, so we have to use a bit of magic. |
| 59 |
// The source texel can be in either of 4 alignment 'states': |
| 60 |
// 0 - [ XYZ_-____ ] |
| 61 |
// 1 - [ _YZW-____ ] |
| 62 |
// 2 - [ __ZW-X___ ] |
| 63 |
// 3 - [ ___W-XY__ ] |
| 64 |
// The texel index additionally needs to be decremented after every 'cycle' in order to properly fit into the source array. |
| 65 |
|
| 66 |
vec3 color_rgb = vec3(0.0); |
| 67 |
int data_index = texel_index - (texel_index / 4); |
| 68 |
|
| 69 |
switch ((texel_index * 3) % 4) { |
| 70 |
case 0: |
| 71 |
color_rgb = unpackUnorm4x8(source.data[data_index]).xyz; |
| 72 |
break; |
| 73 |
case 1: |
| 74 |
color_rgb = unpackUnorm4x8(source.data[data_index - 1]).yzw; |
| 75 |
break; |
| 76 |
case 2: |
| 77 |
color_rgb.rg = unpackUnorm4x8(source.data[data_index - 1]).zw; |
| 78 |
color_rgb.b = unpackUnorm4x8(source.data[data_index]).x; |
| 79 |
break; |
| 80 |
case 3: |
| 81 |
color_rgb.r = unpackUnorm4x8(source.data[data_index - 1]).w; |
| 82 |
color_rgb.gb = unpackUnorm4x8(source.data[data_index]).xy; |
| 83 |
break; |
| 84 |
default: |
| 85 |
break; |
| 86 |
} |
| 87 |
|
| 88 |
#else |
| 89 |
// In a similar vein to RGB8, the RGBH/RGB16 source texel can be in either of 2 alignment 'states': |
| 90 |
// 0 - [ XY-X_ ] |
| 91 |
// 1 - [ _Y-XY ] |
| 92 |
// The texel index has to be incremented this time, as the size of a texel (6 bytes) is greater than that of a 32-bit uint (4 bytes). |
| 93 |
|
| 94 |
vec3 color_rgb = vec3(0.0); |
| 95 |
int data_index = texel_index + (texel_index / 2); |
| 96 |
|
| 97 |
switch ((texel_index * 3) % 2) { |
| 98 |
#if defined(VER_HALF) |
| 99 |
case 0: |
| 100 |
color_rgb.xy = unpackHalf2x16(source.data[data_index]); |
| 101 |
color_rgb.z = unpackHalf2x16(source.data[data_index + 1]).x; |
| 102 |
break; |
| 103 |
case 1: |
| 104 |
color_rgb.x = unpackHalf2x16(source.data[data_index]).y; |
| 105 |
color_rgb.yz = unpackHalf2x16(source.data[data_index + 1]); |
| 106 |
break; |
| 107 |
#elif defined(VER_UINT16) |
| 108 |
case 0: |
| 109 |
color_rgb.xy = unpackUnorm2x16(source.data[data_index]); |
| 110 |
color_rgb.z = unpackUnorm2x16(source.data[data_index + 1]).x; |
| 111 |
break; |
| 112 |
case 1: |
| 113 |
color_rgb.x = unpackUnorm2x16(source.data[data_index]).y; |
| 114 |
color_rgb.yz = unpackUnorm2x16(source.data[data_index + 1]); |
| 115 |
break; |
| 116 |
#endif |
| 117 |
default: |
| 118 |
break; |
| 119 |
} |
| 120 |
#endif |
| 121 |
|
| 122 |
// Store the resulting RGBA color. |
| 123 |
imageStore(dest, ivec2(gl_GlobalInvocationID.xy), vec4(color_rgb, 1.0)); |
| 124 |
} |