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

fix low hanging fruits for render performance #4485

Merged
merged 8 commits into from
Nov 5, 2024
6 changes: 2 additions & 4 deletions CairoMakie/src/infrastructure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ function cairo_draw(screen::Screen, scene::Scene)
draw_background(screen, scene)

allplots = Makie.collect_atomic_plots(scene; is_atomic_plot = is_cairomakie_atomic_plot)
zvals = Makie.zvalue2d.(allplots)
permute!(allplots, sortperm(zvals))

sort!(allplots; by=Makie.zvalue2d)
# If the backend is not a vector surface (i.e., PNG/ARGB),
# then there is no point in rasterizing twice.
should_rasterize = is_vector_backend(screen.surface)
Expand Down Expand Up @@ -120,7 +118,7 @@ function draw_background(screen::Screen, scene::Scene, root_h)
bg = scene.backgroundcolor[]
Cairo.set_source_rgba(cr, red(bg), green(bg), blue(bg), alpha(bg));
r = viewport(scene)[]
# Makie has (0,0) at bottom left, Cairo at top left. Makie extends up,
# Makie has (0,0) at bottom left, Cairo at top left. Makie extends up,
# Cairo down. Negative height breaks other backgrounds
x, y = origin(r); w, h = widths(r)
Cairo.rectangle(cr, x, root_h - y - h, w, h) # background
Expand Down
7 changes: 3 additions & 4 deletions GLMakie/src/GLAbstraction/GLRender.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ function render(list::Tuple)
return
end

function setup_clip_planes(robj)
N = to_value(get(robj.uniforms, :num_clip_planes, 0))
function setup_clip_planes(N::Integer)
for i in 0:min(7, N-1)
glEnable(GL_CLIP_DISTANCE0 + UInt32(i))
end
Expand All @@ -22,13 +21,13 @@ When rendering a specialised list of Renderables, we can do some optimizations
function render(list::Vector{RenderObject{Pre}}) where Pre
isempty(list) && return nothing
first(list).prerenderfunction()
setup_clip_planes(first(list))
vertexarray = first(list).vertexarray
program = vertexarray.program
glUseProgram(program.id)
bind(vertexarray)
for renderobject in list
renderobject.visible || continue # skip invisible
setup_clip_planes(to_value(get(renderobject.uniforms, :num_clip_planes, 0)))
# make sure we only bind new programs and vertexarray when it is actually
# different from the previous one
if renderobject.vertexarray != vertexarray
Expand Down Expand Up @@ -70,7 +69,7 @@ a lot of objects.
function render(renderobject::RenderObject, vertexarray=renderobject.vertexarray)
if renderobject.visible
renderobject.prerenderfunction()
setup_clip_planes(renderobject)
setup_clip_planes(to_value(get(renderobject.uniforms, :num_clip_planes, 0)))
program = vertexarray.program
glUseProgram(program.id)
for (key, value) in program.uniformloc
Expand Down
6 changes: 3 additions & 3 deletions GLMakie/src/GLAbstraction/GLRenderObject.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ function (sp::StandardPostrender)()
render(sp.vao, sp.primitive)
end

struct StandardPostrenderInstanced{T}
main::T
struct StandardPostrenderInstanced
n_instances::Observable{Int}
vao::GLVertexArray
primitive::GLenum
end

function (sp::StandardPostrenderInstanced)()
renderinstanced(sp.vao, to_value(sp.main), sp.primitive)
return renderinstanced(sp.vao, sp.n_instances[], sp.primitive)
end

struct EmptyPrerender end
Expand Down
2 changes: 1 addition & 1 deletion GLMakie/src/postprocessing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ function to_screen_postprocessor(framebuffer, shader_cache, screen_fb_id = nothi
default_id = isnothing(screen_fb_id) ? 0 : screen_fb_id[]
# GLFW uses 0, Gtk uses a value that we have to probe at the beginning of rendering
glBindFramebuffer(GL_FRAMEBUFFER, default_id)
glViewport(0, 0, framebuffer_size(screen.glscreen)...)
glViewport(0, 0, framebuffer_size(screen)...)
glClear(GL_COLOR_BUFFER_BIT)
GLAbstraction.render(pass) # copy postprocess
end
Expand Down
9 changes: 6 additions & 3 deletions GLMakie/src/rendering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ Renders a single frame of a `window`
function render_frame(screen::Screen; resize_buffers=true)
nw = to_native(screen)
ShaderAbstractions.switch_context!(nw)

function sortby(x)
robj = x[3]
plot = screen.cache2plot[robj.id]
Copy link
Member Author

Choose a reason for hiding this comment

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

The plot lookup was the expensive bit here, so would be nice if we could avoid it!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Dict lookups are O(1) and somewhere around 10ns. My benchmarks for the current solution only have 3% difference to the old one.
Using robj[:model] is also what made this pr fail, because lines with linestyles apply it on the CPU so it does not end up in uniforms. I would also expect this to subtly fail with f32 converts because those can change the model matrix (set it to I after applying it on the CPU)

# TODO, use actual boundingbox
return Makie.zvalue2d(plot)
# ~7% faster than calling zvalue2d doing the same thing?
return Makie.transformationmatrix(plot)[][3, 4]
# return Makie.zvalue2d(plot)
end
zvals = sortby.(screen.renderlist)
permute!(screen.renderlist, sortperm(zvals))

sort!(screen.renderlist; by=sortby)

# NOTE
# The transparent color buffer is reused by SSAO and FXAA. Changing the
Expand Down
4 changes: 3 additions & 1 deletion GLMakie/src/screen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ mutable struct Screen{GLWindow} <: MakieScreen
renderlist::Vector{Tuple{ZIndex, ScreenID, RenderObject}}
postprocessors::Vector{PostProcessor}
cache::Dict{UInt64, RenderObject}
cache2plot::Dict{UInt32, AbstractPlot}
cache2plot::Dict{UInt32, Plot}
framecache::Matrix{RGB{N0f8}}
render_tick::Observable{Makie.TickState} # listeners must not Consume(true)
window_open::Observable{Bool}
Expand Down Expand Up @@ -218,6 +218,8 @@ mutable struct Screen{GLWindow} <: MakieScreen
end
end

framebuffer_size(screen::Screen) = screen.framebuffer.resolution[]

Makie.isvisible(screen::Screen) = screen.config.visible

# for e.g. closeall, track all created screens
Expand Down
5 changes: 3 additions & 2 deletions src/layouting/transformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -536,5 +536,6 @@ end
# and this way we can use the z-value as a means to shift the drawing order
# by translating e.g. the axis spines forward so they are not obscured halfway
# by heatmaps or images
zvalue2d(x)::Float32 = Float32(Makie.translation(x)[][3] + zvalue2d(x.parent))
zvalue2d(::Nothing)::Float32 = 0f0
# zvalue2d(x)::Float32 = Float32(Makie.translation(x)[][3] + zvalue2d(x.parent))
@inline zvalue2d(x)::Float32 = Float32(transformationmatrix(x)[][3, 4])
@inline zvalue2d(::Nothing)::Float32 = 0f0
Loading