-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinstance_native.go
More file actions
248 lines (223 loc) · 6.04 KB
/
Copy pathinstance_native.go
File metadata and controls
248 lines (223 loc) · 6.04 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//go:build !rust && !(js && wasm)
package wgpu
import (
"fmt"
"sync"
"github.com/gogpu/gputypes"
"github.com/gogpu/wgpu/core"
"github.com/gogpu/wgpu/hal"
)
// InstanceDescriptor configures instance creation.
type InstanceDescriptor struct {
Backends Backends
// Flags controls instance features like debug layers and validation.
// Use gputypes.InstanceFlagsDebug to enable GPU debug layer.
Flags gputypes.InstanceFlags
}
// Instance is the entry point for GPU operations.
//
// Instance methods are safe for concurrent use, except Release() which
// must not be called concurrently with other methods.
type Instance struct {
core *core.Instance
mu sync.Mutex
released bool
devices map[*Device]struct{}
surfaces map[*Surface]struct{}
}
// CreateInstance creates a new GPU instance.
// If desc is nil, all available backends are used.
func CreateInstance(desc *InstanceDescriptor) (*Instance, error) {
var gpuDesc *gputypes.InstanceDescriptor
if desc != nil {
d := gputypes.DefaultInstanceDescriptor()
d.Backends = desc.Backends
d.Flags = desc.Flags
gpuDesc = &d
}
coreInstance := core.NewInstance(gpuDesc)
return &Instance{core: coreInstance}, nil
}
// RequestAdapter requests a GPU adapter matching the options.
// If opts is nil, the best available adapter is returned.
//
// When opts.CompatibleSurface is set, backends that require a surface for
// adapter enumeration (GLES/OpenGL) will perform deferred enumeration using
// the surface's GL context. This follows the WebGPU spec pattern where
// requestAdapter accepts a compatible surface hint.
func (i *Instance) RequestAdapter(opts *RequestAdapterOptions) (*Adapter, error) {
if i.isReleased() {
return nil, ErrReleased
}
// Convert wgpu-level options to gputypes for core.
var coreOpts *gputypes.RequestAdapterOptions
if opts != nil {
coreOpts = &gputypes.RequestAdapterOptions{
PowerPreference: opts.PowerPreference,
ForceFallbackAdapter: opts.ForceFallbackAdapter,
}
}
// If a compatible surface is provided, use the surface-aware path
// that triggers deferred GLES adapter enumeration.
var adapterID core.AdapterID
var err error
if opts != nil && opts.CompatibleSurface != nil {
adapterID, err = i.core.RequestAdapterWithSurfaces(
coreOpts,
opts.CompatibleSurface.halSurfacesForAdapterRequest(),
)
} else {
adapterID, err = i.core.RequestAdapter(coreOpts)
}
if err != nil {
return nil, err
}
keepAdapter := false
defer func() {
if !keepAdapter {
i.core.ReleaseSurfaceAdapter(adapterID)
}
}()
info, err := core.GetAdapterInfo(adapterID)
if err != nil {
return nil, fmt.Errorf("wgpu: failed to get adapter info: %w", err)
}
features, err := core.GetAdapterFeatures(adapterID)
if err != nil {
return nil, fmt.Errorf("wgpu: failed to get adapter features: %w", err)
}
limits, err := core.GetAdapterLimits(adapterID)
if err != nil {
return nil, fmt.Errorf("wgpu: failed to get adapter limits: %w", err)
}
hal.Logger().Info("wgpu: adapter selected",
"name", info.Name,
"backend", info.Backend,
"type", info.DeviceType,
)
hub := core.GetGlobal().Hub()
coreAdapter, err := hub.GetAdapter(adapterID)
if err != nil {
return nil, fmt.Errorf("wgpu: failed to get adapter: %w", err)
}
adapter := &Adapter{
id: adapterID,
core: &coreAdapter,
info: info,
features: features,
limits: limits,
instance: i,
}
keepAdapter = true
return adapter, nil
}
func (i *Instance) isReleased() bool {
if i == nil {
return true
}
i.mu.Lock()
defer i.mu.Unlock()
return i.released
}
func (i *Instance) adoptDevice(device *Device) error {
if device == nil {
return fmt.Errorf("wgpu: cannot adopt a nil device")
}
i.mu.Lock()
defer i.mu.Unlock()
if i.released {
return ErrReleased
}
if i.devices == nil {
i.devices = make(map[*Device]struct{})
}
i.devices[device] = struct{}{}
device.instance = i
return nil
}
func (i *Instance) unregisterDevice(device *Device) {
if i == nil || device == nil {
return
}
i.mu.Lock()
delete(i.devices, device)
i.mu.Unlock()
}
func (i *Instance) adoptSurface(surface *Surface) error {
if surface == nil {
return fmt.Errorf("wgpu: cannot adopt a nil surface")
}
i.mu.Lock()
defer i.mu.Unlock()
if i.released {
return ErrReleased
}
if i.surfaces == nil {
i.surfaces = make(map[*Surface]struct{})
}
i.surfaces[surface] = struct{}{}
surface.instance = i
return nil
}
func (i *Instance) unregisterSurface(surface *Surface) {
if i == nil || surface == nil {
return
}
i.mu.Lock()
delete(i.surfaces, surface)
i.mu.Unlock()
}
func (i *Instance) surfacesForDevice(device *Device) []*Surface {
if i == nil || device == nil {
return nil
}
i.mu.Lock()
defer i.mu.Unlock()
surfaces := make([]*Surface, 0, len(i.surfaces))
// Device.Release marks the device released before reaching this scan, and
// Surface.Configure rejects a released device before assigning surface.device.
// The association is therefore stable for this teardown snapshot even though
// Surface deliberately has no second ownership lock.
for surface := range i.surfaces {
if surface != nil && surface.device == device {
surfaces = append(surfaces, surface)
}
}
return surfaces
}
func (i *Instance) beginRelease() ([]*Device, []*Surface, bool) {
if i == nil {
return nil, nil, false
}
i.mu.Lock()
defer i.mu.Unlock()
if i.released {
return nil, nil, false
}
i.released = true
devices := make([]*Device, 0, len(i.devices))
for device := range i.devices {
devices = append(devices, device)
}
surfaces := make([]*Surface, 0, len(i.surfaces))
for surface := range i.surfaces {
surfaces = append(surfaces, surface)
}
return devices, surfaces, true
}
// Release releases the instance and all associated resources.
func (i *Instance) Release() {
devices, surfaces, ok := i.beginRelease()
if !ok {
return
}
// Devices own configured swapchains, surfaces own platform surface handles,
// and the instance owns the native instance. Release in that order.
for _, device := range devices {
device.Release()
}
for _, surface := range surfaces {
surface.Release()
}
i.core.Destroy()
}