-
Notifications
You must be signed in to change notification settings - Fork 793
Add Custom Shader Uniforms for Cursor Effects #6912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
df3c3d5
c224ff4
66badda
b9a4dcc
2d71fde
acf77ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,9 @@ | ||||||
const std = @import("std"); | ||||||
const Allocator = std.mem.Allocator; | ||||||
const CursorStyle = @import("../../renderer/cursor.zig").Style; | ||||||
const gl = @import("opengl"); | ||||||
const Glyph = @import("../../font/Glyph.zig"); | ||||||
const Screen = @import("../../terminal/Screen.zig"); | ||||||
const Size = @import("../size.zig").Size; | ||||||
|
||||||
const log = std.log.scoped(.opengl_custom); | ||||||
|
@@ -22,6 +25,9 @@ pub const Uniforms = extern struct { | |||||
mouse: [4]f32 align(16) = .{ 0, 0, 0, 0 }, | ||||||
date: [4]f32 align(16) = .{ 0, 0, 0, 0 }, | ||||||
sample_rate: f32 align(4) = 1, | ||||||
current_cursor: [4]f32 align(16) = .{ 0, 0, 0, 0 }, | ||||||
previous_cursor: [4]f32 align(16) = .{ 0, 0, 0, 0 }, | ||||||
cursor_change_time: f32 align(4) = 1 | ||||||
}; | ||||||
|
||||||
/// The state associated with custom shaders. This should only be initialized | ||||||
|
@@ -176,6 +182,37 @@ pub const State = struct { | |||||
null, | ||||||
); | ||||||
} | ||||||
|
||||||
// Update the cursor related uniforms | ||||||
pub fn addCursor(self: *State, size: Size, cursor: Screen.Cursor, cursor_style: CursorStyle, glyph: Glyph) void { | ||||||
// Converts the cursor's cell-based position to pixel coordinates. | ||||||
// Additionally, the y-position is adjusted to account for the inverted Y-axis | ||||||
// in the window coordinate system used by gl_FragCoord, where the origin is at the bottom-left. | ||||||
// The offset from the glyph is also added to position the cursor correctly. | ||||||
const current_x: f32 = @as(f32, @floatFromInt(cursor.x * size.cell.width + size.padding.left)) + @as(f32, @floatFromInt(glyph.offset_x)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might be misreading this, but since the two floats added together both came from integers, why not just:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't add glyph.offset directly because it's an i32, while all the other variables are u32. |
||||||
const current_y: f32 = @as(f32, @floatFromInt(size.screen.height - size.padding.top - (cursor.y + 1) * size.cell.height)) + @as(f32, @floatFromInt(glyph.offset_y)); | ||||||
|
||||||
// Determine the cursor's width and height based on the cursor style: | ||||||
// - A bar-style cursor has a width of 1.0, while other cursor styles use the glyph's width. | ||||||
const cursor_width: f32 = if (cursor_style == .bar) 1.0 else @floatFromInt(glyph.width); | ||||||
const cursor_height: f32 = @floatFromInt(glyph.height); | ||||||
|
||||||
// Check if the cursor's position or size has changed compared to the previously set values. | ||||||
const cursor_changed: bool = current_x != self.uniforms.current_cursor[0] or | ||||||
current_y != self.uniforms.current_cursor[1] or | ||||||
cursor_width != self.uniforms.current_cursor[2] or | ||||||
cursor_height != self.uniforms.current_cursor[3]; | ||||||
|
||||||
if (cursor_changed) { | ||||||
self.uniforms.previous_cursor = self.uniforms.current_cursor; | ||||||
self.uniforms.current_cursor = .{ current_x, current_y, cursor_width, cursor_height }; | ||||||
|
||||||
// Calculate the time elapsed since the first frame to update the cursor change time. | ||||||
const now = std.time.Instant.now() catch self.last_frame_time; | ||||||
const since_ns: f32 = @floatFromInt(now.since(self.first_frame_time)); | ||||||
self.uniforms.cursor_change_time = since_ns / std.time.ns_per_s; | ||||||
} | ||||||
} | ||||||
|
||||||
/// Call this prior to drawing a frame to update the time | ||||||
/// and synchronize the uniforms. This synchronizes uniforms | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at other parts of the code, I wonder if we should use the "deferred" pattern as used by
SetFontSize
,SetScreenSize
, etc. The custom state seems to be more commonly modified there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look into it and see how I can adapt it.