{examples,shaderexp}: rename mach.Engine -> mach.Core
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
e79c9e075a
commit
1b97c9f1e5
10 changed files with 263 additions and 263 deletions
|
|
@ -31,21 +31,21 @@ update_frag_uniform_buffer: bool,
|
|||
bind_group: gpu.BindGroup,
|
||||
texture_atlas_data: AtlasRGB8,
|
||||
|
||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||
try engine.setOptions(.{
|
||||
pub fn init(app: *App, core: *mach.Core) !void {
|
||||
try core.setOptions(.{
|
||||
.width = 640,
|
||||
.height = 480,
|
||||
.size_min = .{ .width = 20, .height = 20 },
|
||||
});
|
||||
|
||||
const queue = engine.device.getQueue();
|
||||
const queue = core.device.getQueue();
|
||||
|
||||
// TODO: Refactor texture atlas size number
|
||||
app.texture_atlas_data = try AtlasRGB8.init(engine.allocator, 1280);
|
||||
app.texture_atlas_data = try AtlasRGB8.init(core.allocator, 1280);
|
||||
const atlas_size = gpu.Extent3D{ .width = app.texture_atlas_data.size, .height = app.texture_atlas_data.size };
|
||||
const atlas_float_size = @intToFloat(f32, app.texture_atlas_data.size);
|
||||
|
||||
const texture = engine.device.createTexture(&.{
|
||||
const texture = core.device.createTexture(&.{
|
||||
.size = atlas_size,
|
||||
.format = .rgba8_unorm,
|
||||
.usage = .{
|
||||
|
|
@ -59,35 +59,35 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
|||
.rows_per_image = @intCast(u32, atlas_size.height),
|
||||
};
|
||||
|
||||
const img = try zigimg.Image.fromMemory(engine.allocator, @embedFile("../assets/gotta-go-fast.png"));
|
||||
const img = try zigimg.Image.fromMemory(core.allocator, @embedFile("../assets/gotta-go-fast.png"));
|
||||
defer img.deinit();
|
||||
|
||||
const atlas_img_region = try app.texture_atlas_data.reserve(engine.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);
|
||||
|
||||
switch (img.pixels.?) {
|
||||
.Rgba32 => |pixels| app.texture_atlas_data.set(atlas_img_region, pixels),
|
||||
.Rgb24 => |pixels| {
|
||||
const data = try rgb24ToRgba32(engine.allocator, pixels);
|
||||
defer data.deinit(engine.allocator);
|
||||
const data = try rgb24ToRgba32(core.allocator, pixels);
|
||||
defer data.deinit(core.allocator);
|
||||
app.texture_atlas_data.set(atlas_img_region, data.Rgba32);
|
||||
},
|
||||
else => @panic("unsupported image color format"),
|
||||
}
|
||||
|
||||
const white_tex_scale = 80;
|
||||
var atlas_white_region = try app.texture_atlas_data.reserve(engine.allocator, white_tex_scale, white_tex_scale);
|
||||
var atlas_white_region = try app.texture_atlas_data.reserve(core.allocator, white_tex_scale, white_tex_scale);
|
||||
atlas_white_region.x += 1;
|
||||
atlas_white_region.y += 1;
|
||||
atlas_white_region.width -= 2;
|
||||
atlas_white_region.height -= 2;
|
||||
const white_texture_uv_data = atlas_white_region.getUVData(atlas_float_size);
|
||||
var white_tex_data = try engine.allocator.alloc(zigimg.color.Rgba32, white_tex_scale * white_tex_scale);
|
||||
var white_tex_data = try core.allocator.alloc(zigimg.color.Rgba32, white_tex_scale * white_tex_scale);
|
||||
std.mem.set(zigimg.color.Rgba32, white_tex_data, zigimg.color.Rgba32.initRGB(0xff, 0xff, 0xff));
|
||||
app.texture_atlas_data.set(atlas_white_region, white_tex_data);
|
||||
|
||||
app.vertices = try std.ArrayList(draw.Vertex).initCapacity(engine.allocator, 9);
|
||||
app.fragment_uniform_list = try std.ArrayList(draw.FragUniform).initCapacity(engine.allocator, 3);
|
||||
app.vertices = try std.ArrayList(draw.Vertex).initCapacity(core.allocator, 9);
|
||||
app.fragment_uniform_list = try std.ArrayList(draw.FragUniform).initCapacity(core.allocator, 3);
|
||||
|
||||
// Quick test for using freetype
|
||||
const lib = try ft.Library.init();
|
||||
|
|
@ -95,13 +95,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
|||
|
||||
const size_multiplier = 5;
|
||||
const character = "è";
|
||||
var label = try Label.init(lib, "freetype/upstream/assets/FiraSans-Regular.ttf", 0, 110 * size_multiplier, engine.allocator);
|
||||
var label = try Label.init(lib, "freetype/upstream/assets/FiraSans-Regular.ttf", 0, 110 * size_multiplier, core.allocator);
|
||||
defer label.deinit();
|
||||
// try label.print(app, "All your game's bases are belong to us èçòà", .{}, @Vector(2, f32){ 0, 420 }, @Vector(4, f32){ 1, 1, 1, 1 });
|
||||
try label.print(app, character, .{}, @Vector(2, f32){ 50 * size_multiplier, 40 }, @Vector(4, f32){ 1, 1, 1, 1 });
|
||||
|
||||
var resizable_label: ResizableLabel = undefined;
|
||||
try resizable_label.init(lib, "freetype/upstream/assets/FiraSans-Regular.ttf", 0, engine.allocator, white_texture_uv_data);
|
||||
try resizable_label.init(lib, "freetype/upstream/assets/FiraSans-Regular.ttf", 0, core.allocator, white_texture_uv_data);
|
||||
defer resizable_label.deinit();
|
||||
try resizable_label.print(app, character, .{}, @Vector(4, f32){ 0, 40, 0, 0 }, @Vector(4, f32){ 1, 1, 1, 1 }, 80 * size_multiplier);
|
||||
|
||||
|
|
@ -113,7 +113,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
|||
app.texture_atlas_data.data,
|
||||
);
|
||||
|
||||
const wsize = engine.getWindowSize();
|
||||
const wsize = core.getWindowSize();
|
||||
const window_width = @intToFloat(f32, wsize.width);
|
||||
const window_height = @intToFloat(f32, wsize.height);
|
||||
const triangle_scale = 250;
|
||||
|
|
@ -128,12 +128,12 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
|||
// try draw.quad(app, .{ 0, 0 }, .{ 480, 480 }, .{}, .{ .bottom_left = .{ 0, 0 }, .width_and_height = .{ 1, 1 } });
|
||||
// try draw.circle(app, .{ window_width / 2, window_height / 2 }, window_height / 2 - 10, .{ 0, 0.5, 0.75, 1.0 }, white_texture_uv_data);
|
||||
|
||||
const vs_module = engine.device.createShaderModule(&.{
|
||||
const vs_module = core.device.createShaderModule(&.{
|
||||
.label = "my vertex shader",
|
||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||
});
|
||||
|
||||
const fs_module = engine.device.createShaderModule(&.{
|
||||
const fs_module = core.device.createShaderModule(&.{
|
||||
.label = "my fragment shader",
|
||||
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
||||
});
|
||||
|
|
@ -152,7 +152,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
|||
};
|
||||
|
||||
const color_target = gpu.ColorTargetState{
|
||||
.format = engine.swap_chain_format,
|
||||
.format = core.swap_chain_format,
|
||||
.blend = &blend,
|
||||
.write_mask = gpu.ColorWriteMask.all,
|
||||
};
|
||||
|
|
@ -167,13 +167,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
|||
const fbgle = gpu.BindGroupLayout.Entry.buffer(1, .{ .fragment = true }, .read_only_storage, true, 0);
|
||||
const sbgle = gpu.BindGroupLayout.Entry.sampler(2, .{ .fragment = true }, .filtering);
|
||||
const tbgle = gpu.BindGroupLayout.Entry.texture(3, .{ .fragment = true }, .float, .dimension_2d, false);
|
||||
const bgl = engine.device.createBindGroupLayout(
|
||||
const bgl = core.device.createBindGroupLayout(
|
||||
&gpu.BindGroupLayout.Descriptor{
|
||||
.entries = &.{ vbgle, fbgle, sbgle, tbgle },
|
||||
},
|
||||
);
|
||||
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
||||
const pipeline_layout = engine.device.createPipelineLayout(&.{
|
||||
const pipeline_layout = core.device.createPipelineLayout(&.{
|
||||
.bind_group_layouts = &bind_group_layouts,
|
||||
});
|
||||
|
||||
|
|
@ -199,30 +199,30 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
|||
},
|
||||
};
|
||||
|
||||
const vertex_buffer = engine.device.createBuffer(&.{
|
||||
const vertex_buffer = core.device.createBuffer(&.{
|
||||
.usage = .{ .copy_dst = true, .vertex = true },
|
||||
.size = @sizeOf(draw.Vertex) * app.vertices.items.len,
|
||||
.mapped_at_creation = false,
|
||||
});
|
||||
|
||||
const vertex_uniform_buffer = engine.device.createBuffer(&.{
|
||||
const vertex_uniform_buffer = core.device.createBuffer(&.{
|
||||
.usage = .{ .copy_dst = true, .uniform = true },
|
||||
.size = @sizeOf(draw.VertexUniform),
|
||||
.mapped_at_creation = false,
|
||||
});
|
||||
|
||||
const frag_uniform_buffer = engine.device.createBuffer(&.{
|
||||
const frag_uniform_buffer = core.device.createBuffer(&.{
|
||||
.usage = .{ .copy_dst = true, .storage = true },
|
||||
.size = @sizeOf(draw.FragUniform) * app.fragment_uniform_list.items.len,
|
||||
.mapped_at_creation = false,
|
||||
});
|
||||
|
||||
const sampler = engine.device.createSampler(&.{
|
||||
const sampler = core.device.createSampler(&.{
|
||||
// .mag_filter = .linear,
|
||||
// .min_filter = .linear,
|
||||
});
|
||||
|
||||
const bind_group = engine.device.createBindGroup(
|
||||
const bind_group = core.device.createBindGroup(
|
||||
&gpu.BindGroup.Descriptor{
|
||||
.layout = bgl,
|
||||
.entries = &.{
|
||||
|
|
@ -234,7 +234,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
|||
},
|
||||
);
|
||||
|
||||
app.pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
|
||||
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||
app.queue = queue;
|
||||
app.vertex_buffer = vertex_buffer;
|
||||
app.vertex_uniform_buffer = vertex_uniform_buffer;
|
||||
|
|
@ -250,28 +250,28 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
|||
bgl.release();
|
||||
}
|
||||
|
||||
pub fn deinit(app: *App, engine: *mach.Engine) void {
|
||||
pub fn deinit(app: *App, core: *mach.Core) void {
|
||||
app.vertex_buffer.release();
|
||||
app.vertex_uniform_buffer.release();
|
||||
app.frag_uniform_buffer.release();
|
||||
app.bind_group.release();
|
||||
app.vertices.deinit();
|
||||
app.fragment_uniform_list.deinit();
|
||||
app.texture_atlas_data.deinit(engine.allocator);
|
||||
app.texture_atlas_data.deinit(core.allocator);
|
||||
}
|
||||
|
||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||
while (engine.pollEvent()) |event| {
|
||||
pub fn update(app: *App, core: *mach.Core) !void {
|
||||
while (core.pollEvent()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
if (ev.key == .space)
|
||||
engine.setShouldClose(true);
|
||||
core.setShouldClose(true);
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
||||
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||
const color_attachment = gpu.RenderPassColorAttachment{
|
||||
.view = back_buffer_view,
|
||||
.resolve_target = null,
|
||||
|
|
@ -280,7 +280,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
|||
.store_op = .store,
|
||||
};
|
||||
|
||||
const encoder = engine.device.createCommandEncoder(null);
|
||||
const encoder = core.device.createCommandEncoder(null);
|
||||
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||
.color_attachments = &.{color_attachment},
|
||||
};
|
||||
|
|
@ -295,7 +295,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
|||
app.update_frag_uniform_buffer = false;
|
||||
}
|
||||
if (app.update_vertex_uniform_buffer) {
|
||||
encoder.writeBuffer(app.vertex_uniform_buffer, 0, draw.VertexUniform, &.{try getVertexUniformBufferObject(engine)});
|
||||
encoder.writeBuffer(app.vertex_uniform_buffer, 0, draw.VertexUniform, &.{try getVertexUniformBufferObject(core)});
|
||||
app.update_vertex_uniform_buffer = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -313,11 +313,11 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
|||
|
||||
app.queue.submit(&.{command});
|
||||
command.release();
|
||||
engine.swap_chain.?.present();
|
||||
core.swap_chain.?.present();
|
||||
back_buffer_view.release();
|
||||
}
|
||||
|
||||
pub fn resize(app: *App, _: *mach.Engine, _: u32, _: u32) !void {
|
||||
pub fn resize(app: *App, _: *mach.Core, _: u32, _: u32) !void {
|
||||
app.update_vertex_uniform_buffer = true;
|
||||
}
|
||||
|
||||
|
|
@ -331,12 +331,12 @@ fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []zigimg.color.Rgb24) !zigimg
|
|||
}
|
||||
|
||||
// Move to draw.zig
|
||||
pub fn getVertexUniformBufferObject(engine: *mach.Engine) !draw.VertexUniform {
|
||||
pub fn getVertexUniformBufferObject(core: *mach.Core) !draw.VertexUniform {
|
||||
// Note: We use window width/height here, not framebuffer width/height.
|
||||
// On e.g. macOS, window size may be 640x480 while framebuffer size may be
|
||||
// 1280x960 (subpixels.) Doing this lets us use a pixel, not subpixel,
|
||||
// coordinate system.
|
||||
const window_size = engine.getWindowSize();
|
||||
const window_size = core.getWindowSize();
|
||||
const proj = zm.orthographicRh(
|
||||
@intToFloat(f32, window_size.width),
|
||||
@intToFloat(f32, window_size.height),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue