-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathencoder.go
More file actions
233 lines (207 loc) · 6.23 KB
/
encoder.go
File metadata and controls
233 lines (207 loc) · 6.23 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
package wgpu
import (
"fmt"
"github.com/gogpu/wgpu/core"
"github.com/gogpu/wgpu/hal"
)
// CommandEncoder records GPU commands for later submission.
//
// A command encoder is single-use. After calling Finish(), the encoder
// cannot be used again. Call Device.CreateCommandEncoder() to create a new one.
//
// NOT thread-safe - do not use from multiple goroutines.
type CommandEncoder struct {
core *core.CoreCommandEncoder
device *Device
released bool
}
// setError records a deferred error on the underlying command encoder.
// This implements the WebGPU deferred error pattern: encoding-phase errors
// are collected and surfaced when Finish() is called.
func (e *CommandEncoder) setError(err error) {
if e.core != nil {
e.core.SetError(err)
}
}
// BeginRenderPass begins a render pass.
// The returned RenderPassEncoder records draw commands.
// Call RenderPassEncoder.End() when done.
func (e *CommandEncoder) BeginRenderPass(desc *RenderPassDescriptor) (*RenderPassEncoder, error) {
if e.released {
return nil, ErrReleased
}
coreDesc := convertRenderPassDesc(desc)
corePass, err := e.core.BeginRenderPass(coreDesc)
if err != nil {
return nil, err
}
return &RenderPassEncoder{core: corePass, encoder: e}, nil
}
// BeginComputePass begins a compute pass.
// The returned ComputePassEncoder records dispatch commands.
// Call ComputePassEncoder.End() when done.
func (e *CommandEncoder) BeginComputePass(desc *ComputePassDescriptor) (*ComputePassEncoder, error) {
if e.released {
return nil, ErrReleased
}
var coreDesc *core.CoreComputePassDescriptor
if desc != nil {
coreDesc = &core.CoreComputePassDescriptor{Label: desc.Label}
}
corePass, err := e.core.BeginComputePass(coreDesc)
if err != nil {
return nil, err
}
return &ComputePassEncoder{core: corePass, encoder: e}, nil
}
// CopyBufferToBuffer copies data between buffers.
func (e *CommandEncoder) CopyBufferToBuffer(src *Buffer, srcOffset uint64, dst *Buffer, dstOffset uint64, size uint64) {
if e.released {
return
}
if src == nil {
e.setError(fmt.Errorf("wgpu: CommandEncoder.CopyBufferToBuffer: source buffer is nil"))
return
}
if dst == nil {
e.setError(fmt.Errorf("wgpu: CommandEncoder.CopyBufferToBuffer: destination buffer is nil"))
return
}
raw := e.core.RawEncoder()
if raw == nil {
return
}
halSrc := src.halBuffer()
halDst := dst.halBuffer()
if halSrc == nil || halDst == nil {
return
}
raw.CopyBufferToBuffer(halSrc, halDst, []hal.BufferCopy{
{SrcOffset: srcOffset, DstOffset: dstOffset, Size: size},
})
}
// CopyTextureToBuffer copies data from a texture to a buffer.
// This is used for GPU-to-CPU readback of rendered content.
func (e *CommandEncoder) CopyTextureToBuffer(src *Texture, dst *Buffer, regions []BufferTextureCopy) {
if e.released {
return
}
if src == nil {
e.setError(fmt.Errorf("wgpu: CommandEncoder.CopyTextureToBuffer: source texture is nil"))
return
}
if dst == nil {
e.setError(fmt.Errorf("wgpu: CommandEncoder.CopyTextureToBuffer: destination buffer is nil"))
return
}
raw := e.core.RawEncoder()
if raw == nil {
return
}
halDst := dst.halBuffer()
if src.hal == nil || halDst == nil {
return
}
halRegions := make([]hal.BufferTextureCopy, len(regions))
for i, r := range regions {
halRegions[i] = r.toHAL()
}
raw.CopyTextureToBuffer(src.hal, halDst, halRegions)
}
// TransitionTextures transitions texture states for synchronization.
// This is needed on Vulkan for layout transitions between render pass
// and copy operations (e.g., after MSAA resolve before CopyTextureToBuffer).
// On Metal, GLES, and software backends this is a no-op.
func (e *CommandEncoder) TransitionTextures(barriers []TextureBarrier) {
if e.released {
return
}
raw := e.core.RawEncoder()
if raw == nil {
return
}
halBarriers := make([]hal.TextureBarrier, len(barriers))
for i, b := range barriers {
halBarriers[i] = b.toHAL()
}
raw.TransitionTextures(halBarriers)
}
// DiscardEncoding discards the encoder without producing a command buffer.
// Use this to abandon an in-progress encoding when an error occurs.
func (e *CommandEncoder) DiscardEncoding() {
if e.released {
return
}
e.released = true
raw := e.core.RawEncoder()
if raw != nil {
raw.DiscardEncoding()
}
}
// Finish completes command recording and returns a CommandBuffer.
// After calling Finish(), the encoder cannot be used again.
func (e *CommandEncoder) Finish() (*CommandBuffer, error) {
if e.released {
return nil, ErrReleased
}
e.released = true
coreCmdBuffer, err := e.core.Finish()
if err != nil {
return nil, err
}
return &CommandBuffer{core: coreCmdBuffer, device: e.device}, nil
}
// convertRenderPassDesc converts a public descriptor to core descriptor.
func convertRenderPassDesc(desc *RenderPassDescriptor) *core.RenderPassDescriptor {
if desc == nil {
return &core.RenderPassDescriptor{}
}
coreDesc := &core.RenderPassDescriptor{
Label: desc.Label,
}
for _, ca := range desc.ColorAttachments {
coreCA := core.RenderPassColorAttachment{
LoadOp: ca.LoadOp,
StoreOp: ca.StoreOp,
ClearValue: ca.ClearValue,
}
if ca.View != nil {
coreCA.View = &core.TextureView{HAL: ca.View.hal}
}
if ca.ResolveTarget != nil {
coreCA.ResolveTarget = &core.TextureView{HAL: ca.ResolveTarget.hal}
}
coreDesc.ColorAttachments = append(coreDesc.ColorAttachments, coreCA)
}
if desc.DepthStencilAttachment != nil {
ds := desc.DepthStencilAttachment
coreDSA := &core.RenderPassDepthStencilAttachment{
DepthLoadOp: ds.DepthLoadOp,
DepthStoreOp: ds.DepthStoreOp,
DepthClearValue: ds.DepthClearValue,
DepthReadOnly: ds.DepthReadOnly,
StencilLoadOp: ds.StencilLoadOp,
StencilStoreOp: ds.StencilStoreOp,
StencilClearValue: ds.StencilClearValue,
StencilReadOnly: ds.StencilReadOnly,
}
if ds.View != nil {
coreDSA.View = &core.TextureView{HAL: ds.View.hal}
}
coreDesc.DepthStencilAttachment = coreDSA
}
return coreDesc
}
// CommandBuffer holds recorded GPU commands ready for submission.
// Created by CommandEncoder.Finish().
type CommandBuffer struct {
core *core.CoreCommandBuffer
device *Device
}
// halBuffer returns the underlying HAL command buffer.
func (cb *CommandBuffer) halBuffer() hal.CommandBuffer {
if cb.core == nil {
return nil
}
return cb.core.Raw()
}