gpu: taked slice child type in writeTexture

This commit is contained in:
Ali Chraghi 2022-05-23 11:43:11 +04:30 committed by Stephen Gutekanst
parent 6922435110
commit b5d10fb167
4 changed files with 10 additions and 7 deletions

View file

@ -518,7 +518,7 @@ const Brick = struct {
fn texture(device: gpu.Device) Texture {
const slice: []const u8 = &data();
return Texture.fromData(device, W, H, slice);
return Texture.fromData(device, W, H, u8, slice);
}
fn data() [W * H * 4]u8 {
@ -589,7 +589,7 @@ const Texture = struct {
self.sampler.release();
}
fn fromData(device: gpu.Device, width: u32, height: u32, data: anytype) Self {
fn fromData(device: gpu.Device, width: u32, height: u32, comptime T: type, data: []const T) Self {
const extent = gpu.Extent3D{
.width = width,
.height = height,
@ -631,12 +631,13 @@ const Texture = struct {
&gpu.ImageCopyTexture{
.texture = texture,
},
data,
&gpu.Texture.DataLayout{
.bytes_per_row = 4 * width,
.rows_per_image = height,
},
&extent,
T,
data,
);
const bind_group_layout = Self.bindGroupLayout(device);

View file

@ -78,9 +78,10 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
queue.writeTexture(
&.{ .texture = texture },
texture_atlas_data.data,
&data_layout,
&.{ .width = texture_atlas_data.size, .height = texture_atlas_data.size },
zigimg.color.Rgba32,
texture_atlas_data.data,
);
app.vertices = try std.ArrayList(draw.Vertex).initCapacity(engine.allocator, 9);

View file

@ -129,11 +129,11 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
.rows_per_image = @intCast(u32, img.height),
};
switch (img.pixels.?) {
.Rgba32 => |pixels| queue.writeTexture(&.{ .texture = cube_texture }, pixels, &data_layout, &img_size),
.Rgba32 => |pixels| queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, zigimg.color.Rgba32, pixels),
.Rgb24 => |pixels| {
const data = try rgb24ToRgba32(engine.allocator, pixels);
//defer data.deinit(allocator);
queue.writeTexture(&.{ .texture = cube_texture }, data.Rgba32, &data_layout, &img_size);
queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, zigimg.color.Rgba32, data.Rgba32);
},
else => @panic("unsupported image color format"),
}