Commit Graph

2556 Commits

Author SHA1 Message Date
Benjamin Otte
334e380d31 gpu: Handle overlapping rounded rect corners
Have a fallback in place for the most egregious abuses of rounded
corners, like
  0 0 50 50 / 50 0
and the like.

Fixes obscure border colors.
2024-01-07 07:22:51 +01:00
Benjamin Otte
d8db673fb7 gpu: Add a box shadow shader
Code was inspired mainly by
  https://madebyevan.com/shaders/fast-rounded-rectangle-shadows/
and
  https://pcwalton.github.io/_posts/2015-12-21-drawing-css-box-shadows-in-webrender.html

So far the results aren't cached, that's the task of future commits.
2024-01-07 07:22:51 +01:00
Benjamin Otte
268ad54c6a gpu: Add a rounded color shader
There's multiple uses I want it for:

1. Generating the box-shadow area for blurring
2. Generating masks for rounded-rect masking
3. Optimizing the common use case of rounded-clip + color

Only the last one is implemented in this commit.
2024-01-07 07:22:50 +01:00
Benjamin Otte
64a67ac3a8 gpu: Turn globals into macros
This way, we can be more flexible in refactoring how we handle globals
(guess what we're gonna do next).
2024-01-07 07:22:50 +01:00
Benjamin Otte
74ac95dc32 gpu: Don't try to be smart
Don't try to use all those fancy GL features like glMapBuffer() and
such. Just malloc() some buffer memory and glBufferSubData() it later.

That works everywhere and is faster than (almost?) any combination of
fancy new buffer APIs. And yes I'm frustrated because I played with
those flags and none of them were better than this.

Doubles the framerate on my discrete AMD GPU.
2024-01-07 07:22:50 +01:00
Benjamin Otte
7f817fce0a gpu: Use GL_STREAM_DRAW for the push constants buffer
This seems to hit a bunch of optimizations and makes push constants
slightly faster.
2024-01-07 07:22:50 +01:00
Benjamin Otte
5549a7ab5f gpu: Merge ops on GL, too
Just like on Vulkan, try to minimize the glDrawArrays() calls by merging
adjacent ops.
2024-01-07 07:22:50 +01:00
Benjamin Otte
53821da4d6 gpu: Refactor image handling
Introduce a new GskGpuImageDescriptors object that tracks descriptors
for a set of images that can be managed by the GPU.
Then have each GskGpuShaderOp just reference the descriptors object they are
using, so that the coe can set things up properly.

To reference an image, the ops now just reference their descriptor -
which is the uint32 we've been sending to the shaders since forever.
2024-01-07 07:22:50 +01:00
Benjamin Otte
f518d780ed gpu: Add atlas support
... and use it for glyphs.
2024-01-07 07:22:50 +01:00
Benjamin Otte
9045431bde gpu: Add a GL optimization
Use glDrawArraysInstancedBaseInstance() to draw. (Yay for GL naming.)
That allows setting up the offset in the vertex array without having to
glVertexAttribPointer() everything again.

However, this is only supported since GL 4.2 and not at all in stock GLES,
so we need to have code that can work without it.
Fortunately, it is mandatory in Vulkan, so every recent GPU supports it.
And if that GPU has a proper driver, it will also expose the GL extension
for it.
(Hint: You can check https://opengles.gpuinfo.org/listextensions.php for
how many proper drivers exist outside of Mesa.)
2024-01-07 07:22:50 +01:00
Benjamin Otte
8271687ef6 gpu: Make border shader usable for inset/outset
... and use it for those when unblurred.
2024-01-07 07:22:50 +01:00
Benjamin Otte
8043d4d6e0 gpu: Add GSK_GPU_SKIP=merge
Disables merging of ops for vkCmdDraw().
2024-01-07 07:22:50 +01:00
Benjamin Otte
e7a59d92ac gpu: Add GSK_GPU_SKIP env var
The env var allows skipping various optimizations in the GPU shader.

This is useful for testing during development when trying to figure
out how to make a renderer as fast as possible.

We could also use it to enable/disable optimizations depending on GL
version or so, but I didn't think about that too much yet.
2024-01-07 07:22:50 +01:00
Benjamin Otte
e3bac4063c gpu: Copy the clear trick from the Vulkan shader
When drawing opaque color regions that are large enough, use
vkCmdClearAttachments()/glClear() instead of a shader. This speeds up
background rendering on particular on older GPUs.

See the commit messages of
  bb2cd7225e
  ce042f7ba1
  0edd7547c1
for a further discussion of performance impacts.
2024-01-07 07:22:50 +01:00
Benjamin Otte
48012a1ce4 gpu: Add a color shader
We don't want to use the pattern shader for simple colors, slow GPUs do
not like this at all.
2024-01-07 07:22:50 +01:00
Benjamin Otte
63f6e75b38 gpu: Implement blur nodes
With the work already done with shadow nodes, this one was easy.
2024-01-07 07:22:50 +01:00
Benjamin Otte
23c1ec07e7 gpu: Implement shadow nodes 2024-01-07 07:22:50 +01:00
Benjamin Otte
7431a58617 gpu: Change sorting for ops
The previous algorithm would reverse the order of subpasses, whcih leads
to unexpected behavior if dependent subpasses are not added as children
of a subpass, but just as a previous subpass - like when a subpass is
used multiple times later.

An example for this is a shadow node with multiple shadows - the source
of the shadow is used by the multiple shadows.

So ensure that adjacent subpasses stay in the same order.
2024-01-07 07:22:50 +01:00
Benjamin Otte
30e9d98f0d gpu: Add a "transparent" sampler
This is using the equivalent of EXTEND_NONE, but I wasn't sure what to
call it.

It's unused atm.
2024-01-07 07:22:50 +01:00
Benjamin Otte
43d0b0fb3c gpu: Turn off optimizing in glslc
The code generated by glslc -O is optimized worse by Mesa than
code generated unoptimized.

So generate unoptimized code until somebody figures out what's going
wrong here.
2024-01-07 07:22:50 +01:00
Benjamin Otte
d47c8613b0 gpu: Add support for mask patterns 2024-01-07 07:22:50 +01:00
Benjamin Otte
e01311a565 gpu: Add support for cross-fades 2024-01-07 07:22:50 +01:00
Benjamin Otte
0876089f8f gpu: Add repeat nodes
They're done using the pattern shader.

The pattern shader now gained a stack where vec4's can be pushed and
popped back later, which allows storing the position before computing
the new position inside the repeat node's child.
2024-01-07 07:22:50 +01:00
Benjamin Otte
b7a8c2207e gpu: Introduce gsk_texture() shader function/macro
Due to GLES and old GL not allowing non-constant texture array
lookups,we need to turn the array lookup into a big switch statementin
those versions, and that requires putting the texture() call into that
switch.

But with that trick, we can use texture IDs in GLSL.
2024-01-07 07:22:50 +01:00
Benjamin Otte
5ab8fde0bc gpu: Add colorize shader
... and use it for glyphs.

The name is a slight variation of the "coloring" name from the GL
renderer.
The functionality is exactly what the "glyph" shader from the Vulkan
renderer does.
2024-01-07 07:22:50 +01:00
Benjamin Otte
d1d1af1a62 gpu: Improve conic gradient rendering
1. Compute the fwidth() twice with offset offsets
   That way, we avoid glitches at the boundary between 0.0 and 1.0,
   because by offsetting it by 0.5, that boundary goes away.
   Then we take the min() of both which gives us the one we care about.

2. Set the gradient to repeating
   By doing that, we don't get values at the 0.0/1.0 boundary clamped,
   but things smoothly transition.
   This smoothes the line at that boundary and makes it look just like
   every other line.
2024-01-07 07:22:50 +01:00
Benjamin Otte
88618952c5 gpu: Round offscreens to pixel boundaries
Instead of strictly rounding to the given clip rectangle, increase the
rectangle to the next pixel boundary.

Also add docs that the clip_bounds do not influence the actual size of
the returned image.
2024-01-07 07:22:50 +01:00
Benjamin Otte
92d1df94fa gpu: Add GskGpuPatternWriter
It's just an object that encapsulates everything needed to create (the
data for) a pattern op.

It also clarifies which code does what, because now the NodeProcessor
and the PatternWriter are 2 different things.
2024-01-07 07:22:50 +01:00
Benjamin Otte
187db92a88 gpu: Make shader image access a vfunc
That allows shaders to handle textures differently.

In particularly, it will allow the pattern shader to take a huge amount
of textures.
2024-01-07 07:22:50 +01:00
Benjamin Otte
a9b8551e70 gpu: Add clip pattern
So now we can clip inside an opacity node without needing fallback.
2024-01-07 07:22:50 +01:00
Benjamin Otte
0cac75aff1 gpu: Passthrough subsurface nodes
We don't support subsurfaces for now, so we can just ignore the nodes.
2024-01-07 07:22:50 +01:00
Benjamin Otte
6cf3f6ebff gpu: Add support for debug nodes
Passthrough is always easy.
2024-01-07 07:22:50 +01:00
Benjamin Otte
3efe1bef93 gpu: Add a border shader
Pretty much a copy of the Vulkan border shader.

A notable change is that the input arguments are changed, because GL
gets confused if you put a mat4 at the end.
2024-01-07 07:22:50 +01:00
Benjamin Otte
16c804c5e3 gpu: Handle nested buffer writes
when doing get_node_as_image(), that may spawn a new buffer writer that
writes into the samme buffer when rendering an offscreen with patterns.

So as a more or less hacky workaround, we now abort the current buffer
write and restart it once we've created the image.
2024-01-07 07:22:50 +01:00
Benjamin Otte
28f4666366 gpu: Implement conic gradients 2024-01-07 07:22:50 +01:00
Benjamin Otte
72a5927d38 gpu: Add radial gradients 2024-01-07 07:22:50 +01:00
Benjamin Otte
7473617ffb gpu: Add linear gradients to pattern shader
This copy/pastes the gist of the Vulkan gradient renderer.
2024-01-07 07:22:50 +01:00
Benjamin Otte
7d43e4e56a gpu: Add glyphs support
This is very rudimentary, but it's a step in the direction of getting
text going.

There's lots of things missing still.
2024-01-07 07:22:50 +01:00
Benjamin Otte
8fd2a267ae gpu: Add a utility function for colors in patterns
... and use it.
2024-01-07 07:22:50 +01:00
Benjamin Otte
3809e6efb9 gpu: Add color-matrix handling to pattern shader 2024-01-07 07:22:50 +01:00
Benjamin Otte
9224efd95e gpu: Make pattern creation always succeed
If creation fails, create an offscreen image instead and draw that as a
texture.

Because offscreens basically always succeed, we can pretty much assume
success everywhere - apart from pattern creation functions that also
create images, because they can run out of shader space.
2024-01-07 07:22:50 +01:00
Benjamin Otte
4cbd384e39 gpu: Have a get_node_as_pattern() function
... and use it to replace all the individual parts of code that get the
node as a pattern.
2024-01-07 07:22:50 +01:00
Benjamin Otte
12010a597b gpu: Add a texture pattern
Needs a lot of infrastructure for handling images, but we're handling
images now.
2024-01-07 07:22:50 +01:00
Benjamin Otte
720ac700b3 gpu: Move the pattern code into the nodeprocessor
We need the nodeprocessor infrastructure to create patterns, so keeping
it in a different source file would just cause header headaches.
2024-01-07 07:22:50 +01:00
Benjamin Otte
373a6ab9f1 gpu: Add a texture cache
... and use it when drawing textures.

We'll need it in other places later, but for now that's what we have.
2024-01-07 07:22:50 +01:00
Benjamin Otte
57c1c95e75 gpu: Make frames carry a timestamp
Frames now carry a timestamp for when they are used.

This is mainly intended to attach timestamps to cached items (textures
or glyphs), but it could in theory also be used when profiling.

We use wallclock time here, not server time, because it's cheaper and
because we're more intereseted in the local machine we're rendering on.
2024-01-07 07:22:50 +01:00
Benjamin Otte
ffc117564b gpu: Make patterns do opacity nodes
Of course, for now this only works for opacity nodes that contain color
nodes, but we're still building up to ore useful stuff here.
2024-01-07 07:22:50 +01:00
Benjamin Otte
ee3367697d gpu: Move pattern code into its own file
Now we can extend the pattern creation easily - and we can add new
patterns quickly later.

Plus, we need to keep this file in sync with pattern.glsl and it's neat
when those 2 files reference only each other.
2024-01-07 07:22:50 +01:00
Benjamin Otte
c6e19f0384 gpu: Add float array to shaders and add an ubershader
... and use it for a naive color node implementation using both
so I can test it actually works.
2024-01-07 07:22:50 +01:00
Benjamin Otte
9df265acdc gpu: Clip fallback nodes to current clip
Avoids uploading parts of the node that aren't visible.
2024-01-07 07:22:49 +01:00
Benjamin Otte
461d9b4052 gpu: Set scissor rect before clearing
The render area that restricts clearing on Vulkan needs to be respected
by the GL renderer, too.
2024-01-07 07:22:49 +01:00
Benjamin Otte
0ed45c5f40 gpu: Move global syncing out
This is necessary so that fallback code can properly sync itself,
instead of just add_node().

Fixes a bunch of glitches when fallbacks would be used.
2024-01-07 07:22:49 +01:00
Benjamin Otte
73ac2d0a1c gpu: Add a flip_y argument to shader execution
Because GL flips its shit sometimes (ie when it's the framebuffer),
pass the height of the target as the flip variable, so commands
that need to operate on the pixels can flip the y axis around this value.
2024-01-07 07:22:49 +01:00
Benjamin Otte
57ab670991 gpu: Add handling for (rounded) clip node(s)
This is again mostly a copy of the Vulkan renderer.

It's a bit awkward codewise with the new invalidation framework,
because we need to cache the previous values individually now,
but it's a lot more finegrained, and we don't emit globals multiple
times when clips are nested.
2024-01-07 07:22:49 +01:00
Benjamin Otte
e2b5e0d17d gpu: Add scissor operation
Nothing is using it yet (we don't do clipping) apart from initializing
the scissor rect at startup.
2024-01-07 07:22:49 +01:00
Benjamin Otte
77e05a4240 gpu: Add support for transform nodes
This essentially copies the Vulkan renderer machinery, but adapts it to
the new handling with pending globals.
2024-01-07 07:22:49 +01:00
Benjamin Otte
97c5bb284b gpu: Document the coordinate systems we use 2024-01-07 07:22:49 +01:00
Benjamin Otte
286b473f55 gpu: Add gsk_gpu_image_get_projection_matrix()
... and use it to initialize the "proper" projection matrix to use in
shaders.

The resulting viewport will go from top left (0,0) to bottom right
(width, height) and the z clipping plane will go from -10000 to 10000.
2024-01-07 07:22:49 +01:00
Benjamin Otte
1152c93778 gpu: Handle container nodes
Now everything is slower because we upload every other node
individually!
2024-01-07 07:22:49 +01:00
Benjamin Otte
1a85d569e3 gpu: Add ability to run shaders
This heaves over an inital chunk of code from the Vulkan renderer to
execute shaders.

The only shader that exists for now is a shader that draws a single
texture.
We use that to replace the blit op we were doing before.
2024-01-07 07:22:49 +01:00
Benjamin Otte
7a1f764910 gsk: Add new renderers to the GSK_RENDERER env var
Use:
  "ngl" for the new GL renderer
  "vulkan" for the new Vulkan renderer
2024-01-07 07:22:49 +01:00
Benjamin Otte
97c60b84c8 gl: Undeprecate the NGL renderer for now
It's used for the new GPU renderer.
2024-01-07 07:22:49 +01:00
Benjamin Otte
bf89431464 gpu: Use gdk_draw_context_empty_frame() when appropriate
It's a new function, so make use of it.
2024-01-07 07:22:49 +01:00
Benjamin Otte
9ddae8aebc gpu: Add outline of new GPU renderer
For now, it just renders using cairo, uploads the result to the GPU,
blits it onto the framebuffer and then is happy.

But it can do that using Vulkan and using GL (no idea which version).

The most important thing still missing is shaders.

It also has a bunch of copy/paste from the Vulkan renderer that isn't
used yet.
But I didn't want to rip it out and then try to copy it back later
2024-01-07 07:22:49 +01:00
Benjamin Otte
1e54e838e0 vulkan: Remove GskVulkanRenderer
We want to introduce a new one next.

Technically, this breaks API, because gsk_vulkan_renderer_new() is going
away, but practically, we're gonna bring it back once we introduce that
renderer in a few commits.
2024-01-07 07:22:49 +01:00
Benjamin Otte
40854f2ae9 gsk: Add header guard to missing header 2024-01-07 07:22:49 +01:00
Matthias Clasen
bda6530fea docs: Fix: gsk->gtk links
gi-docgen can only generate links for dependencies, so we have
to manually expand to a relative url here.
2024-01-05 14:57:16 -05:00
Benjamin Otte
dde2d9b545 gl: Don't initialize texture storage in wrong format
We're calling glTexImage2D() right after with the correct format.

So add the special format "0" to avoid intializing the texture memory.
2024-01-05 07:20:32 +01:00
Matthias Clasen
ab1fba6fdc gl: When loading a texture, really create a mipmap
If we need a mipmap, and we find an existing texture that can't
mipmap, we need to recreate it.
2024-01-03 12:26:13 -05:00
Matthias Clasen
cfa53de9af gl: Check if texture has a mipmap
When reusing an exiting texture, check if it has a mipmap, in case
we need one.
2024-01-03 12:22:28 -05:00
Benjamin Otte
96a71d515b gl: Track if mipmap generation is allowed
... and if it isn't, switch to a format that does allow mipmaps.
2024-01-03 16:56:43 +01:00
Benjamin Otte
b830ca8fab gl: Pass correct format/type to function
Instead of querying the format again and potentially coming out with a
different format, use the one we queried in the calling function.
2024-01-03 16:56:31 +01:00
Matthias Clasen
c56360f20b Merge branch 'matthiasc/for-main' into 'main'
gsk: Mark some subsurface node apis as skip

See merge request GNOME/gtk!6702
2024-01-03 14:09:38 +00:00
Matthias Clasen
726d5b25e8 Merge branch 'bilelmoussaoui/add-missing-annotations' into 'main'
gi: Add missing Since annotations

See merge request GNOME/gtk!6701
2024-01-03 13:35:44 +00:00
Matthias Clasen
681aac7317 gsk: Mark some subsurface node apis as skip
These are not usable outside of GTK, so lets not burden bindings
with them.

I'll keep the get_child() function exposed, since it is needed to
iterate over node trees containing subsurface nodes.
2024-01-03 07:53:27 -05:00
Bilal Elmoussaoui
2cd550cdbc gi: Add missing Since annotations 2024-01-03 08:49:39 +01:00
Benjamin Otte
63615c05bc rendernode: Allocate right amount of memory
asan randomly failed when this almost correct code wasn't quite correct.

Hopefully this is the correct incantation to compute the size.

Related: glib#205
2024-01-03 05:01:08 +01:00
Benjamin Otte
cdf5d08294 rendernode: Fix a memleak 2024-01-03 04:58:08 +01:00
Benjamin Otte
7742434dde rendernode: Allow drawing oversized textures
Create surfaces for tiles of the image and then combine those tiles.

Use an offscreen and OPERATOR_ADD to avoid seams.
2024-01-03 04:11:35 +01:00
Benjamin Otte
09882701c2 rendernode: Fix various places where clip was double applied
This is mostly untested and a result of reading the code.

The main effect here happens when a node was drawn that didn't start on
an integer boundary, which is very rare.
However, with specially crafted tests and when using fractional scaling,
this can happen.

This happened most often when clipping by the node bounds to restrict a
push_group() call. Enlarge that rectangle to fall on a pixel boundary.
2024-01-03 04:11:35 +01:00
Benjamin Otte
a19bc6620a rendernode: Apply the same radius for begin and end
Somebody fixed the end() call but not the begin() call when the too
large blur radius was fixed.
2024-01-03 04:11:35 +01:00
Benjamin Otte
da5de1ba99 nodeparser: Fix SEGV in shadows parsing code
Testcase included

The code was writing invalid memory, so this might not have always
crashed, but I did my best to write the test so it causes a SEGV.

Also included is a fix for the testsuite where the expected result was
wrong.
2024-01-03 04:11:35 +01:00
Benjamin Otte
28b552ad8c rendernode: Remove unnecessary save/restore 2023-12-28 07:35:47 +01:00
Benjamin Otte
c7b4347342 roundedrect: Fix an intersection cornercase
These 2 rectangles used to intersect fine:
  0 0 50 50 / 50 0
  0 0 50 50 / 0 50
But the computed result was:
  0 0 50 50 / 50
which is not a valid rectangle, because the corners overlap.

Make sure such rectangles return NOT_REPRESENTABLE.

The above rectangle has been added to the testsuite.
2023-12-28 07:35:46 +01:00
Benjamin Otte
ed8bca1548 path: Make gsk_path_to_string() work in non-US locales 2023-12-28 07:35:46 +01:00
Benjamin Otte
0c3c8a6a85 stroke: Turn stroke copy intialization into a macro
That way I can use it in static initializers.
2023-12-28 07:35:46 +01:00
Matthias Clasen
ea5768cb42 Merge branch 'wip/otte/for-main' into 'main'
rendernode: Use different hilights

See merge request GNOME/gtk!6694
2023-12-27 22:15:05 +00:00
Matthias Clasen
906b88067a Merge branch 'matthiasc/for-main' into 'main'
Add a few annotations

See merge request GNOME/gtk!6695
2023-12-27 12:34:50 +00:00
Matthias Clasen
062902c003 Add a few annotations
Add a few pure, const and malloc annotations. These were suggested
by the gcc -Wsuggest-attribute option.
2023-12-26 21:57:04 -05:00
Matthias Clasen
6d57bbe7dd Fix a typo 2023-12-26 21:54:48 -05:00
Benjamin Otte
af014007f4 gl: Remove old Cairo fallback drawing code
We use gsk_render_node_draw_fallback() now which does all of that for
us.
2023-12-26 17:28:08 +01:00
Benjamin Otte
3f97ba2041 gsk: Clarify debug category messages a bit
After discussion on IRC about debug messages:

 - FALLBACK is meant to be used for printing stuff about fallbacks
   (Cairo, offscreens, conversion when uploading, etc)
 - CAIRO is for overdrawing everything drawn with Cairo
2023-12-26 17:28:08 +01:00
Benjamin Otte
4ce08a7b89 rendernode: Use different hilights
When hilighting Cairo nodes, use a different hilight color than when
hilighting other nodes.

This allows differentiating application use of Cairo (via nodes) from
renderer use of Cairo (via fallback).
2023-12-26 17:28:08 +01:00
Benjamin Otte
914e9bc316 Merge branch 'wip/otte/lots-of-tests' into 'main'
Lots of rendering tests and fixes

See merge request GNOME/gtk!6692
2023-12-26 15:22:35 +00:00
Benjamin Otte
1385ffd2c2 gsk: Repurpose GSK_DEBUG=cairo
Use it to overlay an error pattern over all Cairo drawing done by
renderers.

This has 2 purposes:
1. It allows detecting fallbacks in GPU renderers.
2. Application code can use it to detect where it is using Cairo
   drawing.

As such, it is meant to trigger both with cairo nodes as well as when
renderers fallback for regular nodes.

The old use of the debug flag - which were 2 not very useful print
statements - was removed.
2023-12-26 05:31:05 +01:00
Benjamin Otte
57a4500bae rendernode: Don't mark impure functions as pure
Functions with out arguments can never be pure, because they write to a
pointer.

But pure functions must have no side effects.
2023-12-26 05:03:24 +01:00
Benjamin Otte
b01ed4ce39 rendernode: Set better size for mask nodes
Mask nodes are transparent outside of the intersection of source and
mask, unless the mask ode is inverted alpha.

Set the bounds accordingly.

Tests have been updated accordingly.
2023-12-26 05:03:24 +01:00
Benjamin Otte
6ab6109149 rendernode: Redo repeat handling
The previous approach could lead to offscreens that were too large and
cause errors.
2023-12-25 19:12:57 +01:00
Maximiliano Sandoval R.
e857f0a3af Add Since annotations to enums
Doing this in a way that is picked up by gobject-introspection
requires splitting off new enum members into separate doc
comments, which is a bit unfortunate.
2023-12-22 08:47:47 -05:00
Benjamin Otte
d2a85abf79 Merge branch 'wip/otte/dmabuf-refactoring' into 'main'
dmabuf refactoring

See merge request GNOME/gtk!6678
2023-12-20 10:25:29 +00:00