New shared codecs: codecs/image/Texel (uncompressed GPU texel formats incl. packed, shared exponent, YUY2), codecs/image/Texture (dispatch over Bcn / Etc / Texel, surface assembly, mip halving). Tested against nvcompress / nvassemble / ImageMagick files (Pillow per surface, Godot for BC6H floats), hand-written formats, and our files read back by Pillow + nvddsinfo.
76 lines
2.8 KiB
Luau
76 lines
2.8 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
|