shader-slang/slang

Access Interface through pointer

Closed this issue · 5 comments

It is possible to access an Interface through a StructuredBuffer, though it is not possible to do this through a pointer.

interface ITest {};
// works
StructuredBuffer<ITest> buffer;

// does not work
[[vk::push_constant]] ITest* pc;

// also does not work 
struct MyData {
    ITest* ptr;
};
StructuredBuffer<MyData> data;

There should not really be a big difference between these usecases, so I hope this can be allowed soon.

The following two codes are not same,

// Passes "ITest" not pointer value
StructuredBuffer<ITest> buffer;

struct MyData {
   // Passes a pointer value not ITest
    ITest* ptr;
};
StructuredBuffer<MyData> data;

In order to be same, it should be either one of the followings.

StructuredBuffer<ITest*> buffer;
struct MyData {
    ITest* ptr;
};
StructuredBuffer<MyData> data;

or

StructuredBuffer<ITest> buffer;
struct MyData {
    ITest ptr;
};
StructuredBuffer<MyData> data;

The first case, however, is not supported.
The second case doesn't yield errors for me, although I am not sure if that's what you want to do.

Ok I see. I guess this example was wrong then.

My goal is to be able to access an Interface just via an address.
so something like

struct MyData {
    ITest* ptr;
};
StructuredBuffer<MyData> data;

or

[[vk::push_constant]] ITest* pc;

I think what you want to do is "Link-time type" described in the Slang User's document.
Or you can also find more details in a new coming document

The reason why I said it is that interface already does the dynamic dispatching at runtime.
I don't see why you want to have double indirection with an extra pointer.

I don't think so, the main point is really to allow to access a buffer that contains interface implementations through a pointer.

struct Mesh {
    // some data
    IMaterial* material;
};
StructuredBuffer<Mesh> meshes;

In this example this would allow to bundle the material with the mesh data

// like so
meshes[0].material.foo()

// instead of
StructuredBuffer<IMaterial> materials;
materials[meshes[0].material_idx].foo()

doing something like this crashes the compiler without any warnings or errors.

struct Input {
    Ptr<uint8_t> sensors;
}
[[vk::push_constant]] Input pc;

[shader("vertex")]
float4 vertexMain(uint vid : SV_VertexID, uint iid : SV_InstanceID) : SV_Position
{
    let sensor = Ptr<ISensor>(pc.sensors);
    return sensor.splat(float4(1.0))
}