#Help casting data to []anyopaque

1 messages · Page 1 of 1 (latest)

glass talon
#

I've got some vertex data I want to upload to the GPU(using SDL3 gpu api).

    const vertices: []const Vertex = &[_]Vertex{
        .{ .pos = .{ -1, -1 }, .color = .{ 1, 0, 0, 1 } },
        .{ .pos = .{ 0, 1 }, .color = .{ 0, 1, 0, 1 } },
        .{ .pos = .{ 1, -1 }, .color = .{ 0, 0, 1, 1 } },
    };
    try vert_buf.upload(queue, @ptrCast(vertices));

Here's the upload function

pub fn upload(self: Self, queue: *TransferQueue, data: []anyopaque) !void {
    try queue.stage(.{
        .data = data,
        .location = .{ .buf = self.ptr },
    });
}

And here's the item struct that is being passed to queue.stage()

const Item = struct {
    data: []anyopaque,
    location: union(enum) {
        buf: *sdl.SDL_GPUBuffer,
    },
};

But I get this TODO msg when I @ptrCast the slices

src/root.zig:196:32: error: TODO: implement @ptrCast between slices changing the length
    try vert_buf.upload(queue, @ptrCast(vertices));

I tried std.mem.sliceAsBytes, but I get an error about type conversion

src/root.zig:196:52: error: expected type '[]anyopaque', found '[]align(4) const u8'
    try vert_buf.upload(queue, std.mem.sliceAsBytes(vertices));

Any help with how to cast arbitrary slice data to []anyopaque?
Is there different way I should queue arbitrary data for GPU upload?

slender walrus
#

[]anyopaque isnt a type that can exist

#

if your goal is a slice of "opaque" data it seems like just u8 is what you want

#

or possibly the more manual *anyopaque + length

glass talon
#

Thanks, I'll give that a try.

Why can't []anyopaque exist? Isn't a slice just

struct {
  ptr,
  len: usize,
};

So, my understanding was []anyopaque is a slice where the pointer doesn't know what it's pointing to, but it does know how big it is

slender walrus
#

it would have to know what it is to know how big it is

#

imagine you tried to index into a slice like that, what would the offset be in bytes?

glass talon
#

Ah, I suppose that makes sense

left kiln
#

I'd use []u8 and std.mem.sliceAsBytes

glass talon
#
src/root.zig:196:52: error: expected type '[]u8', found '[]align(4) const u8'
    try vert_buf.upload(queue, std.mem.sliceAsBytes(vertices));
                               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
src/root.zig:196:52: note: cast discards const qualifier
src/sequoia/render/vertex_buffer.zig:62:56: note: parameter type declared here
pub fn upload(self: Self, queue: *TransferQueue, data: []u8) !void {
#

I'm not sure if this is complaining about the const or about the align

slender walrus
#

does the data need to be mutable?

slender walrus
glass talon
#

I'd like this to be mutable so I can dynamically build a list of vertices then upload them each frame. But right now, I'm just trying to upload static data to get started(see original question for the vertices data)

#

I can't just change this to var though because I get

error: local variable is never mutated
    var vertices = &[_]Vertex{
slender walrus
#

because thats still a *const, the pointer is to the array literal not the variable

#

remove the & there and put it in the call to sliceAsBytes

#

im a little confused how youd use this to build a list of vertices, do you mean before uploading it? if so it doesnt need to be mutable still

glass talon