examples: update to new Engine API

This commit is contained in:
iddev5 2022-06-01 12:28:24 +05:30 committed by Stephen Gutekanst
parent 50fe649ab1
commit 03491dfd27
9 changed files with 170 additions and 170 deletions

View file

@ -4,8 +4,8 @@
//! We also need a second texture to use on the cube, that after the render pass
//! needs to copy the other texture. We can't use the same texture since
//! it would interfere with the sincronization on the gpu during the render pass.
//! This demo currently does not work on opengl, because engine.gpu_driver.current_desc.width/height,
//! are set to 0 after engine.gpu_driver.init() and because webgpu does not implement copyTextureToTexture,
//! This demo currently does not work on opengl, because engine.current_desc.width/height,
//! are set to 0 after engine.init() and because webgpu does not implement copyTextureToTexture,
//! for opengl
const std = @import("std");
@ -41,9 +41,9 @@ bgl: gpu.BindGroupLayout,
pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try mach.Timer.start();
try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null });
try engine.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null });
const vs_module = engine.gpu_driver.device.createShaderModule(&.{
const vs_module = engine.device.createShaderModule(&.{
.label = "my vertex shader",
.code = .{ .wgsl = @embedFile("vert.wgsl") },
});
@ -59,7 +59,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
.attributes = &vertex_attributes,
};
const fs_module = engine.gpu_driver.device.createShaderModule(&.{
const fs_module = engine.device.createShaderModule(&.{
.label = "my fragment shader",
.code = .{ .wgsl = @embedFile("frag.wgsl") },
});
@ -77,7 +77,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
},
};
const color_target = gpu.ColorTargetState{
.format = engine.gpu_driver.swap_chain_format,
.format = engine.swap_chain_format,
.blend = &blend,
.write_mask = gpu.ColorWriteMask.all,
};
@ -91,14 +91,14 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
const bgle_buffer = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bgle_sampler = gpu.BindGroupLayout.Entry.sampler(1, .{ .fragment = true }, .filtering);
const bgle_textureview = gpu.BindGroupLayout.Entry.texture(2, .{ .fragment = true }, .float, .dimension_2d, false);
const bgl = engine.gpu_driver.device.createBindGroupLayout(
const bgl = engine.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{
.entries = &.{ bgle_buffer, bgle_sampler, bgle_textureview },
},
);
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
const pipeline_layout = engine.gpu_driver.device.createPipelineLayout(&.{
const pipeline_layout = engine.device.createPipelineLayout(&.{
.bind_group_layouts = &bind_group_layouts,
});
@ -128,7 +128,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
},
};
const vertex_buffer = engine.gpu_driver.device.createBuffer(&.{
const vertex_buffer = engine.device.createBuffer(&.{
.usage = .{ .vertex = true },
.size = @sizeOf(Vertex) * vertices.len,
.mapped_at_creation = true,
@ -137,44 +137,44 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
std.mem.copy(Vertex, vertex_mapped, vertices[0..]);
vertex_buffer.unmap();
const uniform_buffer = engine.gpu_driver.device.createBuffer(&.{
const uniform_buffer = engine.device.createBuffer(&.{
.usage = .{ .copy_dst = true, .uniform = true },
.size = @sizeOf(UniformBufferObject),
.mapped_at_creation = false,
});
// The texture to put on the cube
const cube_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
const cube_texture = engine.device.createTexture(&gpu.Texture.Descriptor{
.usage = .{ .texture_binding = true, .copy_dst = true },
.size = .{ .width = engine.gpu_driver.current_desc.width, .height = engine.gpu_driver.current_desc.height },
.format = engine.gpu_driver.swap_chain_format,
.size = .{ .width = engine.current_desc.width, .height = engine.current_desc.height },
.format = engine.swap_chain_format,
});
// The texture on which we render
const cube_texture_render = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
const cube_texture_render = engine.device.createTexture(&gpu.Texture.Descriptor{
.usage = .{ .render_attachment = true, .copy_src = true },
.size = .{ .width = engine.gpu_driver.current_desc.width, .height = engine.gpu_driver.current_desc.height },
.format = engine.gpu_driver.swap_chain_format,
.size = .{ .width = engine.current_desc.width, .height = engine.current_desc.height },
.format = engine.swap_chain_format,
});
const sampler = engine.gpu_driver.device.createSampler(&gpu.Sampler.Descriptor{
const sampler = engine.device.createSampler(&gpu.Sampler.Descriptor{
.mag_filter = .linear,
.min_filter = .linear,
});
const cube_texture_view = cube_texture.createView(&gpu.TextureView.Descriptor{
.format = engine.gpu_driver.swap_chain_format,
.format = engine.swap_chain_format,
.dimension = .dimension_2d,
.mip_level_count = 1,
.array_layer_count = 1,
});
const cube_texture_view_render = cube_texture_render.createView(&gpu.TextureView.Descriptor{
.format = engine.gpu_driver.swap_chain_format,
.format = engine.swap_chain_format,
.dimension = .dimension_2d,
.mip_level_count = 1,
.array_layer_count = 1,
});
const bind_group = engine.gpu_driver.device.createBindGroup(
const bind_group = engine.device.createBindGroup(
&gpu.BindGroup.Descriptor{
.layout = bgl,
.entries = &.{
@ -185,8 +185,8 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
},
);
app.pipeline = engine.gpu_driver.device.createRenderPipeline(&pipeline_descriptor);
app.queue = engine.gpu_driver.device.getQueue();
app.pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
app.queue = engine.device.getQueue();
app.vertex_buffer = vertex_buffer;
app.uniform_buffer = uniform_buffer;
app.bind_group = bind_group;
@ -219,18 +219,18 @@ pub fn deinit(app: *App, _: *mach.Engine) void {
}
pub fn update(app: *App, engine: *mach.Engine) !bool {
while (engine.core.pollEvent()) |event| {
while (engine.pollEvent()) |event| {
switch (event) {
.key_press => |ev| {
if (ev.key == .space)
engine.core.setShouldClose(true);
engine.setShouldClose(true);
},
else => {},
}
}
const cube_view = app.cube_texture_view_render;
const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView();
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
const cube_color_attachment = gpu.RenderPassColorAttachment{
.view = cube_view,
@ -256,7 +256,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
.stencil_store_op = .none,
};
const encoder = engine.gpu_driver.device.createCommandEncoder(null);
const encoder = engine.device.createCommandEncoder(null);
const cube_render_pass_info = gpu.RenderPassEncoder.Descriptor{
.color_attachments = &.{cube_color_attachment},
.depth_stencil_attachment = &depth_stencil_attachment,
@ -276,7 +276,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
);
const proj = zm.perspectiveFovRh(
(std.math.pi * 2.0 / 5.0),
@intToFloat(f32, engine.gpu_driver.current_desc.width) / @intToFloat(f32, engine.gpu_driver.current_desc.height),
@intToFloat(f32, engine.current_desc.width) / @intToFloat(f32, engine.current_desc.height),
1,
100,
);
@ -301,7 +301,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
&gpu.ImageCopyTexture{
.texture = app.cube_texture,
},
&.{ .width = engine.gpu_driver.current_desc.width, .height = engine.gpu_driver.current_desc.height },
&.{ .width = engine.current_desc.width, .height = engine.current_desc.height },
);
const cube_pass = encoder.beginRenderPass(&cube_render_pass_info);
@ -317,7 +317,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
app.queue.submit(&.{command});
command.release();
engine.gpu_driver.swap_chain.?.present();
engine.swap_chain.?.present();
back_buffer_view.release();
return true;
@ -326,23 +326,23 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
if (app.depth_texture != null) {
app.depth_texture.?.release();
app.depth_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
app.depth_texture = engine.device.createTexture(&gpu.Texture.Descriptor{
.usage = .{ .render_attachment = true },
.size = .{ .width = width, .height = height },
.format = .depth24_plus,
});
app.cube_texture.release();
app.cube_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
app.cube_texture = engine.device.createTexture(&gpu.Texture.Descriptor{
.usage = .{ .texture_binding = true, .copy_dst = true },
.size = .{ .width = width, .height = height },
.format = engine.gpu_driver.swap_chain_format,
.format = engine.swap_chain_format,
});
app.cube_texture_render.release();
app.cube_texture_render = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
app.cube_texture_render = engine.device.createTexture(&gpu.Texture.Descriptor{
.usage = .{ .render_attachment = true, .copy_src = true },
.size = .{ .width = width, .height = height },
.format = engine.gpu_driver.swap_chain_format,
.format = engine.swap_chain_format,
});
app.depth_texture_view.release();
@ -355,21 +355,21 @@ pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
app.cube_texture_view.release();
app.cube_texture_view = app.cube_texture.createView(&gpu.TextureView.Descriptor{
.format = engine.gpu_driver.swap_chain_format,
.format = engine.swap_chain_format,
.dimension = .dimension_2d,
.mip_level_count = 1,
.array_layer_count = 1,
});
app.cube_texture_view_render.release();
app.cube_texture_view_render = app.cube_texture_render.createView(&gpu.TextureView.Descriptor{
.format = engine.gpu_driver.swap_chain_format,
.format = engine.swap_chain_format,
.dimension = .dimension_2d,
.mip_level_count = 1,
.array_layer_count = 1,
});
app.bind_group.release();
app.bind_group = engine.gpu_driver.device.createBindGroup(
app.bind_group = engine.device.createBindGroup(
&gpu.BindGroup.Descriptor{
.layout = app.bgl,
.entries = &.{
@ -380,7 +380,7 @@ pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
},
);
} else {
app.depth_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
app.depth_texture = engine.device.createTexture(&gpu.Texture.Descriptor{
.usage = .{ .render_attachment = true },
.size = .{ .width = width, .height = height },
.format = .depth24_plus,