forked from gogpu/gogpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_command_encoder_test.go
More file actions
247 lines (218 loc) · 7.08 KB
/
Copy pathcontext_command_encoder_test.go
File metadata and controls
247 lines (218 loc) · 7.08 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
package gogpu
import (
"errors"
"testing"
"github.com/gogpu/gputypes"
"github.com/gogpu/wgpu"
"github.com/gogpu/wgpu/hal"
"github.com/gogpu/wgpu/hal/noop"
)
type countingSubmitQueue struct {
noop.Queue
submits int
}
type commandEncoderFailDevice struct{ noop.Device }
func (d *commandEncoderFailDevice) CreateCommandEncoder(
_ *hal.CommandEncoderDescriptor,
) (hal.CommandEncoder, error) {
return nil, errors.New("injected command encoder failure")
}
func (q *countingSubmitQueue) Submit(_ []hal.CommandBuffer) (uint64, error) {
q.submits++
return uint64(q.submits), nil
}
func newSharedEncoderTestContext(t *testing.T) (*Context, *RenderTarget, *countingSubmitQueue) {
t.Helper()
return newSharedEncoderTestContextWithDevice(t, &noop.Device{})
}
func newSharedEncoderTestContextWithDevice(
t *testing.T,
halDevice hal.Device,
) (*Context, *RenderTarget, *countingSubmitQueue) {
t.Helper()
queue := &countingSubmitQueue{}
device, err := wgpu.NewDeviceFromHAL(
halDevice, queue, 0, gputypes.DefaultLimits(), "shared-encoder-test",
)
if err != nil {
t.Fatalf("NewDeviceFromHAL: %v", err)
}
renderer := &Renderer{device: device}
texture, err := device.CreateTexture(&wgpu.TextureDescriptor{
Label: "shared-encoder-test-target",
Size: wgpu.Extent3D{Width: 1, Height: 1, DepthOrArrayLayers: 1},
MipLevelCount: 1,
SampleCount: 1,
Dimension: gputypes.TextureDimension2D,
Format: gputypes.TextureFormatBGRA8Unorm,
Usage: gputypes.TextureUsageRenderAttachment,
})
if err != nil {
device.Release()
t.Fatalf("CreateTexture: %v", err)
}
view, err := device.CreateTextureView(texture, nil)
if err != nil {
texture.Release()
device.Release()
t.Fatalf("CreateTextureView: %v", err)
}
target := &RenderTarget{
renderer: renderer,
frameStarted: true,
currentView: view,
}
renderer.primary = target
t.Cleanup(func() {
target.discardFrameEncoder()
if target.currentView != nil {
target.currentView.Release()
}
texture.Release()
device.Release()
})
return newContext(renderer, 1), target, queue
}
func TestContextCommandEncoderReusesAndSubmitsFrameEncoderOnce(t *testing.T) {
ctx, target, queue := newSharedEncoderTestContext(t)
first := ctx.CommandEncoder()
if first == nil {
t.Fatal("CommandEncoder returned nil")
}
if second := ctx.CommandEncoder(); second != first {
t.Fatal("CommandEncoder did not reuse the active frame encoder")
}
if queue.submits != 0 {
t.Fatalf("encoder submitted before frame end: %d", queue.submits)
}
target.submitFrameEncoder(target.renderer)
if queue.submits != 1 {
t.Fatalf("frame submissions = %d, want 1", queue.submits)
}
if target.frameEncoder != nil {
t.Fatal("frame encoder retained after submission")
}
}
func TestContextRenderTargetCommandEncoderForwardsActiveEncoder(t *testing.T) {
ctx, _, _ := newSharedEncoderTestContext(t)
encoder := ctx.CommandEncoder()
opaque := ctx.RenderTarget().CommandEncoder()
if opaque.IsNil() {
t.Fatal("opaque CommandEncoder is nil")
}
if got := (*wgpu.CommandEncoder)(opaque.Pointer()); got != encoder {
t.Fatal("RenderTarget returned a different command encoder")
}
}
func TestContextCommandEncoderFlushesPendingClearFirst(t *testing.T) {
ctx, target, queue := newSharedEncoderTestContext(t)
target.clear(0.1, 0.2, 0.3, 1)
if encoder := ctx.CommandEncoder(); encoder == nil {
t.Fatal("CommandEncoder returned nil")
}
if target.hasPendingClear {
t.Fatal("pending clear was not recorded before returning the shared encoder")
}
if !target.frameCleared {
t.Fatal("frame was not marked cleared")
}
if queue.submits != 0 {
t.Fatalf("clear submitted before frame end: %d", queue.submits)
}
}
func TestContextCommandEncoderWithoutSurfaceReturnsNil(t *testing.T) {
ctx := newContext(&Renderer{}, 1)
if got := ctx.CommandEncoder(); got != nil {
t.Fatalf("CommandEncoder() = %v, want nil", got)
}
}
func TestContextRenderTargetCommandEncoderWithoutSurfaceReturnsNil(t *testing.T) {
ctx := newContext(&Renderer{}, 1)
if got := ctx.RenderTarget().CommandEncoder(); !got.IsNil() {
t.Fatal("opaque CommandEncoder is non-nil without an active surface")
}
}
func TestEnsureFrameEncoderWithoutFrameFails(t *testing.T) {
target := &RenderTarget{renderer: &Renderer{}}
if _, err := target.ensureFrameEncoder(); err == nil {
t.Fatal("ensureFrameEncoder succeeded without an active frame")
}
}
func TestContextCommandEncoderReturnsNilWhenCreationFails(t *testing.T) {
ctx, _, _ := newSharedEncoderTestContextWithDevice(t, &commandEncoderFailDevice{})
if got := ctx.CommandEncoder(); got != nil {
t.Fatalf("CommandEncoder() = %v after creation failure, want nil", got)
}
}
func TestFlushClearStandaloneSubmitsOnce(t *testing.T) {
_, target, queue := newSharedEncoderTestContext(t)
target.clear(0.1, 0.2, 0.3, 1)
if !target.flushClear(target.renderer.device, target.renderer) {
t.Fatal("flushClear failed")
}
if queue.submits != 1 {
t.Fatalf("standalone clear submissions = %d, want 1", queue.submits)
}
if target.hasPendingClear || !target.frameCleared {
t.Fatal("standalone clear did not update frame state")
}
}
func TestFlushClearDiscardsStandaloneEncoderOnInvalidView(t *testing.T) {
_, target, queue := newSharedEncoderTestContext(t)
target.currentView.Release()
target.clear(0.1, 0.2, 0.3, 1)
if target.flushClear(target.renderer.device, target.renderer) {
t.Fatal("flushClear succeeded with a released view")
}
if queue.submits != 0 {
t.Fatalf("invalid clear submissions = %d, want 0", queue.submits)
}
}
func TestContextCommandEncoderDiscardsSharedEncoderWhenClearFails(t *testing.T) {
ctx, target, queue := newSharedEncoderTestContext(t)
if encoder := ctx.CommandEncoder(); encoder == nil {
t.Fatal("CommandEncoder returned nil")
}
target.currentView.Release()
target.clear(0.1, 0.2, 0.3, 1)
if encoder := ctx.CommandEncoder(); encoder != nil {
t.Fatal("CommandEncoder returned encoder after clear failure")
}
if target.frameEncoder != nil {
t.Fatal("failed shared encoder was retained")
}
if queue.submits != 0 {
t.Fatalf("failed shared encoder submissions = %d, want 0", queue.submits)
}
}
func TestSubmitFrameEncoderDropsFinishFailure(t *testing.T) {
ctx, target, queue := newSharedEncoderTestContext(t)
encoder := ctx.CommandEncoder()
if encoder == nil {
t.Fatal("CommandEncoder returned nil")
}
encoder.CopyBufferToBuffer(nil, 0, nil, 0, 1)
target.submitFrameEncoder(target.renderer)
if target.frameEncoder != nil {
t.Fatal("failed frame encoder was retained")
}
if queue.submits != 0 {
t.Fatalf("invalid frame submissions = %d, want 0", queue.submits)
}
}
func TestEndFrameDiscardsEncoderAfterPixelPresent(t *testing.T) {
ctx, target, queue := newSharedEncoderTestContext(t)
if encoder := ctx.CommandEncoder(); encoder == nil {
t.Fatal("CommandEncoder returned nil")
}
target.pixelPresented = true
if target.renderer.endFrameForSurface(target) {
t.Fatal("pixel-presented frame unexpectedly reconfigured")
}
if target.frameEncoder != nil {
t.Fatal("pixel-presented frame retained its encoder")
}
if queue.submits != 0 {
t.Fatalf("pixel-presented frame submissions = %d, want 0", queue.submits)
}
}