mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 18:05:35 +00:00
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:
@@ -170,7 +170,7 @@ func newBackend(t *testing.T) backend.Device {
|
||||
if err := ctx.MakeCurrent(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := ctx.Backend()
|
||||
b, err := backend.NewDevice(ctx.API())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ type Window struct {
|
||||
}
|
||||
|
||||
type context interface {
|
||||
Backend() (backend.Device, error)
|
||||
API() gpu.API
|
||||
MakeCurrent() error
|
||||
ReleaseCurrent()
|
||||
Release()
|
||||
@@ -42,7 +42,8 @@ func NewWindow(width, height int) (*Window, error) {
|
||||
ctx: ctx,
|
||||
}
|
||||
err = contextDo(ctx, func() error {
|
||||
dev, err := ctx.Backend()
|
||||
api := ctx.API()
|
||||
dev, err := backend.NewDevice(api)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -62,7 +63,7 @@ func NewWindow(width, height int) (*Window, error) {
|
||||
fboTex.Release()
|
||||
return err
|
||||
}
|
||||
gp, err := gpu.New(dev)
|
||||
gp, err := gpu.New(api)
|
||||
if err != nil {
|
||||
fbo.Release()
|
||||
fboTex.Release()
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
package headless
|
||||
|
||||
import (
|
||||
"gioui.org/gpu/backend"
|
||||
"gioui.org/gpu/gl"
|
||||
|
||||
"gioui.org/gpu"
|
||||
_ "gioui.org/app/internal/cocoainit"
|
||||
)
|
||||
|
||||
@@ -32,6 +30,10 @@ func newGLContext() (context, error) {
|
||||
return &nsContext{ctx: ctx}, nil
|
||||
}
|
||||
|
||||
func (c *nsContext) API() gpu.API {
|
||||
return gpu.OpenGL{}
|
||||
}
|
||||
|
||||
func (c *nsContext) MakeCurrent() error {
|
||||
C.gio_headless_makeCurrentContext(c.ctx)
|
||||
if !c.prepared {
|
||||
@@ -45,10 +47,6 @@ func (c *nsContext) ReleaseCurrent() {
|
||||
C.gio_headless_clearCurrentContext(c.ctx)
|
||||
}
|
||||
|
||||
func (c *nsContext) Backend() (backend.Device, error) {
|
||||
return gl.NewBackend(nil)
|
||||
}
|
||||
|
||||
func (d *nsContext) Release() {
|
||||
if d.ctx != 0 {
|
||||
C.gio_headless_releaseContext(d.ctx)
|
||||
|
||||
@@ -6,8 +6,7 @@ import (
|
||||
"errors"
|
||||
"syscall/js"
|
||||
|
||||
"gioui.org/gpu/backend"
|
||||
"gioui.org/gpu/gl"
|
||||
"gioui.org/gpu"
|
||||
"gioui.org/internal/glimpl"
|
||||
)
|
||||
|
||||
@@ -31,8 +30,8 @@ func newGLContext() (context, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *jsContext) Backend() (backend.Device, error) {
|
||||
return gl.NewBackend(glimpl.Context(c.ctx))
|
||||
func (c *jsContext) API() gpu.API {
|
||||
return gpu.OpenGL{Context: glimpl.Context(c.ctx)}
|
||||
}
|
||||
|
||||
func (c *jsContext) Release() {
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
package headless
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"gioui.org/gpu"
|
||||
"gioui.org/app/internal/d3d11"
|
||||
"gioui.org/gpu/backend"
|
||||
)
|
||||
|
||||
type d3d11Context struct {
|
||||
@@ -19,12 +21,8 @@ func newContext() (context, error) {
|
||||
return &d3d11Context{dev: dev}, nil
|
||||
}
|
||||
|
||||
func (c *d3d11Context) Backend() (backend.Device, error) {
|
||||
backend, err := d3d11.NewBackend(c.dev.Handle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return backend, nil
|
||||
func (c *d3d11Context) API() gpu.API {
|
||||
return gpu.Direct3D11{Device: unsafe.Pointer(c.dev.Handle)}
|
||||
}
|
||||
|
||||
func (c *d3d11Context) MakeCurrent() error {
|
||||
|
||||
Reference in New Issue
Block a user