-
-
Notifications
You must be signed in to change notification settings - Fork 761
Expand file tree
/
Copy pathstencilbuffer.go
More file actions
381 lines (340 loc) · 11 KB
/
stencilbuffer.go
File metadata and controls
381 lines (340 loc) · 11 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright 2025 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file emulates the deprecated APIs (FillRule and AntiAlias) with the non-deprecated APIs.
//
// The pseudo stencil buffer implementation is based on the following article:
// https://medium.com/@evanwallace/easy-scalable-text-rendering-on-the-gpu-c3f4d782c5ac
package ebiten
import (
"fmt"
"image"
"slices"
"sync"
)
var (
stencilBufferFillShader *Shader
stencilBufferNonZeroShader *Shader
stencilBufferEvenOddShader *Shader
)
//ebitengine:shadersource
const stencilBufferFillShaderSrc = `//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4, custom vec4) vec4 {
if frontfacing() {
return vec4(0, 1.0 / 255.0, 0, 0)
}
return vec4(1.0 / 255.0, 0, 0, 0)
}
`
//ebitengine:shadersource
const stencilBufferNonZeroShaderSrc = `//kage:unit pixels
package main
func round(x float) float {
return floor(x + 0.5)
}
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
c := imageSrc0UnsafeAt(srcPos)
w := abs(int(round(c.g*255)) - int(round(c.r*255)))
v := min(float(w), 1)
return v * imageSrc1UnsafeAt(srcPos) * color
}
`
//ebitengine:shadersource
const stencilBufferEvenOddShaderSrc = `//kage:unit pixels
package main
func round(x float) float {
return floor(x + 0.5)
}
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
c := imageSrc0UnsafeAt(srcPos)
w := abs(int(round(c.g*255)) - int(round(c.r*255)))
v := float(w % 2)
return v * imageSrc1UnsafeAt(srcPos) * color
}
`
func ensureStencilBufferShaders() (*Shader, error) {
if stencilBufferFillShader != nil {
return stencilBufferFillShader, nil
}
s, err := NewShader([]byte(stencilBufferFillShaderSrc))
if err != nil {
return nil, err
}
stencilBufferFillShader = s
return stencilBufferFillShader, err
}
func ensureStencilBufferNonZeroShader() (*Shader, error) {
if stencilBufferNonZeroShader != nil {
return stencilBufferNonZeroShader, nil
}
s, err := NewShader([]byte(stencilBufferNonZeroShaderSrc))
if err != nil {
return nil, err
}
stencilBufferNonZeroShader = s
return stencilBufferNonZeroShader, nil
}
func ensureStencilBufferEvenOddShader() (*Shader, error) {
if stencilBufferEvenOddShader != nil {
return stencilBufferEvenOddShader, nil
}
s, err := NewShader([]byte(stencilBufferEvenOddShaderSrc))
if err != nil {
return nil, err
}
stencilBufferEvenOddShader = s
return stencilBufferEvenOddShader, nil
}
var (
stencilBufferM sync.Mutex
stencilBufferImage *Image
offscreenImage1 *Image
offscreenImage2 *Image
)
func ensureStencilBufferImage(bounds image.Rectangle) *Image {
var prevBounds image.Rectangle
if stencilBufferImage != nil && !bounds.In(stencilBufferImage.Bounds()) {
prevBounds = stencilBufferImage.Bounds()
stencilBufferImage.Deallocate()
stencilBufferImage = nil
}
if stencilBufferImage == nil {
stencilBufferImage = NewImageWithOptions(bounds.Union(prevBounds), nil)
} else {
stencilBufferImage.Clear()
}
return stencilBufferImage
}
func ensureOffscreenImage1(bounds image.Rectangle) *Image {
var prevBounds image.Rectangle
if offscreenImage1 != nil && !bounds.In(offscreenImage1.Bounds()) {
prevBounds = offscreenImage1.Bounds()
offscreenImage1.Deallocate()
offscreenImage1 = nil
}
if offscreenImage1 == nil {
offscreenImage1 = NewImageWithOptions(bounds.Union(prevBounds), nil)
} else {
offscreenImage1.Clear()
}
return offscreenImage1
}
func ensureOffscreenImage2(bounds image.Rectangle) *Image {
var prevBounds image.Rectangle
if offscreenImage2 != nil && !bounds.In(offscreenImage2.Bounds()) {
prevBounds = offscreenImage2.Bounds()
offscreenImage2.Deallocate()
offscreenImage2 = nil
}
if offscreenImage2 == nil {
offscreenImage2 = NewImageWithOptions(bounds.Union(prevBounds), nil)
} else {
offscreenImage2.Clear()
}
return offscreenImage2
}
func shaderFromFillRule(fillRule FillRule) *Shader {
switch fillRule {
case FillRuleNonZero:
s, err := ensureStencilBufferNonZeroShader()
if err != nil {
panic(fmt.Sprintf("ebiten: failed to ensure stencil buffer non-zero shader: %v", err))
}
return s
case FillRuleEvenOdd:
s, err := ensureStencilBufferEvenOddShader()
if err != nil {
panic(fmt.Sprintf("ebiten: failed to ensure stencil buffer even-odd shader: %v", err))
}
return s
default:
panic("ebiten: not reached")
}
}
func drawTrianglesWithStencilBuffer(dst *Image, vertices []Vertex, indices []uint32, img *Image, options *DrawTrianglesOptions) {
if options.FillRule == FillRuleFillAll {
if !options.AntiAlias {
panic("not reached")
}
doDrawTrianglesWithAntialias(dst, vertices, indices, img, options, nil, nil)
return
}
doDrawTrianglesShaderWithStencilBuffer(dst, vertices, indices, img, options, nil, nil)
}
func drawTrianglesShaderWithStencilBuffer(dst *Image, vertices []Vertex, indices []uint32, shader *Shader, options *DrawTrianglesShaderOptions) {
if options.FillRule == FillRuleFillAll {
if !options.AntiAlias {
panic("not reached")
}
doDrawTrianglesWithAntialias(dst, vertices, indices, nil, nil, shader, options)
return
}
doDrawTrianglesShaderWithStencilBuffer(dst, vertices, indices, nil, nil, shader, options)
}
var (
tmpVerticesForStencilBuffer []Vertex
)
// doDrawTrianglesWithAntialias draw triangles with antialiasing.
//
// doDrawTrianglesWithAntialias doesn't batch draw calls, so this might not be efficient.
// This is different from Ebitengine v2.9's behavior.
// However, this function is for the legacy API and its usage is expected to be minimal.
func doDrawTrianglesWithAntialias(dst *Image, vertices []Vertex, indices []uint32, img *Image, dtOptions *DrawTrianglesOptions, shader *Shader, dtsOptions *DrawTrianglesShaderOptions) {
stencilBufferM.Lock()
defer stencilBufferM.Unlock()
bounds := dst.Bounds()
bounds.Min.X *= 2
bounds.Max.X *= 2
bounds.Min.Y *= 2
bounds.Max.Y *= 2
tmpVerticesForStencilBuffer = slices.Grow(tmpVerticesForStencilBuffer, len(vertices))
vs := tmpVerticesForStencilBuffer[:len(vertices)]
copy(vs, vertices)
for i := range vs {
vs[i].DstX *= 2
vs[i].DstY *= 2
}
var uncommonBlend bool
if dtOptions != nil {
if dtOptions.CompositeMode != CompositeModeCustom {
uncommonBlend = dtOptions.CompositeMode != CompositeModeSourceOver
} else {
uncommonBlend = dtOptions.Blend != BlendSourceOver
}
} else if dtsOptions != nil {
if dtsOptions.CompositeMode != CompositeModeCustom {
uncommonBlend = dtsOptions.CompositeMode != CompositeModeSourceOver
} else {
uncommonBlend = dtsOptions.Blend != BlendSourceOver
}
}
// Copy the current destination image for the blending, if the blend mode is not the regular alpha blending.
os1 := ensureOffscreenImage1(bounds).SubImage(bounds).(*Image)
if uncommonBlend {
op := &DrawImageOptions{}
op.GeoM.Scale(2, 2)
op.Blend = BlendCopy
os1.DrawImage(dst, op)
}
if dtOptions != nil {
op := &DrawTrianglesOptions{}
op.ColorM = dtOptions.ColorM
op.ColorScaleMode = dtOptions.ColorScaleMode
op.CompositeMode = dtOptions.CompositeMode
op.Blend = dtOptions.Blend
op.Filter = dtOptions.Filter
op.Address = dtOptions.Address
op.DisableMipmaps = dtOptions.DisableMipmaps
os1.DrawTriangles32(vs, indices, img, op)
} else if dtsOptions != nil {
op := &DrawTrianglesShaderOptions{}
op.Uniforms = dtsOptions.Uniforms
op.Images = dtsOptions.Images
op.CompositeMode = dtsOptions.CompositeMode
op.Blend = dtsOptions.Blend
os1.DrawTrianglesShader32(vs, indices, shader, op)
}
op := &DrawImageOptions{}
op.GeoM.Scale(0.5, 0.5)
op.Filter = FilterLinear
if uncommonBlend {
op.Blend = BlendCopy
}
dst.DrawImage(os1, op)
}
// doDrawTrianglesShaderWithStencilBuffer draw triangles with stencil buffer.
//
// doDrawTrianglesShaderWithStencilBuffer doesn't batch draw calls, so this might not be efficient.
// This is different from Ebitengine v2.9's behavior.
// However, this function is for the legacy API and its usage is expected to be minimal.
func doDrawTrianglesShaderWithStencilBuffer(dst *Image, vertices []Vertex, indices []uint32, img *Image, dtOptions *DrawTrianglesOptions, shader *Shader, dtsOptions *DrawTrianglesShaderOptions) {
stencilBufferM.Lock()
defer stencilBufferM.Unlock()
vs := vertices
bounds := dst.Bounds()
if dtOptions != nil && dtOptions.AntiAlias || dtsOptions != nil && dtsOptions.AntiAlias {
bounds.Min.X *= 2
bounds.Max.X *= 2
bounds.Min.Y *= 2
bounds.Max.Y *= 2
tmpVerticesForStencilBuffer = slices.Grow(tmpVerticesForStencilBuffer, len(vertices))
vs = tmpVerticesForStencilBuffer[:len(vertices)]
copy(vs, vertices)
for i := range vs {
vs[i].DstX *= 2
vs[i].DstY *= 2
}
}
// Create an offscreen image to render the vertices as they are, without blendings.
os1 := ensureOffscreenImage1(bounds).SubImage(bounds).(*Image)
if dtOptions != nil {
op := &DrawTrianglesOptions{}
op.ColorM = dtOptions.ColorM
op.ColorScaleMode = dtOptions.ColorScaleMode
op.Filter = dtOptions.Filter
op.Address = dtOptions.Address
op.DisableMipmaps = dtOptions.DisableMipmaps
os1.DrawTriangles32(vs, indices, img, op)
} else if dtsOptions != nil {
op := &DrawTrianglesShaderOptions{}
op.Uniforms = dtsOptions.Uniforms
op.Images = dtsOptions.Images
os1.DrawTrianglesShader32(vs, indices, shader, op)
}
// Create an offscreen image with the stencil buffer.
var finalOS *Image
{
// Create a stencil buffer image.
stencilBufferShader, err := ensureStencilBufferShaders()
if err != nil {
panic(err)
}
stencilImg := ensureStencilBufferImage(bounds).SubImage(bounds).(*Image)
stencilOp := &DrawTrianglesShaderOptions{}
stencilOp.Blend = BlendLighter
stencilImg.DrawTrianglesShader32(vs, indices, stencilBufferShader, stencilOp)
op := &DrawRectShaderOptions{}
op.Images[0] = stencilImg
op.Images[1] = os1
os2 := ensureOffscreenImage2(bounds).SubImage(bounds).(*Image)
var fillRule FillRule
if dtOptions != nil {
fillRule = dtOptions.FillRule
} else if dtsOptions != nil {
fillRule = dtsOptions.FillRule
}
os2.DrawRectShader(bounds.Dx(), bounds.Dy(), shaderFromFillRule(fillRule), op)
finalOS = os2
}
// Render the offscreen image onto dst.
// Note that some blends like BlendXor might not work correctly, but this is expected,
// as this logic is for the legacy API.
op := &DrawImageOptions{}
if dtOptions != nil {
op.CompositeMode = dtOptions.CompositeMode
op.Blend = dtOptions.Blend
if dtOptions.AntiAlias {
op.GeoM.Scale(0.5, 0.5)
op.Filter = FilterLinear
}
} else if dtsOptions != nil {
op.CompositeMode = dtsOptions.CompositeMode
op.Blend = dtsOptions.Blend
if dtsOptions.AntiAlias {
op.GeoM.Scale(0.5, 0.5)
op.Filter = FilterLinear
}
}
dst.DrawImage(finalOS, op)
}