Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
#version 450
/* Copyright (c) 2020-2022 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#extension GL_EXT_samplerless_texture_functions : require
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
layout(set = 0, binding = 0) writeonly uniform image2D uOutput;
layout(set = 0, binding = 1) uniform utexture2D uInput;
layout(constant_id = 0) const int COMPONENTS = 1;
layout(push_constant) uniform Registers
{
ivec2 resolution;
} registers;
ivec2 build_coord()
{
ivec2 base = ivec2(gl_WorkGroupID.xy) * 8;
base.x += 4 * (int(gl_LocalInvocationID.z) & 1);
base.y += 2 * (int(gl_LocalInvocationID.z) & 2);
base += ivec2(gl_LocalInvocationID.xy);
return base;
}
uint flip_endian(uint v)
{
uvec4 words = uvec4(v) >> uvec4(0, 8, 16, 24);
words &= 0xffu;
return (words.x << 24u) | (words.y << 16u) | (words.z << 8u) | (words.w << 0u);
}
uvec2 flip_endian(uvec2 v)
{
return uvec2(flip_endian(v.y), flip_endian(v.x));
}
const ivec4 etc2_alpha_modifier_table[16] = ivec4[](
ivec4(2, 5, 8, 14),
ivec4(2, 6, 9, 12),
ivec4(1, 4, 7, 12),
ivec4(1, 3, 5, 12),
ivec4(2, 5, 7, 11),
ivec4(2, 6, 8, 10),
ivec4(3, 6, 7, 10),
ivec4(2, 4, 7, 10),
ivec4(1, 5, 7, 9),
ivec4(1, 4, 7, 9),
ivec4(1, 3, 7, 9),
ivec4(1, 4, 6, 9),
ivec4(2, 3, 6, 9),
ivec4(0, 1, 2, 9),
ivec4(3, 5, 7, 8),
ivec4(2, 4, 6, 8)
);
uint decode_eac_alpha(uvec2 payload, int linear_pixel)
{
int bit_offset = 45 - 3 * linear_pixel;
int base = int(bitfieldExtract(payload.y, 24, 8)) * 8 + 4;
int multiplier = max(int(bitfieldExtract(payload.y, 20, 4)) * 8, 1);
int table = int(bitfieldExtract(payload.y, 16, 4));
int lsb_index = int(bitfieldExtract(payload[bit_offset >> 5], bit_offset & 31, 2));
bit_offset += 2;
int msb = int((payload[bit_offset >> 5] >> (bit_offset & 31)) & 1);
int mod = etc2_alpha_modifier_table[table][lsb_index] ^ (msb - 1);
int a = base + mod * multiplier;
return clamp(a, 0, 2047);
}
void main()
{
ivec2 coord = build_coord();
if (any(greaterThanEqual(coord, registers.resolution)))
return;
ivec2 tile_coord = coord >> 2;
ivec2 pixel_coord = coord & 3;
int linear_pixel = 4 * pixel_coord.x + pixel_coord.y;
uvec4 payload = texelFetch(uInput, tile_coord, 0);
uint r = decode_eac_alpha(flip_endian(payload.xy), linear_pixel);
if (COMPONENTS == 1)
imageStore(uOutput, coord, vec4(float(r) / 2047.0, 0.0, 0.0, 1.0));
else if (COMPONENTS == 2)
{
uint g = decode_eac_alpha(flip_endian(payload.zw), linear_pixel);
imageStore(uOutput, coord, vec4(float(r) / 2047.0, float(g) / 2047.0, 0.0, 1.0));
}
}