Commit Graph

263 Commits

Author SHA1 Message Date
Elias Naur 2749eb91f3 gpu: [compute] clip image materials to their rendered size
Before this change, transformed images would take up as much atlas
texture space to fit them. However, we can easily run out of space for
large images or images with large scaling applied.

This change limits transformed images to their rendered bounds which is
the window size in the worst case.

Updates gio#219 (fixes the chat kitchen example)

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-27 12:59:01 +02:00
Elias Naur 9e9cb17a5d gpu: [compute] clip transformed images to their bounds
A follow-up change will cause some transformed images to render outside
their allocated atlas bounds. This change uses the GPU viewport to clip
them so they won't overwrite other atlas content.

Updates gio#219

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-27 12:59:01 +02:00
Elias Naur 6818af9390 gpu: [compute] get rid of material regions
The atlas allocations contains enough information to replace regions.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-27 12:59:01 +02:00
Elias Naur 7f6e376424 gpu,gpu/internal: support variable strides in ReadPixels
It saves a roundtrip to scratch memory when the CPU fallback renderer
downloads rendered materials.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-27 12:59:01 +02:00
Elias Naur 06ffa42d16 gpu: [compute] support unlimited number of materials and images
This change extends the atlas allocation machinery to images and
materials.

Updates gio#219

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-27 12:59:01 +02:00
Elias Naur 1d7ed55c29 gpu: support rectangular atlas packers
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-27 07:48:26 +02:00
Elias Naur 8c2913c2e6 gpu: tolerate changing maximum dimensions in packer.tryAdd
This change allows atlas packer clients to start with large maximum
atlas dimensions, then shrink the maximum once the underlying atlas has
been allocated.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-27 07:48:26 +02:00
Elias Naur 589c94e64b gpu: optimize atlas packer for smallest sizes
Previously, the packer optimized for the smalles space where the image
fits. Empirically, smaller atlases can be achieved by greedily picking the
smallest total area.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-27 07:48:26 +02:00
Elias Naur b9ede6d735 gpu/internal/opengl,internal/gl: avoid glBufferSubData after glBufferData
On my Fedora Intel GPU, issuing a glBufferSubData immediately after a
glBufferData with no data may leave the buffer cleared.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-25 14:29:09 +02:00
Elias Naur 414be0a0b3 gpu: Merge GPU.Collect and GPU.Frame
There's no meaningful reason to have them separate. The intention was to
enable rendering concurrent with other processing, but that's gaining
framerate at the expense of input latency and complicating ImageOp
semantics.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-24 08:30:52 +02:00
Elias Naur 7acc031ccf gpu/internal/metal: merge buffer address and size calls
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-23 17:56:57 +02:00
Elias Naur b47653b808 gpu/internal/metal: don't double-buffer staging memory
One staging buffer is enough because BeginFrame waits for the completion
of the staging operatoins from the previous frame.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-23 17:48:59 +02:00
Elias Naur 0f81bf02c1 gpu: don't invoke GPU backend outside BeginFrame/EndFrame
The Metal backend is particularly sensitive because its staging buffers
are synchronized to BeginFrame.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-23 17:47:14 +02:00
Elias Naur 43a7030f6e gpu/internal/metal: use optimal CPU cache mode for staging buffers
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-22 17:46:28 +02:00
Elias Naur 0d009bd534 gpu/internal/d3d11: remove unused method
Invalidate is no longer part of the driver contract.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-22 09:34:17 +02:00
Elias Naur d80992fc66 go.*,gpu: don't (badly) re-implement Go module versioning
Changes to the gioui.org/shader module are generally breaking changes,
which means that a particular version of gioui.org must be build with a
particular version of gioui.org/shader. Until now, the gpu package
checked the module version against an expected version and would fail at
runtime if there's a mismatch.

This change replaces all that complexity with a simple procedural
change: bump the module major version of gioui.org/shader at each such
incompatible change. It doesn't matter that we'll eventually reach
gioui.org/v1234/shader; the module is internal and won't break clients.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-22 09:18:41 +02:00
Elias Naur 64ccb1c06d gpu: [compute] add missing error handling
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 16:48:50 +02:00
Elias Naur 86e474688b gpu: [compute] don't round atlas dimensions to power-of-twos
There is no clear benefit and the rounding tends to waste texture
memory.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 16:40:21 +02:00
Elias Naur 6ee8a1cb7c gpu/internal/opengl: avoid crash when uniform buffers are not supported
Found by Dan Kortschak.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 16:06:45 +02:00
Elias Naur c9970cb8e3 gpu,gpu/internal,internal/gl: replace BlitFramebuffer with CopyTexture
OpenGL ES 2.0 doesn't support glBlitFramebuffer, but does support
glCopyTexSubImage2D. Fortunately, we don't need the extra features of
glBlitFramebuffer anyway.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 11:16:28 +02:00
Elias Naur 44adf01768 go.*,gpu: update gioui.org/shader version
Pulls in a compatiblity fix for OpenGL ES 2.0 devices.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 10:04:56 +02:00
Elias Naur 3b2992c37e gpu,app/internal/wm: add Metal port
The OpenGL (ES) implementations on Apple platforms are deprecated and
don't support GPU compute programs. This change adds support for the
replacement, the Metal GPU API.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 08:31:46 +02:00
Elias Naur a7f08eedf3 gpu,gpu/internal: split shader storage usage into read and write
Metal has texture write usage flags for read and write.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 08:31:46 +02:00
Elias Naur 07fdc1a2de gpu,gpu/internal: drop implied transformation from BlitFramebuffer
The Metal (and presumably the D3D11) backend doesn't support transformed
framebuffer blits. The only caller doesn't need it either, so drop that
capability from the driver abstraction.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 08:31:46 +02:00
Elias Naur 58cc817e5f gpu,gpu/internal: move vertex buffer stride to pipeline state
Metal needs the vertex stride at pipeline creation.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 08:31:46 +02:00
Elias Naur 1af910959b gpu: fold buffer clearing into framebuffer bind
In Metal, clearing a framebuffer is most efficiently done during bind.
Modify our driver accordingly.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 08:31:46 +02:00
Elias Naur d38c78d7ac gpu,gpu/internal: move InputDesc back from gioui.org/shader module
It was moved to gioui.org/shader by mistake.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 08:31:46 +02:00
Elias Naur afaa31eca8 gpu: introduce pipeline abstraction
Modern API such as Metal and Vulkan want clients to compile expensive
state changes into pipeline objects. Change our GPU driver abstraction
to match, thereby paving the way for future drivers.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 08:31:46 +02:00
Elias Naur 13b93b27d8 gpu: update gioui.org/shader for portable materials.vert shader
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-21 08:31:46 +02:00
Elias Naur f475087296 gpu/internal/opengl: handle switch from non-sRGB output to sRGB output
The opengl example has a screenshot functionality that renders to a
non-sRGB texture, whereas window rendering is to a sRGB capable EGL
surface. The opengl driver detects the switch from an sRGB capable
output to a non-sRGB capable output, but not the switch back. This
change releases the emulation framebuffer on the switch back.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-19 10:25:05 +02:00
Elias Naur 8140e39c20 go.*,gpu: use gioui.org/cpu.Supported to determine CPU fallback support
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-17 09:44:17 +02:00
Elias Naur c7ea90c4e2 gpu,go.*: update gioui.org/shader dependency
Fixes the opengl example on macOS.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-16 18:22:32 +02:00
Elias Naur 6b537f0d4e gpu/headless: remove useless framebuffer bind
GPU.Frame now takes an explicit RenderTarget and it is no longer
necessary to bind the target.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-16 15:47:23 +02:00
Elias Naur 6c3711d95a gpu/headless: remove useless headless_gl.go abstraction
There is no longer any shared code between OpenGL backends (unlike
EGL backends).

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-16 15:47:23 +02:00
Elias Naur 8d8bc19c23 gpu/internal/opengl: remove "gpu" prefix from type names
Refactor only.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-16 15:47:23 +02:00
Elias Naur 71c5a134d7 gpu: correct GPU.Profile reference to profile.Op
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-13 08:25:22 +02:00
Elias Naur 6aee543234 all: switch to external shaders in the gioui.org/shaders module
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-08 13:47:41 +01:00
Elias Naur 18b4442393 all: remove Z buffer support
It is no longer needed by any rendering backend.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-08 13:47:37 +01:00
Elias Naur cea8e21f97 gpu: remove opaque rectangle optimization
The default renderer tracks opaque rectangle draw operations and render
them front-to-back with a z-buffer to omit overdraw. However,

- Overlapping opaque rectangles are rare in a GUI, and the most common
instance, a solid window background, is already optimized to a glClear.
- A z-buffer is memory heavy.
- We conservatively assume a 16-bit depth buffer, which limits the
  number of drawing operations to 64k (#127).
- Depth buffer support makes GPU ports more complex, especially for
  upcoming ports (Metal, Vulkan).
- The compute renderer doesn't use z-buffers.

This change removes the optimization; a follow-up removes GPU backend
support.

Fixes gio#127

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-08 13:46:16 +01:00
Elias Naur 2059862416 all: merge .m files with their .go counterparts
The only reason for separate files is Objective-C callbacks into Go,
or when the Go side is common, yet the Objective-C side differs from
macOS to iOS.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-08 13:46:16 +01:00
Elias Naur 7d84e419c9 gpu,gpu/headless,app/internal/wm: add explicit RenderTarget API
Both the OpenGL and the Direct3D API are stateful and gpu.GPU renders to
the render target current when Frame is called.

Modern GPU API such as Metal don't have a concept of a current render
target, and the target even changes each frame.

Add RenderTarget and add an explicit target argument to GPU.Frame as
well as the underlying driver.Device.BeginFrame.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-08 13:45:23 +01:00
Elias Naur 8cec7e04eb gpu,gpu/shaders: [compute] decode sRGB texels in shader when EXT_sRGB is missing
This change avoids the hard dependency on GPU support for sRGB encoded
textures in the compute renderer.

With this change and the previously added CPU fallback, Gio no longer
rely on any GPU functionality outside the OpenGL ES 2.0 level.

Fixes gio#49
Fixes gio#154
Fixes gio#97
Fixes gio#36
Fixes gio#172

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-29 09:05:55 +02:00
Elias Naur d8f8740574 gpu/internal/opengl: use the linear colorspace when EXT_sRGB is missing
The SRGBFBO emulates a framebuffer in the sRGB colorspace. However, some
low-end devices may not have EXT_sRGB support to store framebuffer content in
sRGB.

This change handles missing EXT_sRGB support by falling back to the linear RGB colorspace.
Falling back loses color precision but is better than failing.

Updates gio#49
Updates gio#154
Updates gio#97
Updates gio#36
Updates gio#172

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-29 08:37:59 +02:00
Elias Naur 68fa64dfd5 gpu/internal/opengl: detect sRGB triple in terms of srgbaTripleFor
Refactor in preparation for relaxing sRGB format requirements.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-29 08:37:59 +02:00
Elias Naur 970fadf852 gpu/internal/driver: introduce and use FeatureSRGB
No functional changes; a follow-up will implement graceful fallback in
the compute renderer.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-29 08:37:59 +02:00
Elias Naur b3a8c24334 gpu/internal/driver: rename TextureFormatSRGB to TextureFormatSRGBA
The format implies an alpha channel; name it accordingly.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-29 08:37:18 +02:00
Elias Naur ea38195e2e gpu: [compute] add CPU fallback
This change adds a CPU fallback for devices that don't support the old
renderer nor have GPU support for compute programs.

Most of the hard work is implemented in the gioui.org/cpu module. It
uses the SwiftShader project with light modification to output
statically compiled CPU .o files for each compute program.

The CPU fallback only covers Linux and Android on arm, arm64, amd64
architectures. There is no fundamental reason support can't be extended
to other platforms:

- macOS and iOS are probably easy, but it's likely that virtually every
  device has GPU support for compute shaders.
- Windows needs a Cgo-less port, or a build constraint to require a C
  compiler (Gio core doesn't).
- FreeBSD and OpenBSD are probably also easy to do because they're so
  similar to Linux.
- The 386 binaries didn't work properly in my tests, so fixes to
  SwiftShader is probably needed. However, I expect virtually every
  Intel device can run amd64 binaries.

Updates gio#49
Fixes gio#228

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-27 14:56:50 +02:00
Elias Naur f14c151883 gpu,gpu/internal: generate hashes of compute programs
The CPU fallback for the compute renderer is contained in a separate
module for space reasons, but the CPU binaries must exactly match the
compute programs. However, there is no way to express that constraint
in go.mod.

This change generates hashes of every compute program so that a
following change can verify the CPU binaries match the programs.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-27 14:34:18 +02:00
Elias Naur 5197f637a7 gpu: [compute] compute and store clipping path hashes during construction
The hash of the clipping paths that affect drawing operations are computed
and used to quickly determine that two operations are not equal, the
most likely outcome of a comparison.

However, for paths that are constructed once and cached computing the
hash at every frame is wasteful. This is especially true for text, which
is both cached and also among the largest paths in a frame.

This change moves the hashing to op/clip.Path construction time, and
stores the hash in the ops list so it won't be re-computed at every use.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-27 14:34:18 +02:00
Elias Naur 88fb798cca gpu: [compute] speed up path comparisons with op keys
To re-use previously cached layers, the compute renderer must know
whether two drawing operations are equal. In the case two operations are
not equal, a fast hash comparison will most likely fail. In the case two
equal operations with complicated clipping paths, the comparison of the
path data is expensive.

This change adds support for fast ops.Key comparisons, where two paths
are equal if their ops.Key are. This is an optimization that kicks in
for text rendering, where glyph clipping shapes are re-used across
frames.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-27 14:34:18 +02:00