examples: update zigimg usage

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-08-20 22:43:04 -07:00
parent dc00067ec1
commit 37c3045314
3 changed files with 22 additions and 22 deletions

View file

@ -65,12 +65,12 @@ pub fn init(app: *App, core: *mach.Core) !void {
const atlas_img_region = try app.texture_atlas_data.reserve(core.allocator, @truncate(u32, img.width), @truncate(u32, img.height)); const atlas_img_region = try app.texture_atlas_data.reserve(core.allocator, @truncate(u32, img.width), @truncate(u32, img.height));
const img_uv_data = atlas_img_region.getUVData(atlas_float_size); const img_uv_data = atlas_img_region.getUVData(atlas_float_size);
switch (img.pixels.?) { switch (img.pixels) {
.Rgba32 => |pixels| app.texture_atlas_data.set(atlas_img_region, pixels), .rgba32 => |pixels| app.texture_atlas_data.set(atlas_img_region, pixels),
.Rgb24 => |pixels| { .rgb24 => |pixels| {
const data = try rgb24ToRgba32(core.allocator, pixels); const data = try rgb24ToRgba32(core.allocator, pixels);
defer data.deinit(core.allocator); defer data.deinit(core.allocator);
app.texture_atlas_data.set(atlas_img_region, data.Rgba32); app.texture_atlas_data.set(atlas_img_region, data.rgba32);
}, },
else => @panic("unsupported image color format"), else => @panic("unsupported image color format"),
} }
@ -300,11 +300,11 @@ pub fn resize(app: *App, _: *mach.Core, _: u32, _: u32) !void {
app.update_vertex_uniform_buffer = true; app.update_vertex_uniform_buffer = true;
} }
fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []zigimg.color.Rgb24) !zigimg.color.ColorStorage { fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []zigimg.color.Rgb24) !zigimg.color.PixelStorage {
const out = try zigimg.color.ColorStorage.init(allocator, .Rgba32, in.len); const out = try zigimg.color.PixelStorage.init(allocator, .rgba32, in.len);
var i: usize = 0; var i: usize = 0;
while (i < in.len) : (i += 1) { while (i < in.len) : (i += 1) {
out.Rgba32[i] = zigimg.color.Rgba32{ .R = in[i].R, .G = in[i].G, .B = in[i].B, .A = 255 }; out.rgba32[i] = zigimg.color.Rgba32{ .r = in[i].r, .g = in[i].g, .b = in[i].b, .a = 255 };
} }
return out; return out;
} }

View file

@ -103,12 +103,12 @@ pub fn init(app: *App, core: *mach.Core) !void {
.rows_per_image = @intCast(u32, img.height), .rows_per_image = @intCast(u32, img.height),
}; };
switch (img.pixels.?) { switch (img.pixels) {
.Rgba32 => |pixels| queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, pixels), .rgba32 => |pixels| queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, pixels),
.Rgb24 => |pixels| { .rgb24 => |pixels| {
const data = try rgb24ToRgba32(core.allocator, pixels); const data = try rgb24ToRgba32(core.allocator, pixels);
defer data.deinit(core.allocator); defer data.deinit(core.allocator);
queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, data.Rgba32); queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, data.rgba32);
}, },
else => @panic("unsupported image color format"), else => @panic("unsupported image color format"),
} }
@ -261,11 +261,11 @@ pub fn update(app: *App, core: *mach.Core) !void {
back_buffer_view.release(); back_buffer_view.release();
} }
fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []zigimg.color.Rgb24) !zigimg.color.ColorStorage { fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []zigimg.color.Rgb24) !zigimg.color.PixelStorage {
const out = try zigimg.color.ColorStorage.init(allocator, .Rgba32, in.len); const out = try zigimg.color.PixelStorage.init(allocator, .rgba32, in.len);
var i: usize = 0; var i: usize = 0;
while (i < in.len) : (i += 1) { while (i < in.len) : (i += 1) {
out.Rgba32[i] = zigimg.color.Rgba32{ .R = in[i].R, .G = in[i].G, .B = in[i].B, .A = 255 }; out.rgba32[i] = zigimg.color.Rgba32{ .r = in[i].r, .g = in[i].g, .b = in[i].b, .a = 255 };
} }
return out; return out;
} }

View file

@ -105,7 +105,7 @@ pub fn init(app: *App, core: *mach.Core) !void {
.min_filter = .linear, .min_filter = .linear,
}); });
const queue = core.device.getQueue(); const queue = core.device.getQueue();
const img = try zigimg.Image.fromMemory(core.allocator, @embedFile("../assets/gotta-go-fast.png")); var img = try zigimg.Image.fromMemory(core.allocator, @embedFile("assets/gotta-go-fast.png"));
defer img.deinit(); defer img.deinit();
const img_size = gpu.Extent3D{ .width = @intCast(u32, img.width), .height = @intCast(u32, img.height) }; const img_size = gpu.Extent3D{ .width = @intCast(u32, img.width), .height = @intCast(u32, img.height) };
const cube_texture = core.device.createTexture(&.{ const cube_texture = core.device.createTexture(&.{
@ -121,12 +121,12 @@ pub fn init(app: *App, core: *mach.Core) !void {
.bytes_per_row = @intCast(u32, img.width * 4), .bytes_per_row = @intCast(u32, img.width * 4),
.rows_per_image = @intCast(u32, img.height), .rows_per_image = @intCast(u32, img.height),
}; };
switch (img.pixels.?) { switch (img.pixels) {
.Rgba32 => |pixels| queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, pixels), .rgba32 => |pixels| queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, pixels),
.Rgb24 => |pixels| { .rgb24 => |pixels| {
const data = try rgb24ToRgba32(core.allocator, pixels); const data = try rgb24ToRgba32(core.allocator, pixels);
defer data.deinit(core.allocator); defer data.deinit(core.allocator);
queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, data.Rgba32); queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, data.rgba32);
}, },
else => @panic("unsupported image color format"), else => @panic("unsupported image color format"),
} }
@ -262,11 +262,11 @@ pub fn resize(app: *App, core: *mach.Core, width: u32, height: u32) !void {
}); });
} }
fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []zigimg.color.Rgb24) !zigimg.color.ColorStorage { fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []zigimg.color.Rgb24) !zigimg.color.PixelStorage {
const out = try zigimg.color.ColorStorage.init(allocator, .Rgba32, in.len); const out = try zigimg.color.PixelStorage.init(allocator, .rgba32, in.len);
var i: usize = 0; var i: usize = 0;
while (i < in.len) : (i += 1) { while (i < in.len) : (i += 1) {
out.Rgba32[i] = zigimg.color.Rgba32{ .R = in[i].R, .G = in[i].G, .B = in[i].B, .A = 255 }; out.rgba32[i] = zigimg.color.Rgba32{ .r = in[i].r, .g = in[i].g, .b = in[i].b, .a = 255 };
} }
return out; return out;
} }