-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbuffer_rust.go
More file actions
130 lines (112 loc) · 2.64 KB
/
Copy pathbuffer_rust.go
File metadata and controls
130 lines (112 loc) · 2.64 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
//go:build rust
package wgpu
import (
"context"
"fmt"
rwgpu "github.com/go-webgpu/webgpu/wgpu"
"github.com/gogpu/gputypes"
)
// Buffer represents a GPU buffer.
// On Rust backend, this wraps go-webgpu/webgpu Buffer.
type Buffer struct {
r *rwgpu.Buffer
device *Device
released bool
}
// Size returns the buffer size in bytes.
func (b *Buffer) Size() uint64 {
if b.r == nil {
return 0
}
return b.r.Size()
}
// Usage returns the buffer's usage flags.
func (b *Buffer) Usage() gputypes.BufferUsage {
if b.r == nil {
return 0
}
return b.r.Usage()
}
// Label returns the buffer's debug label.
func (b *Buffer) Label() string {
return ""
}
// Release destroys the buffer.
func (b *Buffer) Release() {
if b.released {
return
}
b.released = true
if b.r != nil {
b.r.Release()
}
}
// MapState returns the current mapping state of the buffer.
func (b *Buffer) MapState() MapState {
if b == nil || b.released || b.r == nil {
return MapStateUnmapped
}
rState := b.r.MapState()
switch rState {
case rwgpu.BufferMapStateMapped:
return MapStateMapped
case rwgpu.BufferMapStatePending:
return MapStatePending
default:
return MapStateUnmapped
}
}
// Map blocks until a CPU-visible mapping is established for the given
// byte range, or until ctx is canceled.
func (b *Buffer) Map(ctx context.Context, mode MapMode, offset, size uint64) error {
if b == nil || b.r == nil {
return ErrReleased
}
if b.released {
return ErrBufferDestroyed
}
rMode := rwgpu.MapMode(mode)
return b.r.Map(ctx, rMode, offset, size)
}
// MapAsync initiates a buffer map without blocking the caller.
func (b *Buffer) MapAsync(mode MapMode, offset, size uint64) (*MapPending, error) {
if b == nil || b.r == nil {
return nil, ErrReleased
}
if b.released {
return nil, ErrBufferDestroyed
}
rMode := rwgpu.MapMode(mode)
rp, err := b.r.MapAsync(rMode, offset, size)
if err != nil {
return nil, fmt.Errorf("wgpu: mapAsync: %w", err)
}
return &MapPending{r: rp, buf: b}, nil
}
// MappedRange returns a safe view over the mapped region [offset, offset+size).
func (b *Buffer) MappedRange(offset, size uint64) (*MappedRange, error) {
if b == nil || b.r == nil {
return nil, ErrReleased
}
if b.released {
return nil, ErrBufferDestroyed
}
if b.MapState() != MapStateMapped {
return nil, ErrMapNotMapped
}
rm, err := b.r.MappedRange(offset, size)
if err != nil {
return nil, fmt.Errorf("wgpu: mapped range: %w", err)
}
return &MappedRange{r: rm}, nil
}
// Unmap releases the current mapping.
func (b *Buffer) Unmap() error {
if b == nil || b.r == nil {
return ErrReleased
}
if b.released {
return ErrBufferDestroyed
}
return b.r.Unmap()
}