// blit_planar.slang — blit (and scale) planar multi-dtype image planes into
// the compose storage image.
//
// Donor: oacpp/source/cpp/lib/oa/ui/shader/compute/blitPlanar.slang
// Adaptation: exact donor shader body; resource declarations use the Rust
// Strict heap layout and the fixed RGBA8 compose ABI.
//
// Each plane is a flat row-major array of W*H elements in its own dtype:
// 0 = U8 uint8 [0,255] → normalized [0.0, 1.0]
// 1 = U16 uint16 [0,65535] → normalized [0.0, 1.0]
// 2 = F32 float32 passthrough
// 3 = BF16 bfloat16 decoded to float32, passthrough
//
// Channel layout: 1=gray (replicated to RGB), 2=gray+alpha, 3=RGB,
// 4=RGBA or RGB+depth. Missing alpha defaults to 1.0.
// RGB channels are kept as-is (sRGB-encoded). Alpha/depth is linear.
//
// dtypes: bits[1:0]=R, bits[3:2]=G, bits[5:4]=B, bits[7:6]=A dtype.
//
// If dst_w != src_w or dst_h != src_h the source is nearest-neighbour scaled.
//
// Dispatch: (ceil(visible_w / 8), ceil(visible_h / 8), 1) — the host dispatches
// only the clip-visible region and passes its origin as (base_x, base_y).
import storage;
static const uint kInvalidSlot = 0xFFFFFFFF;
struct PushConstants {
uint r_idx; // bindless buffer index — R (or gray) plane
uint g_idx; // bindless buffer index — G plane (kInvalidSlot if channels < 3)
uint b_idx; // bindless buffer index — B plane (kInvalidSlot if channels < 3)
uint a_idx; // bindless buffer index — A/depth (kInvalidSlot if channels < 4)
uint dst_idx; // bindless image index — compose storage image
uint src_w;
uint src_h;
int dst_x; // X offset in compose image (pixels)
int dst_y; // Y offset in compose image (pixels)
uint dtypes; // packed per-channel dtype (2 bits each, LSB = R)
uint channels; // number of planes: 1, 2, 3, or 4
uint dst_w; // destination rect width (pixels)
uint dst_h; // destination rect height (pixels)
int clip_x;
int clip_y;
uint clip_w;
uint clip_h;
uint base_x; // dispatch origin within the destination rect
uint base_y;
};
[[vk::push_constant]] PushConstants pc;
[[vk::binding(0, 0)]] RWByteAddressBuffer heap[];
// Compose storage has a fixed RGBA8_UNORM ABI; no formatless image access.
[[vk::image_format("rgba8")]]
[[vk::binding(1, 0)]] RWTexture2D<float4> images[];
uint dtype_of(uint channel) {
return (pc.dtypes >> (channel * 2)) & 0x3;
}
// Read one element from a typed plane buffer. Returns the raw [0,1] or float value.
float load_plane(uint buf_idx, uint pixel_idx, uint dtype) {
if (dtype == 0) {
// U8: pack ratio 4:1, need byte extraction
uint byte_off = pixel_idx;
uint word = heap[buf_idx].Load(byte_off & ~3u);
uint byte_val = (word >> ((byte_off & 3u) * 8)) & 0xFF;
return float(byte_val) / 255.0f;
}
if (dtype == 1) {
// U16: pack ratio 2:1, need halfword extraction
uint byte_off = pixel_idx * 2;
uint word = heap[buf_idx].Load(byte_off & ~3u);
uint shift = (byte_off & 2u) * 8;
uint short_val = (word >> shift) & 0xFFFF;
return float(short_val) / 65535.0f;
}
if (dtype == 2) {
// F32: direct
return asfloat(heap[buf_idx].Load(pixel_idx * 4));
}
// BF16: stored as little-endian uint16, shift into upper bits of uint32
uint byte_off = pixel_idx * 2;
uint word = heap[buf_idx].Load(byte_off & ~3u);
uint shift = (byte_off & 2u) * 8;
uint bits = (word >> shift) & 0xFFFF;
return asfloat(bits << 16);
}
[shader("compute")]
[numthreads(8, 8, 1)]
void main(uint3 gid : SV_GroupID, uint3 lid : SV_GroupThreadID) {
uint x = pc.base_x + gid.x * 8 + lid.x;
uint y = pc.base_y + gid.y * 8 + lid.y;
if (x >= pc.dst_w || y >= pc.dst_h || pc.src_w == 0 || pc.src_h == 0) return;
int dstX = pc.dst_x + int(x);
int dstY = pc.dst_y + int(y);
if (dstX < pc.clip_x || dstY < pc.clip_y
|| uint(dstX - pc.clip_x) >= pc.clip_w
|| uint(dstY - pc.clip_y) >= pc.clip_h) return;
uint imageWidth;
uint imageHeight;
images[pc.dst_idx].GetDimensions(imageWidth, imageHeight);
if (dstX < 0 || dstY < 0
|| dstX >= int(imageWidth) || dstY >= int(imageHeight)) return;
// Map output pixel to source pixel (nearest-neighbour).
uint sx = min(x * pc.src_w / pc.dst_w, pc.src_w - 1);
uint sy = min(y * pc.src_h / pc.dst_h, pc.src_h - 1);
uint pixel = sy * pc.src_w + sx;
float r;
float g;
float b;
float a;
if (pc.channels <= 2) {
// Grayscale or grayscale + alpha — replicate luminance to RGB.
r = load_plane(pc.r_idx, pixel, dtype_of(0));
g = r;
b = r;
a = pc.channels == 2 && pc.g_idx != kInvalidSlot
? load_plane(pc.g_idx, pixel, dtype_of(1))
: 1.0f;
} else {
r = load_plane(pc.r_idx, pixel, dtype_of(0));
g = load_plane(pc.g_idx, pixel, dtype_of(1));
b = load_plane(pc.b_idx, pixel, dtype_of(2));
if (pc.channels >= 4 && pc.a_idx != kInvalidSlot) {
// Alpha / depth: linear, no sRGB decode
a = load_plane(pc.a_idx, pixel, dtype_of(3));
} else {
a = 1.0f;
}
}
images[pc.dst_idx][uint2(uint(dstX), uint(dstY))] = float4(r, g, b, a);
}