-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·380 lines (331 loc) · 9.87 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"image"
"image/color"
"log"
"math"
"os"
"runtime"
"time"
"github.com/PerformLine/go-stockutil/colorutil"
"github.com/remeh/sizedwaitgroup"
"golang.org/x/image/tiff"
)
const (
iterCap = 10000
iterMin = 100
preIters = 10
numLuma = 16
numLumaDiv = 3
)
var (
disChroma *bool
disLuma *bool
outDir *string
imgWidth *float64
imgHeight *float64
superSample *float64
startFrame *float64
endFrame *float64
offX *float64
offY *float64
zoomPow *float64
escapeVal *float64
gammaLuma *float64
gammaChroma *float64
zoomAdd *float64
zSpeedDiv *float64
colorDegPerInter *int
numThreads *int
workBlock *float64
colorBrightness *float64
colorSaturation *float64
numInterations *int
doSleep *bool
sleepMicro *int
//Sleep this long before starting a new thread
//Doesn't affect performance that much, but helps multitasking
threadSleep time.Duration = time.Microsecond
//Gamma LUT tables
paletteL [iterCap]uint32
paletteC [0xFF]uint32
//Image buffer
offscreen *image.Gray16
offscreenC *image.RGBA
//Current zoom level
curZoom float64 = 1.0
//zoom step size
zoomDiv float64
//Integer zoom is based on
zoomInt float64
//Frame count
frameCount float64 = 0
//Multithread group
wg sizedwaitgroup.SizedWaitGroup
//Divide by this to get average pixel color for supersampling
numSamples uint32
//number of times to iterate a sample
numIters uint32
)
type Game struct {
}
func main() {
DnumThreads := (runtime.NumCPU())
disChroma = flag.Bool("disChroma", false, "Do not output chroma image")
disLuma = flag.Bool("disLuma", false, "Do not output luma image")
outDir = flag.String("outDir", "out", "output directory")
imgWidth = flag.Float64("width", 3840, "Width of output image")
imgHeight = flag.Float64("height", 2160, "Height of output image")
superSample = flag.Float64("super", 8, "Super sampling x/y size")
startFrame = flag.Float64("start", 1, "Start on this frame number")
endFrame = flag.Float64("end", 3600, "Stop on this frame number")
offX = flag.Float64("offx", 0, "X offset")
offY = flag.Float64("offy", 0, "Y offset")
zoomPow = flag.Float64("zoom", 100, "Zoom power")
escapeVal = flag.Float64("escape", 4, "Escape value")
gammaLuma = flag.Float64("gammaLuma", 1.0, "Luma gamma")
gammaChroma = flag.Float64("gammaChroma", 1.0, "Chroma gamma")
zoomAdd = flag.Float64("zoomAdd", 1, "Zoom step size")
zSpeedDiv = flag.Float64("zSpeedDiv", 1.0, "Zoom speed divisor")
colorDegPerInter = flag.Int("colorDegPerInter", 1, "Color degrees per iteration")
numThreads = flag.Int("numThreads", DnumThreads, "Number of threads to use")
workBlock = flag.Float64("workBlock", 32, "Work block size x/y size")
colorBrightness = flag.Float64("colorBrightness", 0.5, "HSV brightness of the chroma")
colorSaturation = flag.Float64("colorSaturation", 0.8, "HSV saturation of the chroma")
numInterations = flag.Int("iters", 2500, "Max number of iterations")
doSleep = flag.Bool("doSleep", false, "Sleep before work blocks")
sleepMicro = flag.Int("sleepMicro", 100, "Microseconds of sleep before each workblock")
flag.Parse()
threadSleep = time.Duration(*sleepMicro)
fmt.Printf("%v,%v\n", *offX, *offY)
/* Statically allocated */
if *numInterations > iterCap {
a := iterCap
numInterations = &a
} else if *numInterations < iterMin {
a := iterMin
numInterations = &a
}
if *superSample < 1 {
a := 1.0
superSample = &a
} else if *superSample > 255 {
a := 255.0
superSample = &a
}
//zoom step size
zoomDiv = 10000.0 / *zSpeedDiv
//Integer zoom is based on
zoomInt = 9800.0 / *zSpeedDiv
//Alloc images
offscreen = image.NewGray16(image.Rect(0, 0, int(*imgWidth), int(*imgHeight)))
offscreenC = image.NewRGBA(image.Rect(0, 0, int(*imgWidth), int(*imgHeight)))
//Setup
wg = sizedwaitgroup.New(*numThreads)
numSamples = uint32(int(*superSample) * int(*superSample))
numIters = uint32(*numInterations) - preIters
//Make gamma LUTs
var i uint32
for i = 0; i < numIters; i++ {
paletteL[i] = uint32(math.Pow(float64(i)/float64(numLuma), *gammaLuma) * 0xFFFF)
}
for i := range paletteC {
paletteC[i] = uint32(math.Pow(float64(i)/float64(0xFF), *gammaChroma) * 0xFF)
}
//Zoom needs a pre-calculation
for c := 0; c < int(*startFrame); c++ {
calcZoom()
}
frameCount = *startFrame
//Render loop
for {
//Render frame
renderStart := time.Now()
rendered := updateOffscreen()
took := time.Since(renderStart).Seconds()
//Update zoom for next frame
calcZoom()
//If we have a result, write it
//(we can skip frames for resume and multi-machine rendering)
if rendered {
wg.Add()
go func() {
if !*disChroma {
fileName := fmt.Sprintf("%v/%v-%v.tif", *outDir, "chroma", frameCount)
output, err := os.Create(fileName)
opt := &tiff.Options{Compression: tiff.Deflate, Predictor: true}
if tiff.Encode(output, offscreenC, opt) != nil {
log.Println("ERROR: Failed to write image:", err)
os.Exit(1)
}
output.Close()
}
wg.Done()
}()
wg.Add()
go func() {
if !*disLuma {
fileName := fmt.Sprintf("%v/%v-%v.tif", *outDir, "luma", frameCount)
output, err := os.Create(fileName)
opt := &tiff.Options{Compression: tiff.Deflate, Predictor: true}
if tiff.Encode(output, offscreen, opt) != nil {
log.Println("ERROR: Failed to write image:", err)
os.Exit(1)
}
output.Close()
}
wg.Done()
}()
wg.Wait()
remain := *endFrame - frameCount
eta := time.Duration(took*remain) * time.Second
fmt.Printf("Completed frame: %v / %v (%v remaining) ETA: %v\n", frameCount, *endFrame, remain, eta.String())
}
if frameCount >= *endFrame {
fmt.Println("Rendering complete")
os.Exit(0)
return
}
frameCount++
}
}
func updateOffscreen() bool {
//Skip frames that already exist
//Otherwise make a empty placeholder file to reserve this frame for us
//For lazy file-share multi-machine rendering (i use sshfs)
if !*disChroma {
fileName := fmt.Sprintf("%v/%v-%v.tif", *outDir, "chroma", frameCount)
_, err := os.Stat(fileName)
if err == nil {
fmt.Println(fileName, "chroma exists... Skipping")
return false
} else {
_, err := os.Create(fileName)
if err != nil {
log.Println("ERROR: Failed to create file:", err)
return false
}
}
}
if !*disLuma {
fileName := fmt.Sprintf("%v/%v-%v.tif", *outDir, "luma", frameCount)
_, err := os.Stat(fileName)
if err == nil {
fmt.Println(fileName, "luma exists... Skipping")
return false
} else {
_, err := os.Create(fileName)
if err != nil {
log.Println("ERROR: Failed to create file:", err)
return false
}
}
}
var xBlock, yBlock float64
for xBlock = 0; xBlock <= *imgWidth / *workBlock; xBlock++ {
for yBlock = 0; yBlock <= *imgHeight / *workBlock; yBlock++ {
wg.Add()
if *doSleep {
//Give process manager a moment
time.Sleep(threadSleep * time.Microsecond)
}
go func(xBlock, yBlock float64) {
defer wg.Done()
//Create a block of pixels for the thread to work on
xStart := xBlock * *workBlock
yStart := yBlock * *workBlock
xEnd := xStart + *workBlock
yEnd := yStart + *workBlock
//Don't render off the screen
if xStart < 0 {
xStart = 0
}
if yStart < 0 {
yStart = 0
}
if xEnd > *imgWidth {
xEnd = *imgWidth
}
if yEnd > *imgHeight {
yEnd = *imgHeight
}
//Render the block
for x := xStart; x < xEnd; x++ {
for y := yStart; y < yEnd; y++ {
var pixel uint32 = 0
var r, g, b uint32
var sx, sy float64
//Supersample
for sx = 0; sx < *superSample; sx++ {
for sy = 0; sy < *superSample; sy++ {
//Get the sub-pixel position
ssx := float64(sx) / float64(*superSample)
ssy := float64(sy) / float64(*superSample)
//Translate to position on the mandelbrot
xx := ((((float64(x) + ssx) / *imgWidth) - 0.5) / curZoom) - (*offX)
yy := ((((float64(y) + ssy) / *imgWidth) - 0.3) / curZoom) - (*offY)
c := complex(xx, yy) //Rotate
z := complex(0, 0)
var it uint32 = 0
skip := false
found := false
//Pre-interate (no draw)
//Speed + asthetic choice
for i := 0; i < preIters; i++ {
z = z*z + c
if real(z)*real(z)+imag(z)*imag(z) > *escapeVal {
skip = true
break
}
}
//Don't render at all if we escaped in the pre-iteration.
if !skip {
for it = 0; it < numIters; it++ {
z = z*z + c
if real(z)*real(z)+imag(z)*imag(z) > *escapeVal {
found = true
break
}
}
}
//Don't render if we didn't escape
//This allows background and bulb to be black
if found {
//Add the value ( gamma correct ) to the total
//We later divide to get the average for super-sampling
pixel += paletteL[(it+uint32(frameCount/numLumaDiv))%numLuma]
if !*disChroma {
tr, tg, tb := colorutil.HsvToRgb(float64((it)*uint32(*colorDegPerInter)%360), *colorSaturation, *colorBrightness)
//We already gamma corrected, so use gamma 1.0 for chroma
//But still convert from 8 bits to 16, to match the luma
r += paletteC[tr]
g += paletteC[tg]
b += paletteC[tb]
}
}
}
}
if !*disLuma {
offscreen.Set(int(x), int(y), color.Gray16{uint16(pixel / numSamples)})
}
if !*disChroma {
offscreenC.Set(int(x), int(y), color.RGBA{
uint8((r / numSamples)),
uint8((g / numSamples)),
uint8((b / numSamples)), 0xFF})
}
}
}
}(xBlock, yBlock)
}
}
wg.Wait()
return true
}
func calcZoom() {
zoomInt = zoomInt + *zoomAdd
sStep := zoomInt / zoomDiv
curZoom = math.Pow(sStep, *zoomPow)
}