Skip to content

Commit

Permalink
gocyclo fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeicor committed May 31, 2022
1 parent 9045274 commit 7fc57be
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
60 changes: 32 additions & 28 deletions drawsdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,7 @@ func (r *Renderer) rerenderOpt(forceCancel bool, callbacks ...func(err error)) {
renderSize := sdf.V2i{X: int(float64(r.screenSize.X) / float64(r.implState.ResInv)), Y: int(float64(r.screenSize.Y) / float64(r.implState.ResInv))}
r.implStateLock.Unlock()
partialRenders := make(chan *image.RGBA)
go func(renderSize sdf.V2i) {
partialRenderCopy := image.NewRGBA(image.Rect(0, 0, renderSize.X, renderSize.Y))
lastPartialRender := time.Now()
for partialRender := range partialRenders {
if time.Since(lastPartialRender) < r.partialRenderEvery {
continue // Skip this partial render (throttled) as it slows down significantly the full render
}
lastPartialRender = time.Now()
r.cachedRenderLock.RLock()
copy(partialRenderCopy.Pix, partialRender.Pix)
r.cachedRenderLock.RUnlock()
// WARNING: This blocks the main rendering thread: call sparingly
gpuImg, err := ebiten.NewImageFromImage(partialRenderCopy, ebiten.FilterDefault)
if err != nil {
log.Println("Error sending image to GPU:", err)
continue
}
r.cachedRenderLock.Lock()
r.cachedPartialRender = gpuImg
r.cachedRenderLock.Unlock()
}
r.cachedRenderLock.Lock() // Use the cached render as the partial one (to make sure it is complete)
err := r.cachedPartialRender.Fill(color.Transparent)
if err != nil {
log.Println("cachedPartialRender.Fill(color.Transparent) error:", err)
}
r.cachedRenderLock.Unlock()
}(renderSize)
r.goPartialRendersHandler(partialRenders, renderSize)
renderStartTime := time.Now()
r.implStateLock.RLock()
sameSize := r.cachedRenderCPU != nil && (sdf.V2i{r.cachedRenderCPU.Rect.Max.X, r.cachedRenderCPU.Rect.Max.Y} == renderSize)
Expand Down Expand Up @@ -169,3 +142,34 @@ func (r *Renderer) rerenderOpt(forceCancel bool, callbacks ...func(err error)) {
r.cachedRenderLock.Unlock()
}(callbacks...)
}

func (r *Renderer) goPartialRendersHandler(partialRenders chan *image.RGBA, renderSize sdf.V2i) {
go func(renderSize sdf.V2i) {
partialRenderCopy := image.NewRGBA(image.Rect(0, 0, renderSize.X, renderSize.Y))
lastPartialRender := time.Now()
for partialRender := range partialRenders {
if time.Since(lastPartialRender) < r.partialRenderEvery {
continue // Skip this partial render (throttled) as it slows down significantly the full render
}
lastPartialRender = time.Now()
r.cachedRenderLock.RLock()
copy(partialRenderCopy.Pix, partialRender.Pix)
r.cachedRenderLock.RUnlock()
// WARNING: This blocks the main rendering thread: call sparingly
gpuImg, err := ebiten.NewImageFromImage(partialRenderCopy, ebiten.FilterDefault)
if err != nil {
log.Println("Error sending image to GPU:", err)
continue
}
r.cachedRenderLock.Lock()
r.cachedPartialRender = gpuImg
r.cachedRenderLock.Unlock()
}
r.cachedRenderLock.Lock() // Use the cached render as the partial one (to make sure it is complete)
err := r.cachedPartialRender.Fill(color.Transparent)
if err != nil {
log.Println("cachedPartialRender.Fill(color.Transparent) error:", err)
}
r.cachedRenderLock.Unlock()
}(renderSize)
}
3 changes: 2 additions & 1 deletion examples/cylinder_head/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package main

import (
ui "github.com/Yeicor/sdfx-ui"
"github.com/deadsy/sdfx/render"
"github.com/deadsy/sdfx/sdf"
"github.com/hajimehoshi/ebiten"
"math"
Expand Down Expand Up @@ -390,7 +391,7 @@ func main() {
// Actual rendering loop
err := ui.NewRenderer(s,
ui.OptMWatchFiles([]string{"main.go"}), // Default of "." also works, but it triggers too often if generating a profile
//ui.Opt3Mesh(&render.MarchingCubesUniform{}, 100, math.Pi/3),
ui.Opt3Mesh(&render.MarchingCubesUniform{}, 100, math.Pi/3),
ui.OptMSmoothCamera(true),
).Run()
if err != nil {
Expand Down

0 comments on commit 7fc57be

Please sign in to comment.