gpu,gpu/backend: implement generic backend.NewDevice

NewDevice creates a Device given an API, which is the necessary GPU
resources for a backend.

Convert gpu.New to take an API instead of a backend.Device directly.

In turn, this frees us to later unexport the backend package along with
the backend implementations (for now just gioui.org/gpu/gl for OpenGL).
It also allows programs that embed Gio (such as gioui.org/example/glfw)
to freely choose a backend, not just OpenGL.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-03-04 20:51:47 +01:00
parent 0e723fa192
commit 8ff6546285
19 changed files with 139 additions and 65 deletions
+11 -4
View File
@@ -28,6 +28,9 @@ import (
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
// Register backend.
_ "gioui.org/gpu/gl"
)
type GPU interface {
@@ -380,14 +383,18 @@ const (
materialTexture
)
func New(ctx backend.Device) (GPU, error) {
func New(api API) (GPU, error) {
d, err := backend.NewDevice(api)
if err != nil {
return nil, err
}
forceCompute := os.Getenv("GIORENDERER") == "forcecompute"
feats := ctx.Caps().Features
feats := d.Caps().Features
switch {
case !forceCompute && feats.Has(backend.FeatureFloatRenderTargets):
return newGPU(ctx)
return newGPU(d)
case feats.Has(backend.FeatureCompute):
return newCompute(ctx)
return newCompute(d)
default:
return nil, errors.New("gpu: no support for float render targets nor compute")
}