mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
8ff6546285
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>
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package backend
|
|
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
|
|
"gioui.org/internal/glimpl"
|
|
)
|
|
|
|
// See gpu/api.go for documentation for the API types
|
|
|
|
type API interface {
|
|
implementsAPI()
|
|
}
|
|
|
|
type OpenGL struct {
|
|
// Context contains the WebGL context for WebAssembly platforms. It is
|
|
// empty for all other platforms; an OpenGL context is assumed current when
|
|
// calling NewDevice.
|
|
Context glimpl.Context
|
|
}
|
|
|
|
type Direct3D11 struct {
|
|
// Device contains a *ID3D11Device.
|
|
Device unsafe.Pointer
|
|
}
|
|
|
|
// API specific device constructors.
|
|
var (
|
|
NewOpenGLDevice func(api OpenGL) (Device, error)
|
|
NewDirect3D11Device func(api Direct3D11) (Device, error)
|
|
)
|
|
|
|
// NewDevice creates a new Device given the api.
|
|
//
|
|
// Note that the device does not assume ownership of the resources contained in
|
|
// api; the caller must ensure the resources are valid until the device is
|
|
// released.
|
|
func NewDevice(api API) (Device, error) {
|
|
switch api := api.(type) {
|
|
case OpenGL:
|
|
if NewOpenGLDevice != nil {
|
|
return NewOpenGLDevice(api)
|
|
}
|
|
case Direct3D11:
|
|
if NewDirect3D11Device != nil {
|
|
return NewDirect3D11Device(api)
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("backend: no backend available for the API %T", api)
|
|
}
|
|
|
|
func (OpenGL) implementsAPI() {}
|
|
func (Direct3D11) implementsAPI() {}
|