shame icon indicating copy to clipboard operation
shame copied to clipboard

Consider dynamic type support

Open chronicl opened this issue 6 months ago • 0 comments

Problem

Dynamic types don't have a statically known shame type layout. For example you might not know which vertex attributes your meshes are going to have, however the current API requires a static type to be able to access your vertex attributes:

#[derive(GpuLayout)]
struct Vertex {
    position: f32x3,
    normal: f32x3,
    uv: f32x1,
}

let vertex = drawcall.vertices.buffers.next::<Vertex>().index(...);

while you may not know nor require that your vertices have a normal or uv.

Possible solution

I suggest to create a separate dynamic api, build on new traits like DynamicGpuLayout which would allow derive macros for types with dynamic shame type layout like

#[derive(DynamicGpuLayout)]
struct Vertex {
    position: f32x3, // non-conditional
    normal: Option<f32x3>, // conditional
    uv: Option<f32x1>, // conditional
}

which produces

struct Vertex {
    position: f32x3,
    normal: Option<f32x3>,
    uv: Option<f32x1>,
}

struct VertexConditionals {
    normal: bool,
    uv: bool,
}

impl DynamicGpuLayout for Vertex {
    type Conditionals: VertexConditionals;
  
    fn gpu_layout(conditionals: Self::Conditionals) -> TypeLayout {
        // Construct TypeLayout of Vertex with it's non-conditional fields and 
        // the conditional fields specified in conditionals.
    }
  ...
}

impl DynamicFromAnys for Vertex {
    type Conditionals: VertexConditionals;

    fn from_anys(anys: ..., conditionals: Self::Conditionals) -> Self { ... }
}
...

and then we'd be able to do this

let conditionals = VertexConditionals { normal: true, uv: false };
let vertex: Vertex = drawcall.vertices.buffers.next_dynamic(conditionals).index(...);

if let Some(normal) = vertex.normal {
    // do conditional calculations
}

chronicl avatar May 04 '25 12:05 chronicl