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:
@@ -109,6 +109,10 @@ type SwapChain struct {
|
||||
fbo *Framebuffer
|
||||
}
|
||||
|
||||
func init() {
|
||||
backend.NewDirect3D11Device = newDirect3D11Device
|
||||
}
|
||||
|
||||
func NewDevice() (*Device, error) {
|
||||
var flags uint32
|
||||
if debug {
|
||||
@@ -237,7 +241,8 @@ func (s *SwapChain) Present() error {
|
||||
return s.swchain.Present(1, 0)
|
||||
}
|
||||
|
||||
func NewBackend(dev *_ID3D11Device) (*Backend, error) {
|
||||
func newDirect3D11Device(api backend.Direct3D11) (backend.Device, error) {
|
||||
dev := (*_ID3D11Device)(api.Device)
|
||||
b := &Backend{
|
||||
dev: dev,
|
||||
ctx: dev.GetImmediateContext(),
|
||||
|
||||
@@ -11,8 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"gioui.org/app/internal/srgb"
|
||||
"gioui.org/gpu/backend"
|
||||
"gioui.org/gpu/gl"
|
||||
"gioui.org/gpu"
|
||||
"gioui.org/internal/glimpl"
|
||||
)
|
||||
|
||||
@@ -118,8 +117,8 @@ func NewContext(disp NativeDisplayType) (*Context, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *Context) Backend() (backend.Device, error) {
|
||||
return gl.NewBackend(nil)
|
||||
func (c *Context) API() gpu.API {
|
||||
return gpu.OpenGL{}
|
||||
}
|
||||
|
||||
func (c *Context) ReleaseSurface() {
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
package window
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"gioui.org/gpu"
|
||||
"gioui.org/app/internal/d3d11"
|
||||
"gioui.org/gpu/backend"
|
||||
)
|
||||
|
||||
type d3d11Context struct {
|
||||
@@ -34,8 +36,8 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
func (c *d3d11Context) Backend() (backend.Device, error) {
|
||||
return d3d11.NewBackend(c.dev.Handle)
|
||||
func (c *d3d11Context) API() gpu.API {
|
||||
return gpu.Direct3D11{Device: unsafe.Pointer(c.dev.Handle)}
|
||||
}
|
||||
|
||||
func (c *d3d11Context) Present() error {
|
||||
|
||||
@@ -21,8 +21,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gioui.org/gpu/backend"
|
||||
"gioui.org/gpu/gl"
|
||||
"gioui.org/gpu"
|
||||
"gioui.org/internal/glimpl"
|
||||
)
|
||||
|
||||
@@ -56,8 +55,8 @@ func newContext(w *window) (*context, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *context) Backend() (backend.Device, error) {
|
||||
return gl.NewBackend(nil)
|
||||
func (c *context) API() gpu.API {
|
||||
return gpu.OpenGL{}
|
||||
}
|
||||
|
||||
func (c *context) Release() {
|
||||
|
||||
@@ -7,8 +7,7 @@ import (
|
||||
"syscall/js"
|
||||
|
||||
"gioui.org/app/internal/srgb"
|
||||
"gioui.org/gpu/backend"
|
||||
"gioui.org/gpu/gl"
|
||||
"gioui.org/gpu"
|
||||
"gioui.org/internal/glimpl"
|
||||
)
|
||||
|
||||
@@ -39,8 +38,8 @@ func newContext(w *window) (*context, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *context) Backend() (backend.Device, error) {
|
||||
return gl.NewBackend(glimpl.Context(c.ctx))
|
||||
func (c *context) API() gpu.API {
|
||||
return gpu.OpenGL{Context: glimpl.Context(c.ctx)}
|
||||
}
|
||||
|
||||
func (c *context) Release() {
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
package window
|
||||
|
||||
import (
|
||||
"gioui.org/gpu/backend"
|
||||
"gioui.org/gpu/gl"
|
||||
"gioui.org/gpu"
|
||||
"gioui.org/internal/glimpl"
|
||||
)
|
||||
|
||||
@@ -48,8 +47,8 @@ func newContext(w *window) (*context, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *context) Backend() (backend.Device, error) {
|
||||
return gl.NewBackend(nil)
|
||||
func (c *context) API() gpu.API {
|
||||
return gpu.OpenGL{}
|
||||
}
|
||||
|
||||
func (c *context) Release() {
|
||||
|
||||
@@ -7,7 +7,7 @@ package window
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gioui.org/gpu/backend"
|
||||
"gioui.org/gpu"
|
||||
"gioui.org/io/event"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/io/system"
|
||||
@@ -33,7 +33,7 @@ type Callbacks interface {
|
||||
}
|
||||
|
||||
type Context interface {
|
||||
Backend() (backend.Device, error)
|
||||
API() gpu.API
|
||||
Present() error
|
||||
MakeCurrent() error
|
||||
Release()
|
||||
|
||||
Reference in New Issue
Block a user