-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
89 lines (72 loc) · 2.1 KB
/
instance.go
File metadata and controls
89 lines (72 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package webgpu
import (
"github.com/bluescreen10/webgpu-go/driver"
)
// These are options to be passed when creating an instance.
type InstanceOptions struct {
// Prefer driver to be used.
Driver string
}
// Instance is the main entry-point to interact with the GPU.
// Corresponds to https://www.w3.org/TR/webgpu/#gpu-interface.
type Instance struct {
driver driver.Driver
}
// Returns the driver name
func (i *Instance) DriverName() string {
return i.driver.Name()
}
// Creates a new surface using the configuration provide by the
// surface descriptor. This varies depending on the platform.
func (i *Instance) CreateSurface(desc SurfaceDescriptor) (*Surface, error) {
driverSurface, err := i.driver.CreateSurface(desc)
if err != nil {
return nil, err
}
return &Surface{surface: driverSurface}, nil
}
// Returns an adapter. If options are provided they
// will be used to select from multiple adapters.
func (i *Instance) RequestAdapter(opts *RequestAdapterOptions) (*Adapter, error) {
adapters := i.driver.EnumerateAdapters()
if len(adapters) == 0 {
return nil, ErrNoAdapter
}
// If options are provided they must be met
if opts != nil && len(adapters) > 1 {
var j int
// TODO: implement feature filtering
// for _, a := range adapters {
// if opts.PowerPreference == LowPower && a.IsLowPower() &&
// opts.ForceFallbackAdapter == a.Info().IsFallbackAdapter {
// adapters[j] = a
// j++
// }
// }
adapters = adapters[:j]
if len(adapters) > 0 {
return &Adapter{adapter: adapters[0]}, nil
} else {
return nil, ErrNoAdapter
}
}
return &Adapter{adapter: adapters[0]}, nil
}
// Returns an optimal GPUTextureFormat for displaying 8-bit depth.
func (i *Instance) GetPreferredCanvasFormat() TextureFormat {
return i.driver.GetPreferredCanvasFormat()
}
// Releases the instance.
func (i *Instance) Release() {
i.driver.Release()
}
// Creates a new instance. If specified, uses the
// instance descriptor to customize it.
func New(opts *InstanceOptions) *Instance {
var name string
if opts != nil {
name = opts.Driver
}
driver := Get(name)
return &Instance{driver: driver}
}