mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
Mali bridge (#517, by ASEbrahim) on the non-Adreno test branch
Squash of Droid-Deck/DroidDeck#517 (ASEbrahim/DroidDeck mali-bridge @ 6b823341): the Vulkan
bridge (tools/vkbridge) that runs the guest's Vulkan on the phone's own driver, with BC1-BC7,
clip/cull distance, vertex-divisor and depth-clip emulation and dma-heap memory sharing for Mali.
All of the bridge is ASEbrahim's work; squashed only to carry it on this test branch.
Combined with #570 on this branch, a device draws through exactly one path:
- Adreno: Turnip, unchanged (checked first; the bridge setting is ignored there).
- PowerVR / Xclipse with the system HAL and a render node: the system-Vulkan path (#570).
- Anything else with the Setup toggle on (Mali): the bridge.
This commit is contained in:
1 parent
cf06f89c11
commit
ee0fb08268
125 files changed
+119982
-12
No files matched your search
@@ -0,0 +1,364 @@
|
||||
#version 450
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* BCn block decoder for the Mali bridge's BC texture emulation. One invocation decodes one 4x4
|
||||
* block from the copy's source buffer and stores it into the decoded image (a 2D array view of one
|
||||
* mip level). The arithmetic follows bcdec (third_party/bcdec, MIT/Unlicense, Sergii Kudlai) so the
|
||||
* output can be checked bit-for-bit against it. Built once per format family (FMT_BC1 ... FMT_BC7).
|
||||
*/
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
|
||||
layout(std430, binding = 0) readonly buffer Src { uint w[]; } src;
|
||||
#if defined(FMT_BC6H)
|
||||
layout(rgba16f, binding = 1) uniform writeonly image2DArray dst;
|
||||
#elif defined(FMT_BC4)
|
||||
layout(r8, binding = 1) uniform writeonly image2DArray dst;
|
||||
#elif defined(FMT_BC5)
|
||||
layout(rg8, binding = 1) uniform writeonly image2DArray dst;
|
||||
#else
|
||||
layout(rgba8, binding = 1) uniform writeonly image2DArray dst;
|
||||
#endif
|
||||
|
||||
layout(push_constant) uniform PC {
|
||||
uint src_word; /* first block, in 32-bit words from the bound range's start */
|
||||
uint row_blocks; /* blocks per row in the buffer */
|
||||
uint layer_blocks; /* blocks per layer in the buffer */
|
||||
uint flags; /* 1: BC1 without alpha (alpha forced to 1); 2: signed (BC4/5/6H); first layer << 8 */
|
||||
ivec2 dst_off; /* texel offset of the region in the image */
|
||||
uvec2 extent; /* texels to write */
|
||||
} pc;
|
||||
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
#include "bc_tables.glsl"
|
||||
|
||||
uint bits(uvec4 b, uint pos, uint n)
|
||||
{
|
||||
if (n == 0u) return 0u;
|
||||
uint wi = pos >> 5, o = pos & 31u;
|
||||
uint v = b[wi] >> o;
|
||||
if (o + n > 32u && wi < 3u) v |= b[wi + 1u] << (32u - o);
|
||||
return n == 32u ? v : (v & ((1u << n) - 1u));
|
||||
}
|
||||
|
||||
int sext(int v, int n) { return (v << (32 - n)) >> (32 - n); }
|
||||
|
||||
void put(uint tx, uint ty, vec4 v)
|
||||
{
|
||||
uvec2 blk = gl_GlobalInvocationID.xy;
|
||||
uvec2 p = blk * 4u + uvec2(tx, ty);
|
||||
if (p.x < pc.extent.x && p.y < pc.extent.y)
|
||||
imageStore(dst, ivec3(pc.dst_off + ivec2(p), int(gl_GlobalInvocationID.z + (pc.flags >> 8))), v);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ BC1-3 */
|
||||
|
||||
uvec4 rgb565(uint c)
|
||||
{
|
||||
uint r = (c >> 11) & 31u, g = (c >> 5) & 63u, b = c & 31u;
|
||||
return uvec4((r * 527u + 23u) >> 6, (g * 259u + 33u) >> 6, (b * 527u + 23u) >> 6, 255u);
|
||||
}
|
||||
|
||||
void color_block(uint lo, uint idx, bool opaque, inout uvec4 texels[16])
|
||||
{
|
||||
uint c0 = lo & 0xffffu, c1 = lo >> 16;
|
||||
uint r0 = (c0 >> 11) & 31u, g0 = (c0 >> 5) & 63u, b0 = c0 & 31u;
|
||||
uint r1 = (c1 >> 11) & 31u, g1 = (c1 >> 5) & 63u, b1 = c1 & 31u;
|
||||
uvec4 ref[4];
|
||||
ref[0] = rgb565(c0);
|
||||
ref[1] = rgb565(c1);
|
||||
if (c0 > c1 || opaque) {
|
||||
ref[2] = uvec4(((2u * r0 + r1) * 351u + 61u) >> 7, ((2u * g0 + g1) * 2763u + 1039u) >> 11, ((2u * b0 + b1) * 351u + 61u) >> 7, 255u);
|
||||
ref[3] = uvec4(((r0 + r1 * 2u) * 351u + 61u) >> 7, ((g0 + g1 * 2u) * 2763u + 1039u) >> 11, ((b0 + b1 * 2u) * 351u + 61u) >> 7, 255u);
|
||||
} else {
|
||||
ref[2] = uvec4(((r0 + r1) * 1053u + 125u) >> 8, ((g0 + g1) * 4145u + 1019u) >> 11, ((b0 + b1) * 1053u + 125u) >> 8, 255u);
|
||||
ref[3] = uvec4(0u);
|
||||
}
|
||||
for (uint t = 0u; t < 16u; t++) texels[t] = ref[(idx >> (2u * t)) & 3u];
|
||||
}
|
||||
|
||||
/* BC3's alpha block as bcdec decodes it (integer division). */
|
||||
void smooth_alpha(uint lo, uint hi, out uint a[16])
|
||||
{
|
||||
uint a0 = lo & 0xffu, a1 = (lo >> 8) & 0xffu;
|
||||
uint pal[8];
|
||||
pal[0] = a0;
|
||||
pal[1] = a1;
|
||||
if (a0 > a1) {
|
||||
pal[2] = (6u * a0 + a1) / 7u; pal[3] = (5u * a0 + 2u * a1) / 7u; pal[4] = (4u * a0 + 3u * a1) / 7u;
|
||||
pal[5] = (3u * a0 + 4u * a1) / 7u; pal[6] = (2u * a0 + 5u * a1) / 7u; pal[7] = (a0 + 6u * a1) / 7u;
|
||||
} else {
|
||||
pal[2] = (4u * a0 + a1) / 5u; pal[3] = (3u * a0 + 2u * a1) / 5u; pal[4] = (2u * a0 + 3u * a1) / 5u;
|
||||
pal[5] = (a0 + 4u * a1) / 5u; pal[6] = 0u; pal[7] = 255u;
|
||||
}
|
||||
uvec4 b = uvec4(lo, hi, 0u, 0u);
|
||||
for (uint t = 0u; t < 16u; t++) a[t] = pal[bits(b, 16u + 3u * t, 3u)];
|
||||
}
|
||||
|
||||
/* BC4/BC5 channel (bcdec's precise variant), as the byte pattern of the unsigned/signed value. */
|
||||
void bc4_channel(uint lo, uint hi, bool sgn, out uint v[16])
|
||||
{
|
||||
int e0, e1;
|
||||
if (sgn) {
|
||||
e0 = max(sext(int(lo & 0xffu), 8), -127);
|
||||
e1 = max(sext(int((lo >> 8) & 0xffu), 8), -127);
|
||||
} else {
|
||||
e0 = int(lo & 0xffu);
|
||||
e1 = int((lo >> 8) & 0xffu);
|
||||
}
|
||||
const int w6[6] = int[6](9363, 18724, 28086, 37450, 46812, 56173);
|
||||
const int w4[4] = int[4](13107, 26215, 39321, 52429);
|
||||
int pal[8];
|
||||
pal[0] = e0;
|
||||
pal[1] = e1;
|
||||
if (e0 > e1) {
|
||||
for (int i = 0; i < 6; i++) pal[2 + i] = (w6[5 - i] * e0 + w6[i] * e1 + 32768) >> 16;
|
||||
} else {
|
||||
for (int i = 0; i < 4; i++) pal[2 + i] = (w4[3 - i] * e0 + w4[i] * e1 + 32768) >> 16;
|
||||
pal[6] = sgn ? -127 : 0;
|
||||
pal[7] = sgn ? 127 : 255;
|
||||
}
|
||||
uvec4 b = uvec4(lo, hi, 0u, 0u);
|
||||
for (uint t = 0u; t < 16u; t++) v[t] = uint(pal[bits(b, 16u + 3u * t, 3u)]) & 0xffu;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ BC6H */
|
||||
|
||||
int bc6_unquantize(int val, int nbits, bool sgn)
|
||||
{
|
||||
if (!sgn) {
|
||||
if (nbits >= 15) return val;
|
||||
if (val == 0) return 0;
|
||||
if (val == (1 << nbits) - 1) return 0xffff;
|
||||
return ((val << 16) + 0x8000) >> nbits;
|
||||
}
|
||||
if (nbits >= 16) return val;
|
||||
int s = 0;
|
||||
if (val < 0) { s = 1; val = -val; }
|
||||
int unq;
|
||||
if (val == 0) unq = 0;
|
||||
else if (val >= (1 << (nbits - 1)) - 1) unq = 0x7fff;
|
||||
else unq = ((val << 15) + 0x4000) >> (nbits - 1);
|
||||
return s != 0 ? -unq : unq;
|
||||
}
|
||||
|
||||
uint bc6_finish(int val, bool sgn)
|
||||
{
|
||||
if (!sgn) return uint((val * 31) >> 6);
|
||||
val = val < 0 ? -(((-val) * 31) >> 5) : (val * 31) >> 5;
|
||||
uint s = 0u;
|
||||
if (val < 0) { s = 0x8000u; val = -val; }
|
||||
return s | uint(val);
|
||||
}
|
||||
|
||||
void bc6h(uvec4 blk, bool sgn)
|
||||
{
|
||||
uint pos = 0u;
|
||||
uint code = bits(blk, 0u, 2u);
|
||||
pos = 2u;
|
||||
if (code > 1u) { code |= bits(blk, 2u, 3u) << 2; pos = 5u; }
|
||||
int mode = BC6_MODE_OF_CODE[code];
|
||||
if (mode < 0) {
|
||||
for (uint t = 0u; t < 16u; t++) put(t & 3u, t >> 2, vec4(0.0, 0.0, 0.0, 1.0));
|
||||
return;
|
||||
}
|
||||
int ep[12];
|
||||
for (int i = 0; i < 12; i++) ep[i] = 0;
|
||||
uint part_id = 0u;
|
||||
for (uint e = BC6_LAYOUT_START[mode]; e < BC6_LAYOUT_START[mode + 1]; e++) {
|
||||
uint d = BC6_LAYOUT[e];
|
||||
uint target = d & 15u, n = (d >> 4) & 31u, sh = (d >> 9) & 31u;
|
||||
uint v = bits(blk, pos, n);
|
||||
pos += n;
|
||||
if ((d & (1u << 14)) != 0u) v = bitfieldReverse(v) >> (32u - n);
|
||||
if (target == 12u) part_id = v;
|
||||
else ep[target] |= int(v << sh);
|
||||
}
|
||||
bool two = mode < 10;
|
||||
int npts = two ? 4 : 2;
|
||||
int w = BC6_BITS_W[mode];
|
||||
if (sgn) { ep[0] = sext(ep[0], w); ep[4] = sext(ep[4], w); ep[8] = sext(ep[8], w); }
|
||||
if ((mode != 9 && mode != 10) || sgn) {
|
||||
for (int i = 1; i < npts; i++) {
|
||||
ep[i] = sext(ep[i], BC6_BITS_DR[mode]);
|
||||
ep[4 + i] = sext(ep[4 + i], BC6_BITS_DG[mode]);
|
||||
ep[8 + i] = sext(ep[8 + i], BC6_BITS_DB[mode]);
|
||||
}
|
||||
}
|
||||
if (mode != 9 && mode != 10) {
|
||||
for (int i = 1; i < npts; i++) {
|
||||
for (int c = 0; c < 3; c++) {
|
||||
int v = (ep[c * 4 + i] + ep[c * 4]) & ((1 << w) - 1);
|
||||
ep[c * 4 + i] = sgn ? sext(v, w) : v;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 12; i++) ep[i] = bc6_unquantize(ep[i], w, sgn);
|
||||
const int w3[8] = int[8](0, 9, 18, 27, 37, 46, 55, 64);
|
||||
const int w4[16] = int[16](0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64);
|
||||
uint part = BC7_PART2[part_id], anchor = BC7_PART2_ANCHOR[part_id] & 15u;
|
||||
for (uint t = 0u; t < 16u; t++) {
|
||||
uint subset = two ? ((part >> (2u * t)) & 3u) : 0u;
|
||||
uint nb = two ? 3u : 4u;
|
||||
if (t == 0u || (two && subset == 1u && t == anchor)) nb--;
|
||||
uint idx = bits(blk, pos, nb);
|
||||
pos += nb;
|
||||
int wt = two ? w3[idx] : w4[idx];
|
||||
int s2 = int(subset) * 2;
|
||||
uvec3 h;
|
||||
for (int c = 0; c < 3; c++) {
|
||||
int a = ep[c * 4 + s2], b = ep[c * 4 + s2 + 1];
|
||||
h[c] = bc6_finish((a * (64 - wt) + b * wt + 32) >> 6, sgn);
|
||||
}
|
||||
put(t & 3u, t >> 2, vec4(unpackHalf2x16(h.x).x, unpackHalf2x16(h.y).x, unpackHalf2x16(h.z).x, 1.0));
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ BC7 */
|
||||
|
||||
void bc7(uvec4 blk)
|
||||
{
|
||||
uint mode = 0u;
|
||||
while (mode < 8u && bits(blk, mode, 1u) == 0u) mode++;
|
||||
if (mode >= 8u) {
|
||||
for (uint t = 0u; t < 16u; t++) put(t & 3u, t >> 2, vec4(0.0));
|
||||
return;
|
||||
}
|
||||
uint pos = mode + 1u;
|
||||
uint nparts = 1u, part_id = 0u, rotation = 0u, isel = 0u;
|
||||
if (mode == 0u || mode == 1u || mode == 2u || mode == 3u || mode == 7u) {
|
||||
nparts = (mode == 0u || mode == 2u) ? 3u : 2u;
|
||||
uint pb = mode == 0u ? 4u : 6u;
|
||||
part_id = bits(blk, pos, pb);
|
||||
pos += pb;
|
||||
}
|
||||
uint neps = nparts * 2u;
|
||||
if (mode == 4u || mode == 5u) {
|
||||
rotation = bits(blk, pos, 2u);
|
||||
pos += 2u;
|
||||
if (mode == 4u) { isel = bits(blk, pos, 1u); pos += 1u; }
|
||||
}
|
||||
int cb = BC7_BITS_RGB[mode], ab = BC7_BITS_A[mode];
|
||||
uint ep[6][4];
|
||||
for (uint c = 0u; c < 3u; c++)
|
||||
for (uint e = 0u; e < neps; e++) { ep[e][c] = bits(blk, pos, uint(cb)); pos += uint(cb); }
|
||||
for (uint e = 0u; e < neps; e++) {
|
||||
if (ab > 0) { ep[e][3] = bits(blk, pos, uint(ab)); pos += uint(ab); }
|
||||
else ep[e][3] = 0u;
|
||||
}
|
||||
const uint has_pbits = 203u; /* 0b11001011 */
|
||||
bool pbits = ((has_pbits >> mode) & 1u) != 0u || mode == 1u;
|
||||
if (mode == 0u || mode == 1u || mode == 3u || mode == 6u || mode == 7u) {
|
||||
for (uint e = 0u; e < neps; e++)
|
||||
for (uint c = 0u; c < 4u; c++) ep[e][c] <<= 1;
|
||||
if (mode == 1u) {
|
||||
uint p0 = bits(blk, pos, 1u), p1 = bits(blk, pos + 1u, 1u);
|
||||
pos += 2u;
|
||||
for (uint c = 0u; c < 3u; c++) { ep[0][c] |= p0; ep[1][c] |= p0; ep[2][c] |= p1; ep[3][c] |= p1; }
|
||||
} else {
|
||||
for (uint e = 0u; e < neps; e++) {
|
||||
uint p = bits(blk, pos, 1u);
|
||||
pos += 1u;
|
||||
for (uint c = 0u; c < 4u; c++) ep[e][c] |= p;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (uint e = 0u; e < neps; e++) {
|
||||
uint j = uint(cb) + ((has_pbits >> mode) & 1u);
|
||||
for (uint c = 0u; c < 3u; c++) {
|
||||
ep[e][c] = ep[e][c] << (8u - j);
|
||||
ep[e][c] = (ep[e][c] | (ep[e][c] >> j)) & 0xffu;
|
||||
}
|
||||
j = uint(ab) + ((has_pbits >> mode) & 1u);
|
||||
ep[e][3] = ep[e][3] << (8u - j);
|
||||
ep[e][3] = (ep[e][3] | (ep[e][3] >> j)) & 0xffu;
|
||||
if (ab == 0) ep[e][3] = 255u;
|
||||
}
|
||||
const uint w2[4] = uint[4](0u, 21u, 43u, 64u);
|
||||
const uint w3[8] = uint[8](0u, 9u, 18u, 27u, 37u, 46u, 55u, 64u);
|
||||
const uint w4[16] = uint[16](0u, 4u, 9u, 13u, 17u, 21u, 26u, 30u, 34u, 38u, 43u, 47u, 51u, 55u, 60u, 64u);
|
||||
uint ib = (mode == 0u || mode == 1u) ? 3u : (mode == 6u ? 4u : 2u);
|
||||
uint ib2 = mode == 4u ? 3u : (mode == 5u ? 2u : 0u);
|
||||
uint part = nparts == 2u ? BC7_PART2[part_id] : (nparts == 3u ? BC7_PART3[part_id] : 0u);
|
||||
uint anchors = nparts == 2u ? BC7_PART2_ANCHOR[part_id] : (nparts == 3u ? BC7_PART3_ANCHOR[part_id] : 0u);
|
||||
uint idx1[16];
|
||||
for (uint t = 0u; t < 16u; t++) {
|
||||
uint subset = (part >> (2u * t)) & 3u;
|
||||
uint nb = ib;
|
||||
bool anchor = t == 0u || (subset == 1u && t == (anchors & 15u)) || (subset == 2u && t == (anchors >> 4));
|
||||
if (anchor) nb--;
|
||||
idx1[t] = bits(blk, pos, nb);
|
||||
pos += nb;
|
||||
}
|
||||
for (uint t = 0u; t < 16u; t++) {
|
||||
uint subset = (part >> (2u * t)) & 3u;
|
||||
uint e0 = subset * 2u, e1 = e0 + 1u;
|
||||
uint i1 = idx1[t];
|
||||
uvec4 c;
|
||||
if (ib2 == 0u) {
|
||||
uint wt = ib == 2u ? w2[i1] : (ib == 3u ? w3[i1] : w4[i1]);
|
||||
for (uint k = 0u; k < 4u; k++) c[k] = (ep[e0][k] * (64u - wt) + ep[e1][k] * wt + 32u) >> 6;
|
||||
} else {
|
||||
uint nb2 = t == 0u ? ib2 - 1u : ib2;
|
||||
uint i2 = bits(blk, pos, nb2);
|
||||
pos += nb2;
|
||||
uint wa = ib == 2u ? w2[i1] : w3[i1];
|
||||
uint wb = ib2 == 2u ? w2[i2] : w3[i2];
|
||||
uint wc = isel == 0u ? wa : wb, wal = isel == 0u ? wb : wa;
|
||||
for (uint k = 0u; k < 3u; k++) c[k] = (ep[e0][k] * (64u - wc) + ep[e1][k] * wc + 32u) >> 6;
|
||||
c.w = (ep[e0][3] * (64u - wal) + ep[e1][3] * wal + 32u) >> 6;
|
||||
}
|
||||
if (rotation == 1u) c = c.agbr;
|
||||
else if (rotation == 2u) c = c.rabg;
|
||||
else if (rotation == 3u) c = c.rgab;
|
||||
put(t & 3u, t >> 2, vec4(c) / 255.0);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ main */
|
||||
|
||||
void main()
|
||||
{
|
||||
uvec2 blk = gl_GlobalInvocationID.xy;
|
||||
uvec2 nblk = (pc.extent + 3u) / 4u;
|
||||
if (blk.x >= nblk.x || blk.y >= nblk.y) return;
|
||||
#if defined(FMT_BC1) || defined(FMT_BC4)
|
||||
uint words = 2u;
|
||||
#else
|
||||
uint words = 4u;
|
||||
#endif
|
||||
uint base = pc.src_word + (gl_GlobalInvocationID.z * pc.layer_blocks + blk.y * pc.row_blocks + blk.x) * words;
|
||||
uvec4 b = uvec4(src.w[base], src.w[base + 1u], words > 2u ? src.w[base + 2u] : 0u, words > 2u ? src.w[base + 3u] : 0u);
|
||||
bool sgn = (pc.flags & 2u) != 0u;
|
||||
#if defined(FMT_BC1)
|
||||
uvec4 tex[16];
|
||||
color_block(b.x, b.y, false, tex);
|
||||
for (uint t = 0u; t < 16u; t++) {
|
||||
uvec4 c = tex[t];
|
||||
if ((pc.flags & 1u) != 0u) c.w = 255u;
|
||||
put(t & 3u, t >> 2, vec4(c) / 255.0);
|
||||
}
|
||||
#elif defined(FMT_BC2) || defined(FMT_BC3)
|
||||
uvec4 tex[16];
|
||||
color_block(b.z, b.w, true, tex);
|
||||
uint a[16];
|
||||
#if defined(FMT_BC2)
|
||||
for (uint t = 0u; t < 16u; t++) a[t] = bits(b, 4u * t, 4u) * 17u;
|
||||
#else
|
||||
smooth_alpha(b.x, b.y, a);
|
||||
#endif
|
||||
for (uint t = 0u; t < 16u; t++) put(t & 3u, t >> 2, vec4(tex[t].rgb, a[t]) / 255.0);
|
||||
#elif defined(FMT_BC4)
|
||||
uint v[16];
|
||||
bc4_channel(b.x, b.y, sgn, v);
|
||||
for (uint t = 0u; t < 16u; t++) put(t & 3u, t >> 2, vec4(float(v[t]) / 255.0, 0.0, 0.0, 1.0));
|
||||
#elif defined(FMT_BC5)
|
||||
uint r[16], g[16];
|
||||
bc4_channel(b.x, b.y, sgn, r);
|
||||
bc4_channel(b.z, b.w, sgn, g);
|
||||
for (uint t = 0u; t < 16u; t++) put(t & 3u, t >> 2, vec4(float(r[t]) / 255.0, float(g[t]) / 255.0, 0.0, 1.0));
|
||||
#elif defined(FMT_BC6H)
|
||||
bc6h(b, sgn);
|
||||
#elif defined(FMT_BC7)
|
||||
bc7(b);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/* Generated by gen_bc_tables.py from bcdec.h (MIT/Unlicense, Sergii Kudlai) - do not edit. */
|
||||
const int BC6_MODE_OF_CODE[32] = int[32](0, 1, 2, 10, -1, -1, 3, 11, -1, -1, 4, 12, -1, -1, 5, 13, -1, -1, 6, -1, -1, -1, 7, -1, -1, -1, 8, -1, -1, -1, 9, -1);
|
||||
const uint BC6_LAYOUT_START[15] = uint[15](0u, 20u, 44u, 63u, 84u, 105u, 125u, 145u, 167u, 189u, 213u, 219u, 228u, 237u, 246u);
|
||||
const uint BC6_LAYOUT[246] = uint[246](2070u, 2074u, 2075u, 160u, 164u, 168u, 81u, 2071u, 70u, 85u, 27u, 71u, 89u, 539u, 74u, 82u, 1051u, 83u, 1563u, 92u, 2582u, 2071u, 2583u, 112u, 27u, 539u, 2074u, 116u, 2586u, 1051u, 2070u, 120u, 1563u, 2587u, 2075u, 97u, 70u, 101u, 71u, 105u, 74u, 98u, 99u, 92u, 160u, 164u, 168u, 81u, 5136u, 70u, 69u, 5140u, 27u, 71u, 73u, 5144u, 539u, 74u, 82u, 1051u, 83u, 1563u, 92u, 160u, 164u, 168u, 65u, 5136u, 2071u, 70u, 85u, 5140u, 71u, 73u, 5144u, 539u, 74u, 66u, 27u, 1051u, 67u, 2070u, 1563u, 92u, 160u, 164u, 168u, 65u, 5136u, 2074u, 70u, 69u, 5140u, 27u, 71u, 89u, 5144u, 74u, 66u, 539u, 1051u, 67u, 2075u, 1563u, 92u, 144u, 2074u, 148u, 2070u, 152u, 2075u, 81u, 2071u, 70u, 85u, 27u, 71u, 89u, 539u, 74u, 82u, 1051u, 83u, 1563u, 92u, 128u, 2071u, 2074u, 132u, 1051u, 2070u, 136u, 1563u, 2075u, 97u, 70u, 85u, 27u, 71u, 89u, 539u, 74u, 98u, 99u, 92u, 128u, 27u, 2074u, 132u, 2582u, 2070u, 136u, 2583u, 2075u, 81u, 2071u, 70u, 101u, 71u, 89u, 539u, 74u, 82u, 1051u, 83u, 1563u, 92u, 128u, 539u, 2074u, 132u, 2586u, 2070u, 136u, 2587u, 2075u, 81u, 2071u, 70u, 85u, 27u, 71u, 105u, 74u, 82u, 1051u, 83u, 1563u, 92u, 96u, 2071u, 27u, 539u, 2074u, 100u, 2582u, 2586u, 1051u, 2070u, 104u, 2583u, 1563u, 2587u, 2075u, 97u, 70u, 101u, 71u, 105u, 74u, 98u, 99u, 92u, 160u, 164u, 168u, 161u, 165u, 169u, 160u, 164u, 168u, 145u, 5136u, 149u, 5140u, 153u, 5144u, 160u, 164u, 168u, 129u, 21536u, 133u, 21540u, 137u, 21544u, 160u, 164u, 168u, 65u, 21600u, 69u, 21604u, 73u, 21608u);
|
||||
const int BC6_BITS_W[14] = int[14](10, 7, 11, 11, 11, 9, 8, 8, 8, 6, 10, 11, 12, 16);
|
||||
const int BC6_BITS_DR[14] = int[14](5, 6, 5, 4, 4, 5, 6, 5, 5, 6, 10, 9, 8, 4);
|
||||
const int BC6_BITS_DG[14] = int[14](5, 6, 4, 5, 4, 5, 5, 6, 5, 6, 10, 9, 8, 4);
|
||||
const int BC6_BITS_DB[14] = int[14](5, 6, 4, 4, 5, 5, 5, 5, 6, 6, 10, 9, 8, 4);
|
||||
const uint BC7_PART2[64] = uint[64](1347440720u, 1077952576u, 1414812756u, 1414549568u, 1346387968u, 1431589968u, 1431588928u, 1414545408u, 1346371584u, 1431655504u, 1431584768u, 1413480448u, 1431655488u, 1431633920u, 1431655680u, 1426063360u, 1427439872u, 16468u, 352387072u, 4214868u, 16464u, 352649472u, 83951616u, 1079005268u, 4210768u, 83951872u, 336860180u, 85202000u, 18175040u, 5592320u, 352403540u, 88162640u, 1145324612u, 1426085120u, 289673540u, 84234320u, 89130320u, 286344260u, 1091846420u, 1141969220u, 352669780u, 17125440u, 84152400u, 88428880u, 339820820u, 1342506320u, 1094784020u, 1315840u, 267524u, 1070096u, 273944576u, 68486144u, 1346438420u, 1090851920u, 88166420u, 335888720u, 1090848020u, 1095782420u, 1073812820u, 1410662720u, 1347441920u, 5591120u, 353701904u, 1414792196u);
|
||||
const uint BC7_PART2_ANCHOR[64] = uint[64](15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 15u, 2u, 8u, 2u, 2u, 8u, 8u, 15u, 2u, 8u, 2u, 2u, 8u, 8u, 2u, 2u, 15u, 15u, 6u, 8u, 2u, 8u, 15u, 15u, 2u, 8u, 2u, 2u, 2u, 15u, 15u, 6u, 6u, 2u, 6u, 8u, 15u, 15u, 2u, 2u, 15u, 15u, 15u, 15u, 15u, 2u, 2u, 15u);
|
||||
const uint BC7_PART3[64] = uint[64](2858963024u, 1784303680u, 1515864576u, 1414570152u, 2779054080u, 2694860880u, 1431675040u, 1515868240u, 2857697280u, 2857719040u, 2863289600u, 2425393296u, 2492765332u, 2762253476u, 2846200912u, 705315408u, 2777960512u, 172118100u, 2779096320u, 1436590240u, 2829603924u, 1785348160u, 2762231808u, 437912832u, 5285028u, 2862977168u, 342452500u, 1768494080u, 2693105056u, 2860651540u, 1352967248u, 1784283648u, 2846195712u, 1351655592u, 2829094992u, 606348324u, 11162880u, 613566756u, 608801316u, 1352993360u, 1342874960u, 2863285316u, 1717960704u, 2778768800u, 1352683680u, 1764256040u, 1152035396u, 1717986816u, 2856600644u, 1420317864u, 2508232064u, 2526451200u, 2824098984u, 2157286784u, 2853442580u, 2526412800u, 2863272980u, 2689618080u, 2695210400u, 2516582400u, 1082146944u, 2846402984u, 2863311428u, 709513812u);
|
||||
const uint BC7_PART3_ANCHOR[64] = uint[64](243u, 131u, 143u, 63u, 248u, 243u, 63u, 143u, 248u, 248u, 246u, 246u, 246u, 245u, 243u, 131u, 243u, 131u, 248u, 63u, 243u, 131u, 246u, 138u, 53u, 248u, 104u, 166u, 248u, 245u, 175u, 143u, 248u, 63u, 243u, 165u, 166u, 138u, 152u, 175u, 111u, 243u, 143u, 245u, 63u, 111u, 111u, 143u, 243u, 63u, 245u, 245u, 245u, 248u, 245u, 250u, 245u, 250u, 248u, 253u, 63u, 252u, 243u, 131u);
|
||||
const int BC7_BITS_RGB[8] = int[8](4, 6, 5, 7, 5, 7, 7, 5);
|
||||
const int BC7_BITS_A[8] = int[8](0, 0, 0, 0, 6, 8, 7, 5);
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
# Compiles the BCn decoder variants into app/src/main/cpp/vkbridge/bc_spv.h (committed, so a build
|
||||
# needs no shader compiler). Needs glslangValidator; regenerates bc_tables.glsl first.
|
||||
set -eu
|
||||
cd "$(dirname "$0")"
|
||||
python3 gen_bc_tables.py
|
||||
out=../../../app/src/main/cpp/vkbridge/bc_spv.h
|
||||
tmp=$(mktemp -d)
|
||||
{
|
||||
echo "/* Generated by tools/vkbridge/shaders/build.sh from bc_decode.comp - do not edit. */"
|
||||
echo "#pragma once"
|
||||
echo "#include <stdint.h>"
|
||||
} > "$out"
|
||||
for f in BC1 BC2 BC3 BC4 BC5 BC6H BC7; do
|
||||
name=spv_$(echo $f | tr A-Z a-z)
|
||||
glslangValidator -V --target-env vulkan1.1 -DFMT_$f --vn "$name" -o "$tmp/$name.h" bc_decode.comp >/dev/null
|
||||
grep -v '^ *//\|#pragma once' "$tmp/$name.h" | sed 's/^const uint32_t/static const uint32_t/' >> "$out"
|
||||
done
|
||||
rm -rf "$tmp"
|
||||
echo "wrote $out"
|
||||
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""
|
||||
Generates bc_tables.glsl from bcdec.h (third_party/bcdec, MIT/Unlicense): the BC6H per-mode bit
|
||||
layouts (as a table instead of 14 hand-written cases) and the BC6H/BC7 partition and anchor tables.
|
||||
|
||||
BC6H layout entry (uint): target (r0..r3 = 0..3, g0..g3 = 4..7, b0..b3 = 8..11, partition = 12)
|
||||
| nbits << 4 | shift << 9 | reversed << 14.
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
|
||||
here = os.path.dirname(os.path.abspath(__file__))
|
||||
src = open(os.path.join(here, '..', 'third_party', 'bcdec', 'bcdec.h')).read()
|
||||
|
||||
# ---- BC6H modes
|
||||
bc6 = src[src.rindex('BCDECDEF void bcdec_bc6h_half('):src.rindex('BCDECDEF void bcdec_bc6h_float(')]
|
||||
cases = re.findall(r'case (0b[01]+): \{(.*?)\} break;', bc6, re.S)
|
||||
comp = {'r': 0, 'g': 4, 'b': 8}
|
||||
modes = {}
|
||||
for code, body in cases:
|
||||
entries = []
|
||||
mode_idx = None
|
||||
body = re.sub(r'/\*.*?\*/', '', body, flags=re.S)
|
||||
for line in body.split('\n'):
|
||||
line = line.split('/*')[0].strip()
|
||||
if not line:
|
||||
continue
|
||||
m = re.match(r'mode = (\d+);', line)
|
||||
if m:
|
||||
mode_idx = int(m.group(1))
|
||||
continue
|
||||
m = re.match(r'partition = bcdec__bitstream_read_bits\(&bstream, (\d+)\);', line)
|
||||
if m:
|
||||
entries.append((12, int(m.group(1)), 0, 0))
|
||||
continue
|
||||
m = re.match(r'([rgb])\[(\d)\] \|= bcdec__bitstream_read_bit(s_r|s)?\(&bstream(?:, (\d+))?\)(?: << (\d+))?;', line)
|
||||
if not m:
|
||||
raise SystemExit(f'unparsed BC6H line: {line}')
|
||||
c, i, kind, n, sh = m.groups()
|
||||
nbits = int(n) if n else 1
|
||||
entries.append((comp[c] + int(i), nbits, int(sh) if sh else 0, 1 if kind == 's_r' else 0))
|
||||
modes[int(code, 2)] = (mode_idx, entries)
|
||||
|
||||
out = []
|
||||
out.append('/* Generated by gen_bc_tables.py from bcdec.h (MIT/Unlicense, Sergii Kudlai) - do not edit. */')
|
||||
code_to_mode = [-1] * 32
|
||||
offsets, flat = [0] * 14, []
|
||||
for code in sorted(modes, key=lambda c: modes[c][0]):
|
||||
idx, entries = modes[code]
|
||||
code_to_mode[code] = idx
|
||||
offsets[idx] = len(flat)
|
||||
for t, n, sh, rv in entries:
|
||||
flat.append(t | (n << 4) | (sh << 9) | (rv << 14))
|
||||
offsets.append(len(flat))
|
||||
out.append(f'const int BC6_MODE_OF_CODE[32] = int[32]({", ".join(map(str, code_to_mode))});')
|
||||
out.append(f'const uint BC6_LAYOUT_START[15] = uint[15]({", ".join(str(o) + "u" for o in offsets)});')
|
||||
out.append(f'const uint BC6_LAYOUT[{len(flat)}] = uint[{len(flat)}]({", ".join(str(v) + "u" for v in flat)});')
|
||||
|
||||
def actual_bits(fn_src, rows):
|
||||
m = re.search(r'static char actual_bits_count\[\d\]\[\d+\] = \{(.*?)\};', fn_src, re.S)
|
||||
return [list(map(int, re.findall(r'-?\d+', r.split('/*')[0]))) for r in re.findall(r'\{([^{}]*)\}', m.group(1))][:rows]
|
||||
|
||||
ab6 = actual_bits(bc6, 4)
|
||||
for k, name in enumerate(['W', 'DR', 'DG', 'DB']):
|
||||
out.append(f'const int BC6_BITS_{name}[14] = int[14]({", ".join(map(str, ab6[k]))});')
|
||||
|
||||
# ---- partition tables (BC7's 2- and 3-subset; BC6H uses the first 32 of the 2-subset table)
|
||||
bc7 = src[src.rindex('BCDECDEF void bcdec_bc7('):]
|
||||
table = bc7[bc7.index('partition_sets[2][64][4][4] = {'):bc7.index('static int aWeight2')]
|
||||
rows = re.findall(r'\{ \{([^}]*)\}, \{([^}]*)\}, \{([^}]*)\}, \{([^}]*)\} \}', table)
|
||||
assert len(rows) == 128, len(rows)
|
||||
for name, part in (('BC7_PART2', rows[:64]), ('BC7_PART3', rows[64:])):
|
||||
packed, anchors = [], []
|
||||
for r in part:
|
||||
vals = [int(v) for row in r for v in row.split(',')]
|
||||
word, anc = 0, [0, 0, 0]
|
||||
for t, v in enumerate(vals):
|
||||
word |= (v & 3) << (2 * t)
|
||||
if v & 0x80:
|
||||
anc[v & 3] = t
|
||||
packed.append(word)
|
||||
anchors.append(anc[1] | (anc[2] << 4))
|
||||
out.append(f'const uint {name}[64] = uint[64]({", ".join(str(p) + "u" for p in packed)});')
|
||||
out.append(f'const uint {name}_ANCHOR[64] = uint[64]({", ".join(str(a) + "u" for a in anchors)});')
|
||||
ab7 = actual_bits(bc7, 2)
|
||||
out.append(f'const int BC7_BITS_RGB[8] = int[8]({", ".join(map(str, ab7[0]))});')
|
||||
out.append(f'const int BC7_BITS_A[8] = int[8]({", ".join(map(str, ab7[1]))});')
|
||||
|
||||
open(os.path.join(here, 'bc_tables.glsl'), 'w').write('\n'.join(out) + '\n')
|
||||
print(f'bc_tables.glsl: {len(flat)} BC6H layout entries, {len(modes)} modes')
|
||||
Reference in new issue
Block a user