Skip to content
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

float32 conversion #3663

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d8ada72
use proper float32 scaling (wip)
SimonDanisch Feb 29, 2024
3dca738
allow types to pass through PointBased conversion [prototyping]
ffreyer Mar 2, 2024
c9bbf5d
constants overwrite [prototyping]
ffreyer Mar 2, 2024
d5ab55c
Float64 boundingboxes/data_limits
ffreyer Mar 2, 2024
e7549a1
Float64 axis limits
ffreyer Mar 2, 2024
7eb0603
fix range step cannot be 0 (Float64 limits/ticks in lineaxis)
ffreyer Mar 2, 2024
795c6b7
prototype new version of Float scaling
ffreyer Mar 2, 2024
69e3d05
change model/transform_func to Float64
ffreyer Mar 3, 2024
6e68de5
add Float32Convert -> Mat4d conversion
ffreyer Mar 3, 2024
05756e9
get CairoMakie working
ffreyer Mar 3, 2024
1f8fcf3
move f32 cnversion to scene & prototype CairoMakie
ffreyer Mar 4, 2024
0dce2ce
rename and actually convert to Float32
ffreyer Mar 4, 2024
d2a2087
prototype GLMakie
ffreyer Mar 4, 2024
6ada066
Merge branch 'breaking-0.21' into sd/float32-conversion
ffreyer Mar 5, 2024
c692aaa
cleanup prints
ffreyer Mar 5, 2024
bcc4bdc
get lines working again
ffreyer Mar 5, 2024
af49f41
get GLMakie working + some interactivity
ffreyer Mar 5, 2024
c42a9c3
fix rect zoom
ffreyer Mar 5, 2024
bb6d675
fix typo
ffreyer Mar 5, 2024
5404890
disable old f32 conversion
ffreyer Mar 5, 2024
05c2705
hide most screens
ffreyer Mar 5, 2024
c662066
some test fixes
ffreyer Mar 5, 2024
9a5c4da
fix normalmatrix types
ffreyer Mar 5, 2024
c844ee2
Merge branch 'sd/axis-converts' into sd/float32-conversion
SimonDanisch Mar 6, 2024
764f37f
fix merge conflict
SimonDanisch Mar 6, 2024
738023f
Streamline data_limits and boundingbox (#3671)
ffreyer Mar 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions GLMakie/src/drawing_primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ function get_space(x)
return haskey(x, :markerspace) ? x.markerspace : x.space
end

# TODO consider mirroring f32convert to plot attributes
function apply_transform_and_f32_conversion(
scene::Scene, plot::AbstractPlot, data,
space::Observable = get(plot, :space, Observable(:data))
Expand Down Expand Up @@ -488,7 +489,7 @@ function draw_atomic(screen::Screen, scene::Scene, @nospecialize(plot::Lines))
space, pvm) do _f32c, f, ps, space, pvm

f32c = space in (:data, :transformed) ? _f32c : nothing
transformed = Makie.float32_convert(f32c, apply_transform(f, ps, space))
transformed = Makie.f32_convert(f32c, apply_transform(f, ps, space))
output = Vector{Point4f}(undef, length(transformed))
for i in eachindex(transformed)
output[i] = pvm * to_ndim(Point4f, to_ndim(Point3f, transformed[i], 0f0), 1f0)
Expand Down Expand Up @@ -527,6 +528,8 @@ function draw_atomic(screen::Screen, scene::Scene,
return cached_robj!(screen, scene, plot) do gl_attributes
glyphcollection = plot[1]

f32c = scene.float32convert
f32c_obs = f32c isa Makie.Float32Convert ? f32c.scaling : Observable(nothing)
transfunc = Makie.transform_func_obs(plot)
pos = gl_attributes[:position]
space = plot.space
Expand All @@ -535,8 +538,8 @@ function draw_atomic(screen::Screen, scene::Scene,
atlas = gl_texture_atlas()

# calculate quad metrics
glyph_data = lift(plot, pos, glyphcollection, offset, transfunc, space) do pos, gc, offset, transfunc, space
return Makie.text_quads(atlas, pos, to_value(gc), offset, transfunc, space)
glyph_data = lift(plot, pos, glyphcollection, offset, f32c_obs, transfunc, space) do pos, gc, offset, _, transfunc, space
return Makie.text_quads(atlas, pos, to_value(gc), offset, f32c, transfunc, space)
end

# unpack values from the one signal:
Expand Down Expand Up @@ -609,20 +612,22 @@ function draw_atomic(screen::Screen, scene::Scene, plot::Heatmap)
t = Makie.transform_func_obs(plot)
mat = plot[3]
space = plot.space # needs to happen before connect_camera! call
xypos = lift(plot, t, plot[1], plot[2], space) do t, x, y, space
f32c = scene.float32convert
f32c_obs = f32c isa Makie.Float32Convert ? f32c.scaling : Observable(nothing)
xypos = lift(plot, f32c_obs, t, plot[1], plot[2], space) do _, t, x, y, space
x1d = xy_convert(x, size(mat[], 1))
y1d = xy_convert(y, size(mat[], 2))
# Only if transform doesn't do anything, we can stay linear in 1/2D
if Makie.is_identity_transform(t)
return (x1d, y1d)
return (Makie.f32_convert(f32c, x1d, 1), Makie.f32_convert(f32c, y1d, 2))
else
# If we do any transformation, we have to assume things aren't on the grid anymore
# so x + y need to become matrices.
map!(x1d, x1d) do x
return apply_transform(t, Point(x, 0), space)[1]
return Makie.f32_convert(f32c, apply_transform(t, Point(x, 0), space)[1], 1)
end
map!(y1d, y1d) do y
return apply_transform(t, Point(0, y), space)[2]
return Makie.f32_convert(f32c, apply_transform(t, Point(0, y), space)[2], 2)
end
return (x1d, y1d)
end
Expand Down Expand Up @@ -654,9 +659,9 @@ function draw_atomic(screen::Screen, scene::Scene, plot::Image)
xmin, xmax = extrema(x)
ymin, ymax = extrema(y)
rect = Rect2f(xmin, ymin, xmax - xmin, ymax - ymin)
return decompose(Point2f, rect)
return decompose(Point2d, rect)
end
gl_attributes[:vertices] = lift(apply_transform, plot, transform_func_obs(plot), position, plot.space)
gl_attributes[:vertices] = apply_transform_and_f32_conversion(scene, plot, position)
rect = Rect2f(0, 0, 1, 1)
gl_attributes[:faces] = decompose(GLTriangleFace, rect)
gl_attributes[:texturecoordinates] = map(decompose_uv(rect)) do uv
Expand Down Expand Up @@ -712,9 +717,9 @@ function mesh_inner(screen::Screen, mesh, transfunc, gl_attributes, plot, space=
gl_attributes[:color] = nothing
end

gl_attributes[:vertices] = lift(transfunc, mesh, space) do t, mesh, space
apply_transform(t, metafree(coordinates(mesh)), space)
end
# TODO: avoid intermediate observable
positions = map(m -> metafree(coordinates(m)), mesh)
gl_attributes[:vertices] = apply_transform_and_f32_conversion(Makie.parent_scene(plot), plot, positions)
gl_attributes[:faces] = lift(x-> decompose(GLTriangleFace, x), mesh)
if hasproperty(to_value(mesh), :uv)
gl_attributes[:texturecoordinates] = lift(decompose_uv, mesh)
Expand Down Expand Up @@ -767,18 +772,20 @@ function draw_atomic(screen::Screen, scene::Scene, plot::Surface)

if all(T -> T <: Union{AbstractMatrix, AbstractVector}, types)
t = Makie.transform_func_obs(plot)
f32c = scene.float32convert
f32c_obs = f32c isa Makie.Float32Convert ? f32c.scaling : Observable(nothing)
mat = plot[3]
xypos = lift(plot, t, plot[1], plot[2], space) do t, x, y, space
xypos = lift(plot, f32c_obs, t, plot[1], plot[2], space) do f32c_inner, t, x, y, space
# Only if transform doesn't do anything, we can stay linear in 1/2D
if Makie.is_identity_transform(t)
if Makie.is_identity_transform(t) && isnothing(f32c_inner)
return (x, y)
else
matrix = if x isa AbstractMatrix && y isa AbstractMatrix
apply_transform.((t,), Point.(x, y), space)
Makie.f32_convert(f32c, apply_transform.((t,), Point.(x, y), space), space)
else
# If we do any transformation, we have to assume things aren't on the grid anymore
# so x + y need to become matrices.
[apply_transform(t, Point(x, y), space) for x in x, y in y]
[MAkie.f32_convert(f32c, apply_transform(t, Point(x, y), space), space) for x in x, y in y]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[MAkie.f32_convert(f32c, apply_transform(t, Point(x, y), space), space) for x in x, y in y]
[Makie.f32_convert(f32c, apply_transform(t, Point(x, y), space), space) for x in x, y in y]

(typo fix)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but I don't think it's worth reviewing details at this point. I'm currently prototyping without much care and plan to clean things up later, once we are more certain about whether this is a good direction to go.

Reviews on a more conceptual level would be more useful. For example if you see issues with how this would integrate with GeoMakie. I think I noted this somewhere before but the rough plan for the conversion pipeline here is:

  1. convert_arguments makes structural changes, e.g. Vector, Vector -> Vector{Point2}, leaving types alone or converting to Float32 and Float64 as appropriate
  2. apply transform_func, preferably keeping types
  3. apply Float32 conversion which may rescale data
  4. continue with model, camera as usual

I think Axis converts apply between 1 and 2, but I haven't paid much attention to that pr yet, tbh.

Also regarding transformed space - it's not the point of this pr to add it but I wouldn't mind. I want to add that and world space, but it's not a priority for me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Axis converts apply between 1 and 2, but I haven't paid much attention to that pr yet, tbh.

No, axis convert shouldn't change anything about that at this point.

end
return (first.(matrix), last.(matrix))
end
Expand Down
1 change: 1 addition & 0 deletions src/Makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const Vec3d = Vec3{Float64}
const Vec4d = Vec4{Float64}
const Rect2d = Rect2{Float64}
const Rect3d = Rect3{Float64}
const Rectd = Rect{N, Float64} where N
const Mat3d = Mat3{Float64}
const Mat4d = Mat4{Float64}
export Point2d, Point3d, Point4d, Vec2d, Vec3d, Vec4d, Rect2d, Rect3d
Expand Down
14 changes: 13 additions & 1 deletion src/float32-scaling2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ end

@inline f32_convert(::Nothing, x::Real) = Float32(x)
@inline f32_convert(::Nothing, x::VecTypes{N}) where N = to_ndim(Point{N, Float32}, x, 0)
@inline f32_convert(::Nothing, x::AbstractArray) = f32_convert.(nothing, x)

@inline function f32_convert(c::Float32Convert, p::VecTypes{N}) where N
# Point3f(::Point3i) doesn't work
Expand All @@ -94,7 +95,8 @@ end
return [to_ndim(Point{N, Float32}, c.scaling[](p), 0) for p in ps]
end

@inline f32_convert(::Nothing, x, dim) = Float32(x)
@inline f32_convert(::Nothing, x::Real, dim::Integer) = Float32(x)
@inline f32_convert(::Nothing, x::VecTypes, dim::Integer) = Float32(x[dim])
@inline f32_convert(c::Float32Convert, x::Real, dim::Integer) = Float32(c.scaling[](x, dim))
@inline function f32_convert(c::Float32Convert, xs::AbstractArray{<: Real}, dim::Integer)
return [Float32(c.scaling[](x, dim)) for x in xs]
Expand All @@ -107,6 +109,16 @@ end
end


@inline f32_convert(c::Nothing, data, ::Symbol) = f32_convert(c, data)
@inline function f32_convert(c::Float32Convert, data, space::Symbol)
return space in (:data, :transformed) ? f32_convert(c, data) : f32_convert(nothing, data)
end
@inline f32_convert(c::Nothing, data, dim::Integer, ::Symbol) = f32_convert(c, data, dim)
@inline function f32_convert(c::Float32Convert, data, dim::Integer, space::Symbol)
return space in (:data, :transformed) ? f32_convert(c, data, dim) : f32_convert(nothing, data, dim)
end


f32_convert_matrix(::Nothing, ::Symbol) = Mat4d(I)
function f32_convert_matrix(c::Float32Convert, space::Symbol)
if space in (:data, :transformed) # maybe :world?
Expand Down
8 changes: 4 additions & 4 deletions src/layouting/layouting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ _offset_to_vec(o::Vector) = to_ndim.(Vec3f, o, 0)
Base.getindex(x::ScalarOrVector, i) = x.sv isa Vector ? x.sv[i] : x.sv
Base.lastindex(x::ScalarOrVector) = x.sv isa Vector ? length(x.sv) : 1

function text_quads(atlas::TextureAtlas, position::VecTypes, gc::GlyphCollection, offset, transfunc, space)
p = apply_transform(transfunc, position, space)
function text_quads(atlas::TextureAtlas, position::VecTypes, gc::GlyphCollection, offset, f32c, transfunc, space)
p = f32_convert(f32c, apply_transform(transfunc, position, space), space)
pos = [to_ndim(Point3f, p, 0) for _ in gc.origins]

pad = atlas.glyph_padding / atlas.pix_per_glyph
Expand Down Expand Up @@ -301,8 +301,8 @@ function text_quads(atlas::TextureAtlas, position::VecTypes, gc::GlyphCollection
return pos, char_offsets, quad_offsets, uvs, scales
end

function text_quads(atlas::TextureAtlas, position::Vector, gcs::Vector{<: GlyphCollection}, offset, transfunc, space)
ps = apply_transform(transfunc, position, space)
function text_quads(atlas::TextureAtlas, position::Vector, gcs::Vector{<: GlyphCollection}, offset, f32c, transfunc, space)
ps = f32_convert(f32c, apply_transform(transfunc, position, space), space)
pos = [to_ndim(Point3f, p, 0) for (p, gc) in zip(ps, gcs) for _ in gc.origins]

pad = atlas.glyph_padding / atlas.pix_per_glyph
Expand Down
12 changes: 6 additions & 6 deletions src/makielayout/interactions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ function process_interaction(s::ScrollZoom, event::ScrollEvent, ax::Axis)
if zoom != 0
pa = viewport(scene)[]

z = (1f0 - s.speed)^zoom
z = (1.0 - s.speed)^zoom

mp_axscene = Vec4f((e.mouseposition[] .- pa.origin)..., 0, 1)
mp_axscene = Vec4d((e.mouseposition[] .- pa.origin)..., 0, 1)

# first to normal -1..1 space
mp_axfraction = (cam.pixel_space[] * mp_axscene)[Vec(1, 2)] .*
Expand Down Expand Up @@ -276,11 +276,11 @@ function process_interaction(s::ScrollZoom, event::ScrollEvent, ax::Axis)
timed_ticklabelspace_reset(ax, s.reset_timer, s.prev_xticklabelspace, s.prev_yticklabelspace, s.reset_delay)

newrect_trans = if ispressed(scene, xzoomkey[])
Rectf(newxorigin, yorigin, newxwidth, ywidth)
Rectd(newxorigin, yorigin, newxwidth, ywidth)
elseif ispressed(scene, yzoomkey[])
Rectf(xorigin, newyorigin, xwidth, newywidth)
Rectd(xorigin, newyorigin, xwidth, newywidth)
else
Rectf(newxorigin, newyorigin, newxwidth, newywidth)
Rectd(newxorigin, newyorigin, newxwidth, newywidth)
end

inv_transf = Makie.inverse_transform(transf)
Expand Down Expand Up @@ -346,7 +346,7 @@ function process_interaction(dp::DragPan, event::MouseEvent, ax)
timed_ticklabelspace_reset(ax, dp.reset_timer, dp.prev_xticklabelspace, dp.prev_yticklabelspace, dp.reset_delay)

inv_transf = Makie.inverse_transform(transf)
newrect_trans = Rectf(Vec2f(xori, yori), widths(tlimits_trans))
newrect_trans = Rectd(Vec2(xori, yori), widths(tlimits_trans))
tlimits[] = Makie.apply_transform(inv_transf, newrect_trans)

return Consume(true)
Expand Down