From 1d3f89e5f5e0daa67bafcab9ccae3cb38c945fb5 Mon Sep 17 00:00:00 2001 From: duck Date: Sun, 31 Aug 2025 05:23:27 +0500 Subject: [PATCH] Fixes and adjustments --- src/assets.zig | 3 ++- src/assets/texture.zig | 9 +++++---- src/graphics.zig | 6 +++--- src/world.zig | 6 +----- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/assets.zig b/src/assets.zig index 1c0bb8d..ba8e505 100644 --- a/src/assets.zig +++ b/src/assets.zig @@ -200,7 +200,8 @@ pub fn update() void { while (Assets.free_board.pop()) |request| { if (@atomicLoad(usize, &request.counter, .monotonic) == 0) { if (!Assets.asset_map.remove(.{ .type = request.type, .path = request.path })) continue; - request.unload(Game.alloc); + if (request.state == .loaded) + request.unload(Game.alloc); Game.alloc.destroy(request); } } diff --git a/src/assets/texture.zig b/src/assets/texture.zig index ea9e066..15f066e 100644 --- a/src/assets/texture.zig +++ b/src/assets/texture.zig @@ -11,6 +11,7 @@ sampler: *sdl.GPUSampler, pub fn load(path: []const u8, alloc: std.mem.Allocator) Assets.LoadError!@This() { _ = alloc; var file = Assets.load(.file, path); + defer Assets.free(file); const data = (file.getSync() catch return error.DependencyError).bytes; var width: u32 = undefined; @@ -24,7 +25,6 @@ pub fn load(path: []const u8, alloc: std.mem.Allocator) Assets.LoadError!@This() @ptrCast(&channels), 4, ); - Assets.free(file); if (image == null) return error.ParsingError; defer c.stbi_image_free(image); const image_slice = image[0..@intCast(width * height * channels)]; @@ -33,13 +33,14 @@ pub fn load(path: []const u8, alloc: std.mem.Allocator) Assets.LoadError!@This() const target_format = sdl.GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; const bytes_per_pixel = 4; + const mip_level = if(std.math.isPowerOfTwo(width) and width == height) @as(u32, Graphics.MIP_LEVEL) else @as(u32, 1); const texture = Graphics.createTexture( width, height, target_format, sdl.GPU_TEXTUREUSAGE_SAMPLER | sdl.GPU_TEXTUREUSAGE_COLOR_TARGET, - Graphics.MIP_LEVEL, + mip_level, ); errdefer Graphics.freeTexture(texture); @@ -83,7 +84,7 @@ pub fn load(path: []const u8, alloc: std.mem.Allocator) Assets.LoadError!@This() }, false); } rows_uploaded += rows_to_upload; - if (rows_to_upload == height) { + if (rows_uploaded == height) { sdl.GenerateMipmapsForGPUTexture(command_buffer, texture); } const fence = sdl.SubmitGPUCommandBufferAndAcquireFence(command_buffer) orelse return error.SdlError; @@ -91,7 +92,7 @@ pub fn load(path: []const u8, alloc: std.mem.Allocator) Assets.LoadError!@This() if (!sdl.WaitForGPUFences(Graphics.device, true, &fence, 1)) return error.SdlError; } - const sampler = Graphics.createSampler(); + const sampler = Graphics.createSampler(mip_level); return .{ .texture = texture, diff --git a/src/graphics.zig b/src/graphics.zig index 97c786b..db06acf 100644 --- a/src/graphics.zig +++ b/src/graphics.zig @@ -424,7 +424,7 @@ pub fn freeTexture(texture: *sdl.GPUTexture) void { sdl.ReleaseGPUTexture(Graphics.device, texture); } -pub fn createSampler() *sdl.GPUSampler { +pub fn createSampler(mip_level: u32) *sdl.GPUSampler { return sdl.CreateGPUSampler(Graphics.device, &sdl.GPUSamplerCreateInfo{ .address_mode_u = sdl.GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, .address_mode_v = sdl.GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, @@ -433,8 +433,8 @@ pub fn createSampler() *sdl.GPUSampler { .min_filter = sdl.GPU_FILTER_LINEAR, .mipmap_mode = sdl.GPU_SAMPLERMIPMAPMODE_LINEAR, .min_lod = 0, - .max_lod = 16, - .mip_lod_bias = -2, + .max_lod = @floatFromInt(mip_level - 1), + .mip_lod_bias = -0.5, }) orelse err.sdl(); } diff --git a/src/world.zig b/src/world.zig index 42e10f0..145df57 100644 --- a/src/world.zig +++ b/src/world.zig @@ -94,7 +94,6 @@ pub fn initDebug() void { Assets.free(Assets.load(.texture, "data/yakuza.png")); World.plane_mesh = Graphics.loadMesh(@ptrCast(&PLANE_MESH_DATA)); - World.cube_mesh = Graphics.loadMesh(@ptrCast(&CUBE_MESH_DATA)); World.table_mesh = Graphics.loadMesh(@ptrCast(&Graphics.generatePlane(0, 0, 0.5, 0.5, 8, 8))); World.texture = Assets.load(.texture, "data/yakuza.png"); World.hand_texture = Assets.load(.texture, "data/hand.png"); @@ -116,7 +115,6 @@ pub fn initDebug() void { pub fn deinit() void { Graphics.unloadMesh(World.plane_mesh); - Graphics.unloadMesh(World.cube_mesh); Graphics.unloadMesh(World.table_mesh); Assets.free(World.texture); Assets.free(World.hand_texture); @@ -425,11 +423,9 @@ fn updateOrder() void { } } -const CUBEMAP_MESH_DATA = CUBE_MESH_DATA; - const T1 = 1.0 / 3.0; const T2 = 2.0 / 3.0; -const CUBE_MESH_DATA = [_]f32{ +const CUBEMAP_MESH_DATA = [_]f32{ -0.5, 0.5, -0.5, T2, 0, -0.5, -0.5, -0.5, T2, 0.5, 0.5, 0.5, -0.5, 1, 0,