Hi-dpi support, fsaa tweaks
This commit is contained in:
12
src/game.zig
12
src/game.zig
@@ -116,6 +116,18 @@ fn processEvents() void {
|
|||||||
sdl.EVENT_MOUSE_WHEEL => {
|
sdl.EVENT_MOUSE_WHEEL => {
|
||||||
Game.mouse.wheel += event.wheel.integer_y;
|
Game.mouse.wheel += event.wheel.integer_y;
|
||||||
},
|
},
|
||||||
|
sdl.EVENT_WINDOW_RESIZED => {
|
||||||
|
if (event.window.data1 < 1 or event.window.data2 < 1) continue;
|
||||||
|
|
||||||
|
Graphics.window_width = @intCast(event.window.data1);
|
||||||
|
Graphics.window_height = @intCast(event.window.data2);
|
||||||
|
},
|
||||||
|
sdl.EVENT_WINDOW_PIXEL_SIZE_CHANGED => {
|
||||||
|
if (event.window.data1 < 1 or event.window.data2 < 1) continue;
|
||||||
|
|
||||||
|
Graphics.pixel_width = @intCast(event.window.data1);
|
||||||
|
Graphics.pixel_height = @intCast(event.window.data2);
|
||||||
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -13,6 +13,7 @@ pub var device: *sdl.GPUDevice = undefined;
|
|||||||
var command_buffer: ?*sdl.GPUCommandBuffer = null;
|
var command_buffer: ?*sdl.GPUCommandBuffer = null;
|
||||||
var render_pass: ?*sdl.GPURenderPass = null;
|
var render_pass: ?*sdl.GPURenderPass = null;
|
||||||
var render_target: ?*sdl.GPUTexture = null;
|
var render_target: ?*sdl.GPUTexture = null;
|
||||||
|
var render_fsaa: bool = undefined;
|
||||||
|
|
||||||
var shader_vert: *sdl.GPUShader = undefined;
|
var shader_vert: *sdl.GPUShader = undefined;
|
||||||
var shader_frag: *sdl.GPUShader = undefined;
|
var shader_frag: *sdl.GPUShader = undefined;
|
||||||
@@ -23,8 +24,12 @@ var pipeline: *sdl.GPUGraphicsPipeline = undefined;
|
|||||||
|
|
||||||
pub var window_width: u32 = undefined;
|
pub var window_width: u32 = undefined;
|
||||||
pub var window_height: u32 = undefined;
|
pub var window_height: u32 = undefined;
|
||||||
|
pub var pixel_width: u32 = undefined;
|
||||||
|
pub var pixel_height: u32 = undefined;
|
||||||
var fsaa_scale: u32 = 4;
|
var fsaa_scale: u32 = 4;
|
||||||
var fsaa_level: u32 = 3;
|
var fsaa_level: u32 = 3;
|
||||||
|
var render_width: u32 = undefined;
|
||||||
|
var render_height: u32 = undefined;
|
||||||
|
|
||||||
pub var camera: Camera = undefined;
|
pub var camera: Camera = undefined;
|
||||||
|
|
||||||
@@ -45,8 +50,17 @@ pub fn create() void {
|
|||||||
"",
|
"",
|
||||||
1600,
|
1600,
|
||||||
900,
|
900,
|
||||||
sdl.WINDOW_VULKAN | sdl.WINDOW_RESIZABLE,
|
sdl.WINDOW_VULKAN | sdl.WINDOW_RESIZABLE | sdl.WINDOW_HIGH_PIXEL_DENSITY,
|
||||||
) orelse err.sdl();
|
) orelse err.sdl();
|
||||||
|
if (!sdl.GetWindowSizeInPixels(Graphics.window, @ptrCast(&Graphics.window_width), @ptrCast(&Graphics.window_height))) {
|
||||||
|
Graphics.window_width = 1600;
|
||||||
|
Graphics.window_height = 900;
|
||||||
|
}
|
||||||
|
const scale = sdl.GetWindowDisplayScale(Graphics.window);
|
||||||
|
Graphics.pixel_width = @intFromFloat(@round(scale * @as(f32, @floatFromInt(Graphics.window_width))));
|
||||||
|
Graphics.pixel_height = @intFromFloat(@round(scale * @as(f32, @floatFromInt(Graphics.window_height))));
|
||||||
|
Graphics.render_width = pixel_width;
|
||||||
|
Graphics.render_height = pixel_height;
|
||||||
|
|
||||||
// Device
|
// Device
|
||||||
Graphics.device = sdl.CreateGPUDevice(
|
Graphics.device = sdl.CreateGPUDevice(
|
||||||
@@ -81,19 +95,16 @@ pub fn create() void {
|
|||||||
const target_format = sdl.GetGPUSwapchainTextureFormat(Graphics.device, Graphics.window);
|
const target_format = sdl.GetGPUSwapchainTextureFormat(Graphics.device, Graphics.window);
|
||||||
if (target_format == sdl.GPU_TEXTUREFORMAT_INVALID) err.sdl();
|
if (target_format == sdl.GPU_TEXTUREFORMAT_INVALID) err.sdl();
|
||||||
|
|
||||||
// TODO: Clean
|
|
||||||
if (!sdl.GetWindowSizeInPixels(Graphics.window, @ptrCast(&Graphics.window_width), @ptrCast(&Graphics.window_height))) err.sdl();
|
|
||||||
|
|
||||||
Graphics.depth_texture = createTexture(
|
Graphics.depth_texture = createTexture(
|
||||||
@as(u32, @intCast(Graphics.window_width)) * Graphics.fsaa_scale,
|
@as(u32, @intCast(Graphics.render_width)) * Graphics.fsaa_scale,
|
||||||
@as(u32, @intCast(Graphics.window_height)) * Graphics.fsaa_scale,
|
@as(u32, @intCast(Graphics.render_height)) * Graphics.fsaa_scale,
|
||||||
DEPTH_FORMAT,
|
DEPTH_FORMAT,
|
||||||
sdl.GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET,
|
sdl.GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET,
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
Graphics.fsaa_target = createTexture(
|
Graphics.fsaa_target = createTexture(
|
||||||
@as(u32, @intCast(Graphics.window_width)) * Graphics.fsaa_scale,
|
@as(u32, @intCast(Graphics.render_width)) * Graphics.fsaa_scale,
|
||||||
@as(u32, @intCast(Graphics.window_height)) * Graphics.fsaa_scale,
|
@as(u32, @intCast(Graphics.render_height)) * Graphics.fsaa_scale,
|
||||||
target_format,
|
target_format,
|
||||||
sdl.GPU_TEXTUREUSAGE_COLOR_TARGET | sdl.GPU_TEXTUREUSAGE_SAMPLER,
|
sdl.GPU_TEXTUREUSAGE_COLOR_TARGET | sdl.GPU_TEXTUREUSAGE_SAMPLER,
|
||||||
fsaa_level,
|
fsaa_level,
|
||||||
@@ -177,20 +188,21 @@ pub fn beginDraw() bool {
|
|||||||
// Window is probably hidden
|
// Window is probably hidden
|
||||||
if (Graphics.render_target == null or width == 0 or height == 0) return false;
|
if (Graphics.render_target == null or width == 0 or height == 0) return false;
|
||||||
|
|
||||||
if (width != Graphics.window_width or height != Graphics.window_height) {
|
if (Graphics.render_width != Graphics.pixel_width or Graphics.render_height != Graphics.pixel_height) {
|
||||||
Graphics.resetTextures(width, height);
|
Graphics.render_width = Graphics.pixel_width;
|
||||||
Graphics.camera.aspect = @as(f32, @floatFromInt(width)) / @as(f32, @floatFromInt(height));
|
Graphics.render_height = Graphics.pixel_height;
|
||||||
Graphics.window_width = width;
|
Graphics.resetTextures(Graphics.render_width, Graphics.render_height);
|
||||||
Graphics.window_height = height;
|
Graphics.camera.aspect = @as(f32, @floatFromInt(Graphics.render_width)) / @as(f32, @floatFromInt(Graphics.render_height));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Graphics.render_fsaa = Graphics.fsaa_level > 1;
|
||||||
Graphics.render_pass = sdl.BeginGPURenderPass(Graphics.command_buffer.?, &.{
|
Graphics.render_pass = sdl.BeginGPURenderPass(Graphics.command_buffer.?, &.{
|
||||||
.clear_color = .{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 1.0 },
|
.clear_color = .{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 1.0 },
|
||||||
.cycle = false,
|
.cycle = false,
|
||||||
.load_op = sdl.GPU_LOADOP_CLEAR,
|
.load_op = sdl.GPU_LOADOP_CLEAR,
|
||||||
.store_op = sdl.GPU_STOREOP_STORE,
|
.store_op = sdl.GPU_STOREOP_STORE,
|
||||||
.mip_level = 0,
|
.mip_level = 0,
|
||||||
.texture = Graphics.fsaa_target,
|
.texture = if (Graphics.render_fsaa) Graphics.fsaa_target else Graphics.render_target,
|
||||||
}, 1, &.{
|
}, 1, &.{
|
||||||
.clear_depth = 0.0,
|
.clear_depth = 0.0,
|
||||||
.load_op = sdl.GPU_LOADOP_CLEAR,
|
.load_op = sdl.GPU_LOADOP_CLEAR,
|
||||||
@@ -216,7 +228,7 @@ pub fn clearDepth() void {
|
|||||||
.load_op = sdl.GPU_LOADOP_LOAD,
|
.load_op = sdl.GPU_LOADOP_LOAD,
|
||||||
.store_op = sdl.GPU_STOREOP_STORE,
|
.store_op = sdl.GPU_STOREOP_STORE,
|
||||||
.mip_level = 0,
|
.mip_level = 0,
|
||||||
.texture = Graphics.fsaa_target,
|
.texture = if (Graphics.render_fsaa) Graphics.fsaa_target else Graphics.render_target,
|
||||||
}, 1, &.{
|
}, 1, &.{
|
||||||
.clear_depth = 0.0,
|
.clear_depth = 0.0,
|
||||||
.load_op = sdl.GPU_LOADOP_CLEAR,
|
.load_op = sdl.GPU_LOADOP_CLEAR,
|
||||||
@@ -257,22 +269,24 @@ pub fn endDraw() void {
|
|||||||
if (Graphics.render_pass) |pass| {
|
if (Graphics.render_pass) |pass| {
|
||||||
sdl.EndGPURenderPass(pass);
|
sdl.EndGPURenderPass(pass);
|
||||||
|
|
||||||
if (Graphics.fsaa_level > 1) sdl.GenerateMipmapsForGPUTexture(Graphics.command_buffer, Graphics.fsaa_target);
|
if (Graphics.render_fsaa) {
|
||||||
sdl.BlitGPUTexture(Graphics.command_buffer, &.{
|
sdl.GenerateMipmapsForGPUTexture(Graphics.command_buffer, Graphics.fsaa_target);
|
||||||
.source = .{
|
sdl.BlitGPUTexture(Graphics.command_buffer, &.{
|
||||||
.texture = Graphics.fsaa_target,
|
.source = .{
|
||||||
.w = Graphics.window_width,
|
.texture = Graphics.fsaa_target,
|
||||||
.h = Graphics.window_height,
|
.w = Graphics.render_width,
|
||||||
.mip_level = fsaa_level - 1,
|
.h = Graphics.render_height,
|
||||||
},
|
.mip_level = fsaa_level - 1,
|
||||||
.destination = .{
|
},
|
||||||
.texture = Graphics.render_target,
|
.destination = .{
|
||||||
.w = Graphics.window_width,
|
.texture = Graphics.render_target,
|
||||||
.h = Graphics.window_height,
|
.w = Graphics.render_width,
|
||||||
},
|
.h = Graphics.render_height,
|
||||||
.load_op = sdl.GPU_LOADOP_DONT_CARE,
|
},
|
||||||
.filter = sdl.GPU_FILTER_NEAREST,
|
.load_op = sdl.GPU_LOADOP_DONT_CARE,
|
||||||
});
|
.filter = sdl.GPU_FILTER_NEAREST,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!sdl.SubmitGPUCommandBuffer(Graphics.command_buffer)) err.sdl();
|
if (!sdl.SubmitGPUCommandBuffer(Graphics.command_buffer)) err.sdl();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user