-
Notifications
You must be signed in to change notification settings - Fork 1
/
gm.go
54 lines (46 loc) · 1.4 KB
/
gm.go
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
package pf
import (
"sync"
)
// GlitchyMap can map a PixelFunction over every pixel (uint32 ARGB value).
// This function has data race issues and should not be
// used for anything but creating glitch effects on purpose.
func GlitchyMap(cores int, f PixelFunction, pixels []uint32) {
// Map a pixel function over every pixel, concurrently
var (
wg sync.WaitGroup
iLength = int32(len(pixels))
iStep = iLength / int32(cores)
// iConcurrentlyDone keeps track of how much work have been done by launching goroutines
iConcurrentlyDone = int32(cores) * iStep
// iDone keeps track of how much work have been done in total
iDone int32
)
// Apply partialMap for each of the partitions
if iStep < iLength {
for i := int32(0); i < iConcurrentlyDone; i += iStep {
// run a PixelFunction on parts of the pixel buffer
wg.Add(1)
go func(wg *sync.WaitGroup, f PixelFunction, pixels []uint32, iStart, iStop int32) {
for i := iStart; i < iStop; i++ {
pixels[i] = f(pixels[i])
}
wg.Done()
}(&wg, f, pixels, i, i+iStep)
}
iDone = iConcurrentlyDone
}
if iDone == iLength {
// No leftover pixels
return
}
// Apply partialMap to the final leftover pixels
wg.Add(1)
go func(wg *sync.WaitGroup, f PixelFunction, pixels []uint32, iStart, iStop int32) {
for i := iStart; i < iStop; i++ {
pixels[i] = f(pixels[i])
}
wg.Done()
}(&wg, f, pixels, iDone, iLength)
wg.Wait()
}