-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathadapter_rust.go
More file actions
152 lines (130 loc) · 5.1 KB
/
Copy pathadapter_rust.go
File metadata and controls
152 lines (130 loc) · 5.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
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
//go:build rust
package wgpu
import (
"fmt"
"github.com/gogpu/gputypes"
rwgpu "github.com/go-webgpu/webgpu/wgpu"
)
// DeviceDescriptor configures device creation.
type DeviceDescriptor struct {
Label string
RequiredFeatures gputypes.Features
RequiredLimits gputypes.Limits
}
// Adapter represents a physical GPU.
// On Rust backend, this wraps go-webgpu/webgpu Adapter.
type Adapter struct {
r *rwgpu.Adapter
info gputypes.AdapterInfo
features gputypes.Features
limits gputypes.Limits
instance *Instance
released bool
}
// Info returns adapter metadata.
func (a *Adapter) Info() gputypes.AdapterInfo { return a.info }
// Features returns supported features.
func (a *Adapter) Features() gputypes.Features { return a.features }
// Limits returns the adapter's resource limits.
func (a *Adapter) Limits() gputypes.Limits { return a.limits }
// DownlevelCapabilities returns backend capability flags for downlevel adapters.
// Rust wgpu handles real values internally; from Go FFI we return fully compliant defaults.
func (a *Adapter) DownlevelCapabilities() gputypes.DownlevelCapabilities {
return gputypes.DefaultDownlevelCapabilities()
}
// RequestDevice creates a logical device from this adapter.
// If desc is nil, default features and limits are used.
func (a *Adapter) RequestDevice(desc *DeviceDescriptor) (*Device, error) {
if a.released {
return nil, ErrReleased
}
var rDesc *rwgpu.DeviceDescriptor
if desc != nil {
rDesc = &rwgpu.DeviceDescriptor{
Label: desc.Label,
}
// Limits conversion: if user specified limits, convert them.
if desc.RequiredLimits != (gputypes.Limits{}) {
rl := convertToRustLimits(desc.RequiredLimits)
rDesc.RequiredLimits = &rl
}
}
rd, err := a.r.RequestDevice(rDesc)
if err != nil {
return nil, fmt.Errorf("wgpu: failed to request device: %w", err)
}
deviceFeatures := convertRustFeatures(rd.Features())
deviceLimits := convertRustLimits(rd.Limits())
rq := rd.Queue()
return &Device{
r: rd,
instance: a.instance,
queue: &Queue{r: rq},
features: deviceFeatures,
limits: deviceLimits,
}, nil
}
// SurfaceCapabilities describes what a surface supports on this adapter.
type SurfaceCapabilities struct {
Formats []gputypes.TextureFormat
PresentModes []gputypes.PresentMode
AlphaModes []gputypes.CompositeAlphaMode
}
// GetSurfaceCapabilities returns the capabilities of a surface for this adapter.
func (a *Adapter) GetSurfaceCapabilities(surface *Surface) *SurfaceCapabilities {
if a.released || surface == nil || surface.r == nil {
return nil
}
caps, err := surface.r.GetCapabilities(a.r)
if err != nil {
return nil
}
return &SurfaceCapabilities{
Formats: caps.Formats,
PresentModes: caps.PresentModes,
AlphaModes: caps.AlphaModes,
}
}
// Release releases the adapter.
func (a *Adapter) Release() {
if a.released {
return
}
a.released = true
if a.r != nil {
a.r.Release()
}
}
// convertToRustLimits converts gputypes.Limits to go-webgpu Limits.
//
//nolint:dupl // Symmetric field-by-field mapping (rwgpu→gputypes vs gputypes→rwgpu), not real duplication.
func convertToRustLimits(gl gputypes.Limits) rwgpu.Limits {
return rwgpu.Limits{
MaxTextureDimension1D: gl.MaxTextureDimension1D,
MaxTextureDimension2D: gl.MaxTextureDimension2D,
MaxTextureDimension3D: gl.MaxTextureDimension3D,
MaxTextureArrayLayers: gl.MaxTextureArrayLayers,
MaxBindGroups: gl.MaxBindGroups,
MaxBindingsPerBindGroup: gl.MaxBindingsPerBindGroup,
MaxDynamicUniformBuffersPerPipelineLayout: gl.MaxDynamicUniformBuffersPerPipelineLayout,
MaxDynamicStorageBuffersPerPipelineLayout: gl.MaxDynamicStorageBuffersPerPipelineLayout,
MaxSampledTexturesPerShaderStage: gl.MaxSampledTexturesPerShaderStage,
MaxSamplersPerShaderStage: gl.MaxSamplersPerShaderStage,
MaxStorageBuffersPerShaderStage: gl.MaxStorageBuffersPerShaderStage,
MaxStorageTexturesPerShaderStage: gl.MaxStorageTexturesPerShaderStage,
MaxUniformBuffersPerShaderStage: gl.MaxUniformBuffersPerShaderStage,
MaxUniformBufferBindingSize: gl.MaxUniformBufferBindingSize,
MaxStorageBufferBindingSize: gl.MaxStorageBufferBindingSize,
MinUniformBufferOffsetAlignment: gl.MinUniformBufferOffsetAlignment,
MinStorageBufferOffsetAlignment: gl.MinStorageBufferOffsetAlignment,
MaxVertexBuffers: gl.MaxVertexBuffers,
MaxBufferSize: gl.MaxBufferSize,
MaxVertexAttributes: gl.MaxVertexAttributes,
MaxVertexBufferArrayStride: gl.MaxVertexBufferArrayStride,
MaxComputeWorkgroupSizeX: gl.MaxComputeWorkgroupSizeX,
MaxComputeWorkgroupSizeY: gl.MaxComputeWorkgroupSizeY,
MaxComputeWorkgroupSizeZ: gl.MaxComputeWorkgroupSizeZ,
MaxComputeWorkgroupsPerDimension: gl.MaxComputeWorkgroupsPerDimension,
MaxComputeInvocationsPerWorkgroup: gl.MaxComputeInvocationsPerWorkgroup,
}
}