renderer-mvp/asset-pipeline #53
|
@ -2,12 +2,12 @@ const App = @import("./App.zig");
|
|||
|
||||
const coral = @import("coral");
|
||||
|
||||
const formats = @import("./gfx/formats.zig");
|
||||
|
||||
const device = @import("./gfx/device.zig");
|
||||
|
||||
const ext = @import("./ext.zig");
|
||||
|
||||
const formats = @import("./gfx/formats.zig");
|
||||
|
||||
const msg = @import("./msg.zig");
|
||||
|
||||
const std = @import("std");
|
||||
|
@ -19,10 +19,10 @@ pub const Assets = struct {
|
|||
|
||||
pub const Format = struct {
|
||||
extension: []const u8,
|
||||
open_file: *const fn (*std.heap.ArenaAllocator, []const u8) Error!Desc,
|
||||
file_desc: *const fn (*std.heap.ArenaAllocator, []const u8) Error!Desc,
|
||||
|
||||
pub const Error = std.mem.Allocator.Error || coral.files.Error || error {
|
||||
Unsupported,
|
||||
FormatUnsupported,
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -40,7 +40,7 @@ pub const Assets = struct {
|
|||
continue;
|
||||
}
|
||||
|
||||
return self.context.open(try format.open_file(&self.staging_arena, path));
|
||||
return self.context.open(try format.file_desc(&self.staging_arena, path));
|
||||
}
|
||||
|
||||
return .none;
|
||||
|
@ -91,16 +91,12 @@ pub const Desc = union (enum) {
|
|||
};
|
||||
|
||||
pub const Format = enum {
|
||||
rgba8888,
|
||||
bgra8888,
|
||||
argb8888,
|
||||
rgb888,
|
||||
bgr888,
|
||||
rgba8,
|
||||
bgra8,
|
||||
|
||||
pub fn byte_size(self: Format) usize {
|
||||
return switch (self) {
|
||||
.rgba8888, .bgra8888, .argb8888 => 4,
|
||||
.rgb888, .bgr888 => 3,
|
||||
.rgba8, .bgra8 => 4,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
@ -197,7 +193,7 @@ pub const Transform2D = extern struct {
|
|||
const builtin_formats = [_]Assets.Format{
|
||||
.{
|
||||
.extension = "bmp",
|
||||
.open_file = formats.load_bmp,
|
||||
.file_desc = formats.bmp_file_desc,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -253,6 +253,11 @@ const Loop = struct {
|
|||
break: get subimage;
|
||||
},
|
||||
},
|
||||
|
||||
.pixel_format = switch (texture.format) {
|
||||
.rgba8 => .RGBA8,
|
||||
.bgra8 => .BGRA8,
|
||||
},
|
||||
});
|
||||
|
||||
errdefer sokol.gfx.destroyImage(image);
|
||||
|
|
|
@ -4,7 +4,7 @@ const gfx = @import("../gfx.zig");
|
|||
|
||||
const std = @import("std");
|
||||
|
||||
pub fn load_bmp(arena: *std.heap.ArenaAllocator, path: []const u8) gfx.Assets.Format.Error!gfx.Desc {
|
||||
pub fn bmp_file_desc(arena: *std.heap.ArenaAllocator, path: []const u8) gfx.Assets.Format.Error!gfx.Desc {
|
||||
const header = try coral.files.bundle.read_little(path, 0, extern struct {
|
||||
type: [2]u8 align (1),
|
||||
file_size: u32 align (1),
|
||||
|
@ -22,15 +22,15 @@ pub fn load_bmp(arena: *std.heap.ArenaAllocator, path: []const u8) gfx.Assets.Fo
|
|||
palette_colors_used: u32 align (1),
|
||||
important_colors_used: u32 align (1),
|
||||
}) orelse {
|
||||
return error.Unsupported;
|
||||
return error.FormatUnsupported;
|
||||
};
|
||||
|
||||
if (!std.mem.eql(u8, &header.type, "BM")) {
|
||||
return error.Unsupported;
|
||||
return error.FormatUnsupported;
|
||||
}
|
||||
|
||||
const pixel_width = std.math.cast(u16, header.pixel_width) orelse {
|
||||
return error.Unsupported;
|
||||
return error.FormatUnsupported;
|
||||
};
|
||||
|
||||
const pixels = try arena.allocator().alloc(coral.io.Byte, header.image_size);
|
||||
|
@ -42,27 +42,35 @@ pub fn load_bmp(arena: *std.heap.ArenaAllocator, path: []const u8) gfx.Assets.Fo
|
|||
var buffer_offset: usize = 0;
|
||||
var file_offset = @as(usize, header.image_offset);
|
||||
|
||||
while (buffer_offset < pixels.len) {
|
||||
const line = pixels[buffer_offset .. buffer_offset + byte_stride];
|
||||
switch (header.bits_per_pixel) {
|
||||
32 => {
|
||||
while (buffer_offset < pixels.len) {
|
||||
const line = pixels[buffer_offset .. buffer_offset + byte_stride];
|
||||
|
||||
if (try coral.files.bundle.read_bytes(path, file_offset, line) != byte_stride) {
|
||||
return error.Unsupported;
|
||||
}
|
||||
if (try coral.files.bundle.read_bytes(path, file_offset, line) != byte_stride) {
|
||||
return error.FormatUnsupported;
|
||||
}
|
||||
|
||||
file_offset = line.len + byte_padding;
|
||||
buffer_offset += padded_byte_stride;
|
||||
for (0 .. pixel_width) |i| {
|
||||
const line_offset = i * 4;
|
||||
const pixel = line[line_offset .. line_offset + 4];
|
||||
|
||||
std.mem.swap(u8, &pixel[0], &pixel[2]);
|
||||
}
|
||||
|
||||
file_offset += line.len + byte_padding;
|
||||
buffer_offset += padded_byte_stride;
|
||||
}
|
||||
},
|
||||
|
||||
else => return error.FormatUnsupported,
|
||||
}
|
||||
|
||||
return .{
|
||||
.texture = .{
|
||||
.format = switch (header.bits_per_pixel) {
|
||||
24 => .bgr888,
|
||||
32 => .bgra8888,
|
||||
else => return error.Unsupported,
|
||||
},
|
||||
|
||||
.width = pixel_width,
|
||||
.data = pixels,
|
||||
.format = .rgba8,
|
||||
.access = .static,
|
||||
}
|
||||
};
|
||||
|
|
|
@ -51,6 +51,10 @@ out vec4 texel;
|
|||
|
||||
void main() {
|
||||
texel = texture(sampler2D(tex, smp), uv) * color;
|
||||
|
||||
if (texel.a == 0) {
|
||||
discard;
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
|
|
Loading…
Reference in New Issue