gpu

source of slots

1// The indices a device's objects take in its tables: a released one2// first, so a table stays as long as the most objects alive at once.34import std::vector;5import std::option;6import std::string;78// The kinds of objects a device makes, each in a table of its own; the9// browser's side (webgpu.web.js) numbers them in this order.10pub type ObjectKind = enum {11    Buffer,12    Texture,13    Sampler,14    Shader,15    RenderPipeline,16    ComputePipeline,17    BindGroup,18    Surface,19};2021const KINDS: u64 = 8;2223fn name(self kind: &ObjectKind) -> &[u8] => match (kind) {24    ::Buffer &"buffer";25    ::Texture &"texture";26    ::Sampler &"sampler";27    ::Shader &"shader";28    ::RenderPipeline &"render pipeline";29    ::ComputePipeline &"compute pipeline";30    ::BindGroup &"bind group";31    ::Surface &"surface";32};3334fn lists() -> [Vector<u32> : KINDS] {35    const empty = Vector<u32>::empty();36    return [copy empty : KINDS];37}3839// The indices released, per kind of object, for the next objects of the40// kind to take.41pub type Released = struct {42    lists: [Vector<u32> : KINDS] = lists(),43};4445// Puts the item into the table of its kind at a released index, else at46// the end; its index.47pub fn place<T>(self r: &var Released, kind: ObjectKind, items: &var Vector<T>, item: T) -> u32 {48    if (r.lists[kind to u64].pop() is ::Some |k|) {49        items.set(k to u64, move item);50        return k;51    }52    items.push(move item);53    return items.length() to u32 - 1;54}5556pub fn push(self r: &var Released, kind: ObjectKind, index: u32) {57    r.lists[kind to u64].push(index);58}5960// Each index's generation, per kind of object: a handle carries the one61// it was made at, which releasing its object moves on.62pub type Generations = struct {63    kinds: [Vector<u32> : KINDS] = lists(),64};6566// The generation a new handle of the index takes.67pub fn stamp(self g: &var Generations, kind: ObjectKind, index: u32) -> u32 {68    var list = &var g.kinds[kind to u64];69    while (list.length() <= index to u64) {70        list.push(0);71    }72    return list.get(index to u64);73}7475// Faults when the handle's object was released.76pub fn check(self g: &Generations, kind: ObjectKind, index: u32, generation: u32) {77    const list = &g.kinds[kind to u64];78    if (index to u64 >= list.length() || list.get(index to u64) != generation) {79        const message = f"pkg::alloy::gpu: a {kind.name()} used after its release";80        panic message.view();81    }82}8384// Moves the index's generation on, its handles no longer valid.85pub fn retire(self g: &var Generations, kind: ObjectKind, index: u32) {86    g.stamp(kind, index);87    g.kinds[kind to u64].get_var(index to u64) += 1;88}8990// A rectangle of a texture's pixels.91pub type Region = struct {92    pub x: u32,93    pub y: u32,94    pub width: u32,95    pub height: u32,96};9798// Where a write waiting goes.99pub type Destination = enum {100    Buffer: BufferWrite,101    Texture: TextureWrite,102};103104pub type BufferWrite = struct {105    pub index: u32,106    pub offset: u64,107};108109pub type TextureWrite = struct {110    pub index: u32,111    pub region: Region,112};113114// A write waiting for the next submission: `size` bytes at `at` in the115// staged bytes.116pub type Upload = struct {117    pub at: u64,118    pub size: u64,119    pub destination: Destination,120};121122// The writes waiting, their bytes in one run the device copies into a123// staging buffer before the next submission's commands.124pub type Staged = struct {125    pub bytes: Vector<u8> = Vector<u8>::empty(),126    pub uploads: Vector<Upload> = Vector<Upload>::empty(),127};128129pub fn write_buffer(self s: &var Staged, index: u32, offset: u64, bytes: &[u8]) {130    s.uploads.push(Upload {131        .at = s.bytes.length(),132        .size = bytes.length(),133        .destination = ::Buffer(BufferWrite { .index = index, .offset = offset })134    });135    s.bytes.append(&bytes);136}137138// The region's `size` bytes of pixels, row by row.139pub fn write_texture(self s: &var Staged, index: u32, region: Region, size: u64, bytes: &[u8]) {140    // a copy into an image starts at a multiple of 4141    while (s.bytes.length() % 4 != 0) {142        s.bytes.push(0);143    }144    s.uploads.push(Upload {145        .at = s.bytes.length(),146        .size = size,147        .destination = ::Texture(TextureWrite { .index = index, .region = region })148    });149    s.bytes.append(&bytes[..size]);150}151152// Whether the write goes into that buffer or texture.153pub fn writes(self u: &Upload, kind: ObjectKind, index: u32) -> bool => match (u.destination) {154    ::Buffer |b| kind is ::Buffer && b.index == index;155    ::Texture |t| kind is ::Texture && t.index == index;156};157158// Drops the writes into a buffer or texture released.159pub fn forget(self s: &var Staged, kind: ObjectKind, index: u32) {160    var k: u64 = 0;161    while (k < s.uploads.length()) {162        if (s.uploads.get(k).writes(kind, index)) {163            s.uploads.remove(k);164        } else {165            k += 1;166        }167    }168}169170pub fn clear(self s: &var Staged) {171    s.bytes.clear();172    s.uploads.clear();173}