Resizable window

This commit is contained in:
abux 2026-07-29 15:56:52 +01:00
parent f404232edb
commit 408adc5623
7 changed files with 94 additions and 3 deletions

View file

@ -28,6 +28,7 @@ pub const GPUMaterial = struct { // TODO: Should prob create Material Cache inst
//
pipelines: gtl.KeyedSlotMap(Material.Unique, GPUPipeline),
materials: gtl.SlotMap(GPUMaterial),
shader_cache: *ShaderCache,
queue: *wgpu.WGPUQueueImpl,
device: *wgpu.WGPUDeviceImpl,
@ -43,6 +44,7 @@ pub fn init(
) Self {
return .{
.pipelines = .init(alloc),
.materials = .init(alloc),
.shader_cache = shader_cache,
.queue = queue,
.device = device,
@ -58,8 +60,15 @@ pub fn deinit(self: *Self) void {
wgpu.wgpuRenderPipelineRelease(entry.value.raw);
}
// --- FREE MATERIALS ---
for (self.materials.items()) |gpu| {
wgpu.wgpuBufferRelease(gpu.buffer);
wgpu.wgpuBindGroupRelease(gpu.bind_group);
}
// --- # ---
self.pipelines.deinit();
self.materials.deinit();
}
/// ----------------------------------------------------

View file

@ -1,16 +1,53 @@
//! ----------------------------------------------------
//! ----------------------------------------------------
const std = @import("std");
const sdl = @import("sdl");
const sdl_glue = @import("sdl_glue");
const wgpu = @import("wgpu");
const App = @import("app");
const Window = @import("../../window/resource/window.zig");
const Resized = @import("../../window/event/resized.zig");
const WGPUInstance = @import("../../platform/resource/wgpu_instance.zig");
const WGPUDevice = @import("../../device/resource/wgpu_device.zig");
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn pollEvents(app: *App) !void {
// --- # ---
const res = try app.resources.getMany(struct {
instance: *WGPUInstance,
window: *Window,
device: *WGPUDevice,
});
var event: sdl.SDL_Event = undefined;
while (sdl.SDL_PollEvent(&event)) {
switch (event.type) {
sdl.SDL_EVENT_QUIT => try app.events.send(App.event.AppExit{}),
sdl.SDL_EVENT_WINDOW_RESIZED => {
// --- # ---
try app.events.send(Resized{
.width = @intCast(event.window.data1),
.height = @intCast(event.window.data2),
});
// --- RELEASE ---
sdl_glue.wgpuSurfaceUnconfigure(res.window.surface);
// --- CONFIGURE SURFACE ---
sdl_glue.wgpuSurfaceConfigure(
res.window.surface,
&sdl_glue.WGPUSurfaceConfiguration{
.device = @ptrCast(res.device.raw),
.format = 28, // TODO: Get format properly, idk, store it somewhere
.width = @intCast(event.window.data1),
.height = @intCast(event.window.data2),
.usage = sdl_glue.WGPUTextureUsage_RenderAttachment,
},
);
},
else => {},
}
}

View file

@ -27,6 +27,14 @@ pub const DrawCMD = union(enum) {
begin_pass: struct {
camera: Handle(Camera),
color: [4]f32 = .{ 0, 0, 0, 255 },
load_op: enum {
load,
clear,
} = .clear,
store_op: enum {
store,
discard,
} = .store,
},
end_pass,

View file

@ -32,6 +32,17 @@ pub fn begin(app: *App) !void {
.material = .{},
},
});
}
{ // TEST
// --- DRAW ---
try res.renderer.draw(.{
.begin_pass = .{
.camera = undefined,
.load_op = .load,
},
});
defer res.renderer.draw(.end_pass) catch unreachable;
// --- # ---
try res.renderer.draw(.{

View file

@ -6,6 +6,7 @@ const wgpu = @import("wgpu");
const App = @import("app");
const Window = @import("../../window/resource/window.zig");
const Resized = @import("../../window/event/resized.zig");
const Device = @import("../../device/resource/wgpu_device.zig");
const Renderer = @import("../resource/renderer.zig");
const PipelineCache = @import("../../cache/resource/pipeline_cache.zig");
@ -23,6 +24,11 @@ pub fn draw(app: *App) !void {
mesh_cache: *MeshCache,
});
// --- DO NONE ON WINDOW RESIZE ---
if (app.events.consume(Resized)) |_| {
return;
}
// --- SURFACE TEXTURE ---
var surface_texture: wgpu.WGPUSurfaceTexture = .{};
defer wgpu.wgpuTextureRelease(surface_texture.texture);
@ -33,6 +39,17 @@ pub fn draw(app: *App) !void {
&surface_texture,
);
// --- DEBUG ---
switch (surface_texture.status) {
wgpu.WGPUSurfaceGetCurrentTextureStatus_Error => {
std.debug.print("\x1b[31m[SURFACE_TEXTURE_ERROR]\x1b[0m\n", .{});
},
wgpu.WGPUSurfaceGetCurrentTextureStatus_Outdated => {
std.debug.print("\x1b[31m[SURFACE_TEXTURE_OUTDATED]\x1b[0m\n", .{});
},
else => {},
}
// --- GET SURFACE COLOR VIEW ---
const target_view = wgpu.wgpuTextureCreateView(surface_texture.texture, &wgpu.WGPUTextureViewDescriptor{
.format = wgpu.wgpuTextureGetFormat(surface_texture.texture),
@ -72,8 +89,14 @@ pub fn draw(app: *App) !void {
.colorAttachments = &wgpu.WGPURenderPassColorAttachment{
.view = target_view,
.resolveTarget = null,
.loadOp = wgpu.WGPULoadOp_Clear,
.storeOp = wgpu.WGPUStoreOp_Store,
.loadOp = switch (v.load_op) {
.load => wgpu.WGPULoadOp_Load,
.clear => wgpu.WGPULoadOp_Clear,
},
.storeOp = switch (v.store_op) {
.store => wgpu.WGPUStoreOp_Store,
.discard => wgpu.WGPUStoreOp_Discard,
},
.clearValue = .{
.r = v.color[0],
.g = v.color[1],

View file

@ -0,0 +1,2 @@
width: u32,
height: u32,

View file

@ -17,6 +17,7 @@ const WGPUDevice = @import("../device/resource/wgpu_device.zig");
//
pub const Window = @import("resource/window.zig");
pub const Resized = @import("event/resized.zig");
/// ----------------------------------------------------
/// ----------------------------------------------------
@ -50,7 +51,7 @@ pub fn init(app: *App) !void {
"NYXEngine",
800,
600,
0,
sdl.SDL_WINDOW_RESIZABLE | sdl.SDL_WINDOW_HIGH_PIXEL_DENSITY,
) orelse return error.CreateWindow;
// --- SURFACE ---