Skip to content

Commit

Permalink
[update #103] Screenshot Imgui widget: in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tiawl committed Aug 1, 2023
1 parent 56e9bd0 commit e61a510
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
74 changes: 55 additions & 19 deletions src/imgui/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -253,22 +253,10 @@ pub const context_imgui = struct
}
}

fn find_available_file (self: Self, allocator: std.mem.Allocator) !std.fs.File
fn find_available_file (self: Self, allocator: std.mem.Allocator, dir: std.fs.Dir) ![] const u8
{
_ = self;

var screenshots_dir = std.fs.cwd ().openDir ("screenshots", .{}) catch |err| blk:
{
if (err == std.fs.Dir.OpenError.FileNotFound)
{
try std.fs.cwd ().makeDir ("screenshots");
break :blk try std.fs.cwd ().openDir ("screenshots", .{});
} else {
return err;
}
};
defer screenshots_dir.close ();

const now = datetime.Datetime.now ();
var iterator = std.mem.tokenizeAny (u8, try now.formatISO8601 (allocator, true), ":.+");
var id: u32 = 0;
Expand All @@ -285,7 +273,7 @@ pub const context_imgui = struct
while (id < std.math.maxInt (u32))
{
filename = try std.fmt.allocPrint (allocator, "{s}{d}", .{ date, id, });
file = screenshots_dir.openFile (filename, .{}) catch |err|
file = dir.openFile (filename, .{}) catch |err|
{
if (err == std.fs.File.OpenError.FileNotFound)
{
Expand All @@ -305,7 +293,7 @@ pub const context_imgui = struct
}

filename = try std.fmt.allocPrint (allocator, "{s}.ppm", .{ filename, });
return try screenshots_dir.createFile (filename, .{});
return filename;
}

fn prepare_screenshot (self: Self, allocator: std.mem.Allocator, framebuffer: struct { width: u32, height: u32, }, renderer: ScreenshotRenderer) !void
Expand All @@ -315,7 +303,23 @@ pub const context_imgui = struct
// TODO: display window size
if (imgui.ImGui_ButtonEx ("Take a screenshot", button_size))
{
var file = try self.find_available_file (allocator);
try log_app ("generating ...", severity.INFO, .{});

// TODO: remove imgui window before screenshot
var screenshots_dir = std.fs.cwd ().openDir ("screenshots", .{}) catch |err| blk:
{
if (err == std.fs.Dir.OpenError.FileNotFound)
{
try std.fs.cwd ().makeDir ("screenshots");
break :blk try std.fs.cwd ().openDir ("screenshots", .{});
} else {
return err;
}
};
defer screenshots_dir.close ();

const filename = try self.find_available_file (allocator, screenshots_dir);
var file = try screenshots_dir.createFile (filename, .{});
defer file.close ();

const image_create_info = vk.ImageCreateInfo
Expand Down Expand Up @@ -579,7 +583,7 @@ pub const context_imgui = struct
vk.PipelineStageFlags { .transfer_bit = true, },
vk.DependencyFlags {},
0, null, 0, null, 1,
&swapchain_image_after_blit);
&swapchain_image_after_blit);

try renderer.device_dispatch.endCommandBuffer (command_buffers [0]);

Expand All @@ -592,7 +596,7 @@ pub const context_imgui = struct
},
};

const fence = try renderer.device_dispatch.createFence(renderer.logical_device, &vk.FenceCreateInfo {}, null);
const fence = try renderer.device_dispatch.createFence (renderer.logical_device, &vk.FenceCreateInfo {}, null);
defer renderer.device_dispatch.destroyFence (renderer.logical_device, fence, null);

try renderer.device_dispatch.queueSubmit (renderer.graphics_queue, 1, &submit_info, fence);
Expand All @@ -608,15 +612,47 @@ pub const context_imgui = struct

const subresource_layout = renderer.device_dispatch.getImageSubresourceLayout (renderer.logical_device, dst_image, &subresource);

// TODO: change as ([*] u8, ...) depending of blit support + surface format
var data = @as ([*] u8, @ptrCast ((try renderer.device_dispatch.mapMemory (renderer.logical_device, dst_image_memory, 0, vk.WHOLE_SIZE, vk.MemoryMapFlags {})).?));
defer renderer.device_dispatch.unmapMemory (renderer.logical_device, dst_image_memory);

data += subresource_layout.offset;

// TODO: change 255 depending of blit support + surface format (max: 65_536)
const header = try std.fmt.allocPrint (allocator, "P6\n{d}\n{d}\n255\n", .{ framebuffer.width, framebuffer.height, });
try file.writeAll (header);

// TODO: ppm
var x: u32 = 0;
var y: u32 = 0;
var color: [] u8 = undefined;

while (y < framebuffer.height)
{
x = 0;
while (x < framebuffer.width * 4)
{
if (renderer.blitting_supported)
{
color = try std.fmt.allocPrint (allocator, "{c}{c}{c}", .{ data [y * framebuffer.width * 4 + x], data [y * framebuffer.width * 4 + x + 1], data [y * framebuffer.width * 4 + x + 2], });
try file.writeAll (color);
} else {
// TODO: manage different format when blit is unsupported
// TODO: error message promoting for issue posting when using unsupported format
_ = color;
}

x += 4;
}

// TODO: add this line for unsupported blit ?
// data += subresource_layout.row_pitch;

y += 1;
}

try log_app ("screenshot saved into {s}", severity.INFO, .{ try screenshots_dir.realpathAlloc (allocator, filename), });

// TODO: add shader effect when screenshot finished.
}
}

Expand Down
15 changes: 0 additions & 15 deletions src/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -263,20 +263,6 @@ pub const options = struct
self.camera.zoom = (self.camera.zoom % (CAMERA_ZOOM_MAX - CAMERA_ZOOM_MIN + 1)) + CAMERA_ZOOM_MIN;
}

fn show (self: Self) !void
{
try log_app ("seed: {d}", severity.INFO, .{ self.seed });
try log_app ("window: {any}", severity.INFO, .{ self.window });

try log_app ("camera dynamic: {}", severity.INFO, .{ self.camera.dynamic });
try log_app ("camera pixel: {d}", severity.INFO, .{ self.camera.pixel });
try log_app ("zoom: {d}", severity.INFO, .{ self.camera.zoom });

try log_app ("colors smooth transition: {}", severity.INFO, .{ self.colors.smooth });

try log_app ("stars dynamic transition: {}", severity.INFO, .{ self.stars.dynamic });
}

pub fn init (allocator: std.mem.Allocator) !Self
{
var self = Self {};
Expand Down Expand Up @@ -309,7 +295,6 @@ pub const options = struct
}

self.fix_random ();
if (build.LOG_LEVEL > @intFromEnum (profile.TURBO)) try self.show ();

return self;
}
Expand Down
16 changes: 8 additions & 8 deletions src/vk/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub const context_vk = struct
fps: f32 = undefined,
descriptor_pool: vk.DescriptorPool = undefined,
descriptor_sets: [] vk.DescriptorSet = undefined,
prefered_criterias: [DEVICE_CRITERIAS] bool = undefined,
prefered_criterias: [DEVICE_CRITERIAS - 1] bool = undefined,

offscreen_width: u32 = undefined,
offscreen_height: u32 = undefined,
Expand Down Expand Up @@ -406,13 +406,13 @@ pub const context_vk = struct

fn compute_score (self: Self) u32
{
var score: u32 = 0;
var power = DEVICE_CRITERIAS - 1;
var score: u32 = 1;
var power: u32 = 1;

for (self.prefered_criterias) |criteria|
{
score += @intFromBool (criteria) * std.math.pow (@TypeOf (DEVICE_CRITERIAS), 2, power);
if (power > 0) power -= 1 else break;
power += 1;
}

return score;
Expand Down Expand Up @@ -465,12 +465,12 @@ pub const context_vk = struct
{
try log_app ("Vulkan device {s} is suitable", severity.DEBUG, .{ properties.device_name, });

self.prefered_criterias = [DEVICE_CRITERIAS] bool
// from the least to the most important
self.prefered_criterias = [DEVICE_CRITERIAS - 1] bool
{
properties.device_type == vk.PhysicalDeviceType.discrete_gpu,
candidate.graphics_family == candidate.present_family,
blitting_supported,
true,
candidate.graphics_family == candidate.present_family,
properties.device_type == vk.PhysicalDeviceType.discrete_gpu,
};

return .{
Expand Down

0 comments on commit e61a510

Please sign in to comment.