Files
rosetta/tools/fixtures/_tex_encode.luau
T
4DBug ef2cd93c88 ws-tex: image/ktx (KTX1 + KTX2 read: BCn / ETC2 / EAC / uncompressed, mips, cube, arrays, 3D, Zstd / ZLIB; Basis UNSUPPORTED; KTX2 write)
KTX2 writer: rgba8 (UNORM / SRGB), rgba16, rgba16f / rgba32f with a full DFD, mips, cube / array frames, ZLIB.
Tested against ktx create / deflate / extract, toktx, nvcompress -ktx and hand-written KTX1 (big-endian, packed,
row padding, T=u); our files pass ktx validate.
2026-10-31 14:30:30 -05:00

96 lines
3.7 KiB
Luau

-- tools/fixtures/_tex_encode.luau: our texture encoders' output for the reference tools to read (run by
-- tools/fixtures/{dds,ktx,exr}.py as `luau tools/fixtures/_tex_encode.luau -a <format>`). Prints "name\thex" lines.
-- The source images are generated here (tools/fixtures/_tex_reference.py makes the same ones: source()).
local Types = require("@formats/Types")
local which = ({ ... } :: { string })[1] or "dds"
local function hex(b: buffer): string
local t = table.create(buffer.len(b))
for i = 0, buffer.len(b) - 1 do
t[i + 1] = string.format("%02x", buffer.readu8(b, i))
end
return table.concat(t)
end
-- the test pattern (x, y, channel) -> 0..1, the same formula as _tex_reference.source()
local function value(x: number, y: number, c: number, w: number, h: number, salt: number): number
if c == 0 then
return x / math.max(1, w - 1)
elseif c == 1 then
return y / math.max(1, h - 1)
elseif c == 2 then
return ((x * 7 + y * 13 + salt * 31) % 64) / 63
end
return ((x + y + salt) % 5) / 4
end
local function image(w: number, h: number, fmt: string, salt: number, scale: number?): Types.Image
local img = Types.Image.new(w, h, fmt :: any)
for y = 0, h - 1 do
for x = 0, w - 1 do
for c = 0, 3 do
local v = value(x, y, c, w, h, salt)
local o = (y * w + x) * 4 + c
if fmt == "rgba8" then
buffer.writeu8(img.pixels, o, math.floor(v * 255 + 0.5))
elseif fmt == "rgba16" then
buffer.writeu16(img.pixels, o * 2, math.floor(v * 65535 + 0.5))
else
buffer.writef32(img.pixels, o * 4, if c < 3 then v * (scale or 1) else v)
end
end
end
end
return img
end
local function emit(name: string, b: buffer?, err: any?)
if not b then
error(name .. ": " .. tostring(err))
end
print(name .. "\t" .. hex(b))
end
if which == "dds" then
local Dds = require("@formats/image/dds")
local img = image(37, 23, "rgba8", 0)
for _, f in { "rgba8", "bgra8", "bc1", "bc3", "bc4", "bc5" } do
emit("ours_" .. f .. ".dds", Dds.encode(img, { format = f :: any }))
emit("ours_" .. f .. "_mips.dds", Dds.encode(img, { format = f :: any, mips = true }))
end
emit("ours_rgba8_dx10.dds", Dds.encode(img, { dx10 = true, mips = true }))
emit("ours_rgba16.dds", Dds.encode(image(37, 23, "rgba16", 0), { mips = true }))
emit("ours_rgba32f.dds", Dds.encode(image(37, 23, "rgbaf32", 0, 4), { mips = true }))
emit("ours_rgba16f.dds", Dds.encode(image(37, 23, "rgbaf32", 0, 4), { format = "rgba16f" }))
local cube = image(16, 16, "rgba8", 0)
cube.frames = {}
for i = 1, 6 do
cube.frames[i] = { image = image(16, 16, "rgba8", i), delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }
end
cube.meta.layout = "cube"
emit("ours_cube.dds", Dds.encode(cube, { mips = true }))
cube.meta.layout = "array"
emit("ours_array_bc1.dds", Dds.encode(cube, { format = "bc1" }))
end
if which == "ktx" then
local Ktx = require("@formats/image/ktx")
local img = image(37, 23, "rgba8", 0)
emit("ours_rgba8.ktx2", Ktx.encode(img))
emit("ours_rgba8_mips.ktx2", Ktx.encode(img, { mips = true }))
emit("ours_srgb_zlib.ktx2", Ktx.encode(img, { mips = true, srgb = true, zlib = 6 }))
emit("ours_rgba16.ktx2", Ktx.encode(image(37, 23, "rgba16", 0), { mips = true }))
emit("ours_rgba32f.ktx2", Ktx.encode(image(37, 23, "rgbaf32", 0, 4), { mips = true }))
emit("ours_rgba16f.ktx2", Ktx.encode(image(37, 23, "rgbaf32", 0, 4), { format = "rgba16f" }))
local cube = image(16, 16, "rgba8", 0)
cube.frames = {}
for i = 1, 6 do
cube.frames[i] = { image = image(16, 16, "rgba8", i), delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }
end
cube.meta.layout = "cube"
emit("ours_cube.ktx2", Ktx.encode(cube, { mips = true }))
cube.meta.layout = "array"
emit("ours_array.ktx2", Ktx.encode(cube))
end