1 // SPDX-License-Identifier: Unlicense OR MIT
2 3 package driver
4 5 import (
6 "fmt"
7 "unsafe"
8 9 "github.com/p9c/p9/pkg/gel/gio/internal/gl"
10 )
11 12 // See gpu/api.go for documentation for the API types
13 14 type API interface {
15 implementsAPI()
16 }
17 18 type OpenGL struct {
19 // Context contains the WebGL context for WebAssembly platforms. It is
20 // empty for all other platforms; an OpenGL context is assumed current when
21 // calling NewDevice.
22 Context gl.Context
23 }
24 25 type Direct3D11 struct {
26 // Device contains a *ID3D11Device.
27 Device unsafe.Pointer
28 }
29 30 // API specific device constructors.
31 var (
32 NewOpenGLDevice func(api OpenGL) (Device, error)
33 NewDirect3D11Device func(api Direct3D11) (Device, error)
34 )
35 36 // NewDevice creates a new Device given the api.
37 //
38 // Note that the device does not assume ownership of the resources contained in
39 // api; the caller must ensure the resources are valid until the device is
40 // released.
41 func NewDevice(api API) (Device, error) {
42 switch api := api.(type) {
43 case OpenGL:
44 if NewOpenGLDevice != nil {
45 return NewOpenGLDevice(api)
46 }
47 case Direct3D11:
48 if NewDirect3D11Device != nil {
49 return NewDirect3D11Device(api)
50 }
51 }
52 return nil, fmt.Errorf("driver: no driver available for the API %T", api)
53 }
54 55 func (OpenGL) implementsAPI() {}
56 func (Direct3D11) implementsAPI() {}
57