examples: fix depth texture recreation

This commit is contained in:
PiergiorgioZagaria 2022-05-18 21:54:04 +02:00 committed by Stephen Gutekanst
parent 3bb45c75a1
commit a4e8de2a83
3 changed files with 67 additions and 61 deletions

View file

@ -22,8 +22,7 @@ queue: gpu.Queue,
cube: Cube,
camera: Camera,
light: Light,
depth: Texture,
depth_size: mach.Size,
depth: ?Texture,
keys: u8 = 0,
const Dir = struct {
@ -59,21 +58,15 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
app.queue = engine.gpu_driver.device.getQueue();
app.cube = Cube.init(engine);
app.light = Light.init(engine);
app.depth = Texture.depth(engine.gpu_driver.device, size.width, size.height);
app.depth_size = size;
app.depth = null;
app.camera = Camera.init(engine.gpu_driver.device, eye, target, vec3(0.0, 1.0, 0.0), aspect_ratio, 45.0, 0.1, 100.0);
}
pub fn deinit(_: *App, _: *mach.Engine) void {}
pub fn deinit(app: *App, _: *mach.Engine) void {
app.depth.?.release();
}
pub fn update(app: *App, engine: *mach.Engine) !bool {
// If window is resized, recreate depth buffer otherwise we cannot use it.
const size = engine.core.getFramebufferSize() catch unreachable; // TODO: return type inference can't handle this
if (size.width != app.depth_size.width or size.height != app.depth_size.height) {
app.depth = Texture.depth(engine.gpu_driver.device, size.width, size.height);
app.depth_size = size;
}
// move camera
const speed = zm.f32x4s(@floatCast(f32, engine.delta_time * 5));
const fwd = zm.normalize3(app.camera.target - app.camera.eye);
@ -109,7 +102,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
const render_pass_descriptor = gpu.RenderPassEncoder.Descriptor{
.color_attachments = &.{color_attachment},
.depth_stencil_attachment = &.{
.view = app.depth.view,
.view = app.depth.?.view,
.depth_load_op = .clear,
.depth_store_op = .store,
.stencil_load_op = .none,
@ -158,6 +151,15 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
return true;
}
pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
// If window is resized, recreate depth buffer otherwise we cannot use it.
if (app.depth != null) {
app.depth.?.release();
}
// It also recreates the sampler, which is a waste, but for an example it's ok
app.depth = Texture.depth(engine.gpu_driver.device, width, height);
}
const Camera = struct {
const Self = @This();

View file

@ -30,6 +30,7 @@ vertex_buffer: gpu.Buffer,
uniform_buffer: gpu.Buffer,
bind_group: gpu.BindGroup,
depth_texture: ?gpu.Texture,
depth_texture_view: gpu.TextureView,
cube_texture: gpu.Texture,
cube_texture_view: gpu.TextureView,
cube_texture_render: gpu.Texture,
@ -200,6 +201,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
app.uniform_buffer = uniform_buffer;
app.bind_group = bind_group;
app.depth_texture = null;
app.depth_texture_view = undefined;
app.cube_texture = cube_texture;
app.cube_texture_view = cube_texture_view;
app.cube_texture_render = cube_texture_render;
@ -223,6 +225,7 @@ pub fn deinit(app: *App, _: *mach.Engine) void {
app.cube_texture_view_render.release();
app.bind_group.release();
app.depth_texture.?.release();
app.depth_texture_view.release();
}
pub fn update(app: *App, engine: *mach.Engine) !bool {
@ -245,12 +248,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
};
const depth_stencil_attachment = gpu.RenderPassDepthStencilAttachment{
.view = app.depth_texture.?.createView(&gpu.TextureView.Descriptor{
.format = .depth24_plus,
.dimension = .dimension_2d,
.array_layer_count = 1,
.mip_level_count = 1,
}),
.view = app.depth_texture_view,
.depth_load_op = .clear,
.depth_store_op = .store,
.depth_clear_value = 1.0,
@ -347,6 +345,14 @@ pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
.format = engine.gpu_driver.swap_chain_format,
});
app.depth_texture_view.release();
app.depth_texture_view = app.depth_texture.?.createView(&gpu.TextureView.Descriptor{
.format = .depth24_plus,
.dimension = .dimension_2d,
.array_layer_count = 1,
.mip_level_count = 1,
});
app.cube_texture_view.release();
app.cube_texture_view = app.cube_texture.createView(&gpu.TextureView.Descriptor{
.format = engine.gpu_driver.swap_chain_format,
@ -374,11 +380,16 @@ pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
},
);
} else {
// The first time resize is called, width and height are set to 0
app.depth_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
.usage = .{ .render_attachment = true },
.size = .{ .width = engine.gpu_driver.current_desc.width, .height = engine.gpu_driver.current_desc.height },
.size = .{ .width = width, .height = height },
.format = .depth24_plus,
});
app.depth_texture_view = app.depth_texture.?.createView(&gpu.TextureView.Descriptor{
.format = .depth24_plus,
.dimension = .dimension_2d,
.array_layer_count = 1,
.mip_level_count = 1,
});
}
}

View file

@ -18,8 +18,8 @@ queue: gpu.Queue,
vertex_buffer: gpu.Buffer,
uniform_buffer: gpu.Buffer,
bind_group: gpu.BindGroup,
depth_texture: gpu.Texture,
depth_size: mach.Size,
depth_texture: ?gpu.Texture,
depth_texture_view: gpu.TextureView,
const App = @This();
@ -165,26 +165,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
},
);
const size = try engine.core.getFramebufferSize();
const depth_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
.size = gpu.Extent3D{
.width = size.width,
.height = size.height,
},
.format = .depth24_plus,
.usage = .{
.render_attachment = true,
.texture_binding = true,
},
});
app.pipeline = pipeline;
app.queue = queue;
app.vertex_buffer = vertex_buffer;
app.uniform_buffer = uniform_buffer;
app.bind_group = bind_group;
app.depth_texture = depth_texture;
app.depth_size = size;
app.depth_texture = null;
app.depth_texture_view = undefined;
vs_module.release();
fs_module.release();
@ -194,26 +181,11 @@ pub fn deinit(app: *App, _: *mach.Engine) void {
app.vertex_buffer.release();
app.uniform_buffer.release();
app.bind_group.release();
app.depth_texture.?.release();
app.depth_texture_view.release();
}
pub fn update(app: *App, engine: *mach.Engine) !bool {
// If window is resized, recreate depth buffer otherwise we cannot use it.
const size = engine.core.getFramebufferSize() catch unreachable; // TODO: return type inference can't handle this
if (size.width != app.depth_size.width or size.height != app.depth_size.height) {
app.depth_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
.size = gpu.Extent3D{
.width = size.width,
.height = size.height,
},
.format = .depth24_plus,
.usage = .{
.render_attachment = true,
.texture_binding = true,
},
});
app.depth_size = size;
}
const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
@ -226,12 +198,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
.color_attachments = &.{color_attachment},
.depth_stencil_attachment = &.{
.view = app.depth_texture.createView(&gpu.TextureView.Descriptor{
.format = .depth24_plus,
.dimension = .dimension_2d,
.array_layer_count = 1,
.mip_level_count = 1,
}),
.view = app.depth_texture_view,
.depth_clear_value = 1.0,
.depth_load_op = .clear,
.depth_store_op = .store,
@ -278,6 +245,32 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
return true;
}
pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
// If window is resized, recreate depth buffer otherwise we cannot use it.
if (app.depth_texture != null) {
app.depth_texture.?.release();
app.depth_texture_view.release();
}
app.depth_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{
.size = gpu.Extent3D{
.width = width,
.height = height,
},
.format = .depth24_plus,
.usage = .{
.render_attachment = true,
.texture_binding = true,
},
});
app.depth_texture_view = app.depth_texture.?.createView(&gpu.TextureView.Descriptor{
.format = .depth24_plus,
.dimension = .dimension_2d,
.array_layer_count = 1,
.mip_level_count = 1,
});
}
fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []zigimg.color.Rgb24) !zigimg.color.ColorStorage {
const out = try zigimg.color.ColorStorage.init(allocator, .Rgba32, in.len);
var i: usize = 0;