Skip to content

Commit

Permalink
Datashader (#2883)
Browse files Browse the repository at this point in the history
* copy ShadeYourData.jl and make a recipe

* use points and threads

* improve performance + add examples

* avoid aggregation matrix reallocation on every update

* use on_latest

* fix points

* remove unused destructured args

* rearrange thread array and avoid clamp

* remove `@time`

* clean up and documentation

* categorical prototype

* improve wording

* clean up + legend for categorical

* dont use async for output

* update link to script

* start adding docstrings

* improve docs and names

* fix imports

* fix docs

* address code review by @jkrumbiegel

* make videos autoplay

* fix missing var

* fix import

* update docs + support Colorbar

* add test

* clean up

* remove unused + better stacktrace

* implement texture resizing for WGLMakie

* forgot new

* don't make legend elements half transparent

* Hack into JS to get some logs in Julia

* stringify doesnt work on any object

* try catch!?

* did this time out simply because its slow?!

* bring beck fewer airports

* fix test

* add comment

---------

Co-authored-by: SimonDanisch <[email protected]>
  • Loading branch information
jkrumbiegel and SimonDanisch authored Sep 20, 2023
1 parent a957170 commit dd418b1
Show file tree
Hide file tree
Showing 26 changed files with 994 additions and 26 deletions.
2 changes: 1 addition & 1 deletion GLMakie/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Makie = "=0.19.9"
MeshIO = "0.4"
ModernGL = "1"
Observables = "0.5.1"
ShaderAbstractions = "0.3"
ShaderAbstractions = "0.4"
PrecompileTools = "1.0"
StaticArrays = "0.12, 1.0"
julia = "1"
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ PolygonOps = "0.1.1"
PrecompileTools = "1.0"
RelocatableFolders = "0.1, 0.2, 0.3, 1.0"
Setfield = "1"
ShaderAbstractions = "0.3"
ShaderAbstractions = "0.4"
Showoff = "0.3, 1.0.2"
SignedDistanceFields = "0.4"
StableHashTraits = "1"
Expand Down
52 changes: 52 additions & 0 deletions RPRMakie/examples/datashader-rpr.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using DelimitedFiles, GLMakie
GLMakie.activate!() # hide
# For saving/showing/inlining into documentation we need to disable async calculation.
Makie.set_theme!(DataShader=(; async_latest=false))
airports = Point2f.(eachrow(readdlm(assetpath("airportlocations.csv"))))
(xmin, ymin), (xmax, ymax) = extrema(xx)

xx = Rect2f(points)
all(x-> x in xx, points)


canvas = Canvas(Rect2f(points))
aggregate!(canvas, points);

m = collect(Makie.get_aggregation(canvas))

(xmin, ymin), (xmax, ymax) = map(x-> x./widths(canvas.bounds), extrema(canvas.bounds))

xw, yw = 1 ./ size(m)
maxi = maximum(mscaled)
GLMakie.activate!()
radiance = 50
lights = [EnvironmentLight(0.5, load(RPR.assetpath("studio026.exr"))),
PointLight(Vec3f(0, 0, 2), RGBf(radiance, radiance, radiance))]
mscaled = m ./ widths(canvas.bounds)[1]
recmesh = GeometryBasics.normal_mesh(Rect3f(Vec3f(-0.5), Vec3f(1)))
RPRMakie.activate!(plugin=RPR.Northstar, iterations=1, resource=RPR.RPR_CREATION_FLAGS_ENABLE_GPU1)
f, ax, pl = meshscatter(
xmin .. xmax, ymin .. ymax, mscaled;
axis=(; type=LScene, show_axis=false, scenekw=(; lights=lights)),
marker=recmesh,
color=mscaled,
colorrange=Vec2f(0.000001, maxi),
lowclip=(:blue, 0.1),
colormap=[:white, :red],
material=(; type=:Microfacet, color=:gray, roughness=0.2, ior=1.390),
markersize=Vec3f.(xw, yw, vec(mscaled))
)
ax.scene |> display
display(f; backend=GLMakie)
using RPRMakie, FileIO

RPRMakie.activate!(plugin=RPR.Tahoe, iterations=1, resource=RPR.RPR_CREATION_FLAGS_ENABLE_GPU1)
RPRMakie.replace_scene_rpr!(ax.scene)


l = lights[2]

l.position[] = Vec3f(xmin + xmax/2, ymin + ymax / 2, widths(canvas.bounds)[1])
l.radiance[] = RGBf(500, 500, 500)

pl.colorrange[] = Vec2f(0.000001, maxi)
13 changes: 12 additions & 1 deletion RPRMakie/src/meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,25 @@ function to_rpr_object(context, matsys, scene, plot::Makie.MeshScatter)
cmap = to_colormap(plot.colormap[])
crange = plot.colorrange[]
color_from_num = Makie.interpolated_getindex.((cmap,), color, (crange,))

object_id = RPR.InputLookupMaterial(matsys)
object_id.value = RPR.RPR_MATERIAL_NODE_LOOKUP_OBJECT_ID

uv = object_id * Vec3f(0, 1/n_instances, 0)

tex = RPR.Texture(matsys, collect(color_from_num'); uv = uv)

material.color = tex
elseif color isa AbstractMatrix{<:Number}
cmap = to_colormap(plot.colormap[])
crange = plot.colorrange[]
color_from_num = Makie.interpolated_getindex.((cmap,), color, (crange,))
object_id = RPR.InputLookupMaterial(matsys)
object_id.value = RPR.RPR_MATERIAL_NODE_LOOKUP_OBJECT_ID

uv = object_id * Vec3f(0, 1/n_instances, 0)

tex = RPR.Texture(matsys, color_from_num; uv=uv)

material.color = tex
elseif color isa Colorant
material.color = color
Expand Down
18 changes: 18 additions & 0 deletions ReferenceTests/src/tests/figures_and_makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,21 @@ end

fig
end

@reference_test "datashader" begin
airports = Point2f.(eachrow(readdlm(assetpath("airportlocations.csv"))))
# Dont use the full dataset, since WGLMakie seems to time out if it's too big
fewer = airports[RNG.rand(1:length(airports), 1000)]
fig, ax, ds = datashader(fewer; async=false)
Colorbar(fig[1, 2], ds; width=100)
hidedecorations!(ax)
hidespines!(ax)

normaldist = RNG.randn(Point2f, 100000)
ds1 = normaldist .+ (Point2f(-1, 0),)
ds2 = normaldist .+ (Point2f(1, 0),)
ax, pl = datashader(fig[2, :], Dict("a" => ds1, "b" => ds2); async=false)
hidedecorations!(ax)
axislegend(ax)
fig
end
2 changes: 1 addition & 1 deletion WGLMakie/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ JSServe = "2.2"
Makie = "=0.19.9"
Observables = "0.5.1"
RelocatableFolders = "0.1, 0.2, 0.3, 1.0"
ShaderAbstractions = "0.3"
ShaderAbstractions = "0.4"
PrecompileTools = "1.0"
StaticArrays = "0.12, 1.0"
PNGFiles = "0.3, 0.4"
Expand Down
17 changes: 14 additions & 3 deletions WGLMakie/src/Serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,31 @@ function create_texture(data) {
}

function re_create_texture(old_texture, buffer, size) {
let tex;
if (size.length == 3) {
const tex = new THREE.DataTexture3D(buffer, size[0], size[1], size[2]);
tex = new THREE.DataTexture3D(buffer, size[0], size[1], size[2]);
tex.format = old_texture.format;
tex.type = old_texture.type;
return tex;
} else {
return new THREE.DataTexture(
tex = new THREE.DataTexture(
buffer,
size[0],
size[1] ? size[1] : 1,
old_texture.format,
old_texture.type
);
}
tex.minFilter = old_texture.minFilter
tex.magFilter = old_texture.magFilter
tex.anisotropy = old_texture.anisotropy
tex.wrapS = old_texture.wrapS
if (size.length > 1) {
tex.wrapT = old_texture.wrapT
}
if (size.length > 2) {
tex.wrapR = old_texture.wrapR
}
return tex
}
function BufferAttribute(buffer) {
const jsbuff = new THREE.BufferAttribute(buffer.flat, buffer.type_length);
Expand Down
2 changes: 0 additions & 2 deletions WGLMakie/src/imagelike.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ function create_shader(mscene::Scene, plot::Surface)
normals = Buffer(lift(surface_normals, px, py, pz))
per_vertex = Dict(:positions => positions, :faces => faces, :uv => uv, :normals => normals)

plot_attributes = copy(plot.attributes)

uniforms = Dict(:uniform_color => color, :color => false)
return draw_mesh(mscene, per_vertex, plot, uniforms)
end
Expand Down
1 change: 0 additions & 1 deletion WGLMakie/src/meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ end

function draw_mesh(mscene::Scene, per_vertex, plot, uniforms; permute_tex=true)
filter!(kv -> !(kv[2] isa Function), uniforms)
color = plot.calculated_colors
handle_color!(plot, uniforms, per_vertex; permute_tex=permute_tex)

get!(uniforms, :pattern, false)
Expand Down
6 changes: 3 additions & 3 deletions WGLMakie/src/serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function register_geometry_updates(update_buffer::Observable, named_buffers)
if buffer isa Buffer
on(ShaderAbstractions.updater(buffer).update) do (f, args)
# update to replace the whole buffer!
if f === (setindex!) && args[1] isa AbstractArray && args[2] isa Colon
if f === ShaderAbstractions.update!
new_array = args[1]
flat = flatten_buffer(new_array)
update_buffer[] = [name, serialize_three(flat), length(new_array)]
Expand All @@ -222,7 +222,7 @@ function uniform_updater(uniforms::Dict)
for (name, value) in uniforms
if value isa Sampler
on(ShaderAbstractions.updater(value).update) do (f, args)
if f == setindex! && args[2] isa Colon
if f === ShaderAbstractions.update!
updater[] = [name, [Int32[size(value.data)...], serialize_three(args[1])]]
end
return
Expand Down Expand Up @@ -250,7 +250,7 @@ reinterpret_faces(faces::AbstractVector) = collect(reinterpret(UInt32, decompose
function reinterpret_faces(faces::Buffer)
result = Observable(reinterpret_faces(ShaderAbstractions.data(faces)))
on(ShaderAbstractions.updater(faces).update) do (f, args)
if f === (setindex!) && args[1] isa AbstractArray && args[2] isa Colon
if f === ShaderAbstractions.update!
result[] = reinterpret_faces(args[1])
end
end
Expand Down
10 changes: 8 additions & 2 deletions WGLMakie/src/three_plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ function three_display(session::Session, scene::Scene; screen_config...)
ta = JSServe.Retain(TEXTURE_ATLAS)
evaljs(session, js"""
$(WGL).then(WGL => {
WGL.create_scene($wrapper, $canvas, $canvas_width, $scene_serialized, $comm, $width, $height, $(ta), $(config.framerate), $(config.resize_to_body))
$(done_init).notify(true)
try {
WGL.create_scene($wrapper, $canvas, $canvas_width, $scene_serialized, $comm, $width, $height, $(ta), $(config.framerate), $(config.resize_to_body))
$(done_init).notify(true)
} catch (e) {
JSServe.Connection.send_error("error initializing scene", e)
$(done_init).notify(false)
return
}
})
""")
on(session, done_init) do val
Expand Down
17 changes: 14 additions & 3 deletions WGLMakie/src/wglmakie.bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -20004,14 +20004,25 @@ function create_texture(data) {
}
}
function re_create_texture(old_texture, buffer, size) {
let tex;
if (size.length == 3) {
const tex = new mod.DataTexture3D(buffer, size[0], size[1], size[2]);
tex = new mod.DataTexture3D(buffer, size[0], size[1], size[2]);
tex.format = old_texture.format;
tex.type = old_texture.type;
return tex;
} else {
return new mod.DataTexture(buffer, size[0], size[1] ? size[1] : 1, old_texture.format, old_texture.type);
tex = new mod.DataTexture(buffer, size[0], size[1] ? size[1] : 1, old_texture.format, old_texture.type);
}
tex.minFilter = old_texture.minFilter;
tex.magFilter = old_texture.magFilter;
tex.anisotropy = old_texture.anisotropy;
tex.wrapS = old_texture.wrapS;
if (size.length > 1) {
tex.wrapT = old_texture.wrapT;
}
if (size.length > 2) {
tex.wrapR = old_texture.wrapR;
}
return tex;
}
function BufferAttribute(buffer) {
const jsbuff = new mod.BufferAttribute(buffer.flat, buffer.type_length);
Expand Down
2 changes: 2 additions & 0 deletions WGLMakie/src/wglmakie.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ export function register_popup(popup, scene, plots_to_pick, callback) {
});
}



window.WGL = {
deserialize_scene,
threejs_module,
Expand Down
Binary file added docs/_assets/datashader-14million.mp4
Binary file not shown.
Binary file added docs/_assets/datashader_2-7_billion.mp4
Binary file not shown.
Binary file added docs/_assets/nyc-per-vendor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions docs/explanations/backends/rprmakie.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ end
~~~

~~~
<video autoplay controls src="/assets/opengl_interop.mp4">
<video mute autoplay controls src="/assets/opengl_interop.mp4">
</video>
~~~

Expand Down Expand Up @@ -429,7 +429,7 @@ end
~~~

~~~
<video autoplay controls src="/assets/lego_walk.mp4">
<video mute autoplay controls src="/assets/lego_walk.mp4">
</video>
~~~

Expand Down
Loading

0 comments on commit dd418b1

Please sign in to comment.