Skip to content

Commit

Permalink
much better shadow performance
Browse files Browse the repository at this point in the history
  • Loading branch information
tfriedel6 committed Feb 12, 2020
1 parent 8b79ad1 commit 7213b3e
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions backend/goglbackend/fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (b *GoGLBackend) Fill(style *backendbase.FillStyle, pts [][2]float64, canOv
}

if style.Blur > 0 {
b.drawBlurred(style.Blur)
b.drawBlurred(style.Blur, min, max)
}
}

Expand Down Expand Up @@ -220,18 +220,43 @@ func (b *GoGLBackend) FillImageMask(style *backendbase.FillStyle, mask *image.Al
gl.ActiveTexture(gl.TEXTURE0)

if style.Blur > 0 {
b.drawBlurred(style.Blur)
min, max := extent(pts[:])
b.drawBlurred(style.Blur, min, max)
}
}

func (b *GoGLBackend) drawBlurred(size float64) {
func (b *GoGLBackend) drawBlurred(size float64, min, max vec) {
b.offscr1.alpha = true
b.offscr2.alpha = true

// calculate box blur size
fsize := math.Max(1, math.Floor(size))
sizea := int(fsize)
sizeb := sizea
sizec := sizea
if size-fsize > 0.333333333 {
sizeb++
}
if size-fsize > 0.666666666 {
sizec++
}

min[0] -= fsize * 3
min[1] -= fsize * 3
max[0] += fsize * 3
max[1] += fsize * 3

gl.BindBuffer(gl.ARRAY_BUFFER, b.shadowBuf)
data := [16]float32{
0, 0, 0, float32(b.h), float32(b.w), float32(b.h), float32(b.w), 0,
0, 1, 0, 0, 1, 0, 1, 1}
float32(min[0]), float32(min[1]),
float32(min[0]), float32(max[1]),
float32(max[0]), float32(max[1]),
float32(max[0]), float32(min[1]),
float32(min[0] / b.fw), 1 - float32(min[1]/b.fh),
float32(min[0] / b.fw), 1 - float32(max[1]/b.fh),
float32(max[0] / b.fw), 1 - float32(max[1]/b.fh),
float32(max[0] / b.fw), 1 - float32(min[1]/b.fh),
}
gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, unsafe.Pointer(&data[0]), gl.STREAM_DRAW)

gl.UseProgram(b.shd.ID)
Expand All @@ -249,20 +274,11 @@ func (b *GoGLBackend) drawBlurred(size float64) {

gl.ActiveTexture(gl.TEXTURE0)

// calculate box blur size
fsize := math.Max(1, math.Floor(size))
sizea := int(fsize)
sizeb := sizea
sizec := sizea
if size-fsize > 0.333333333 {
sizeb++
}
if size-fsize > 0.666666666 {
sizec++
}
gl.ClearColor(0, 0, 0, 0)

gl.BindTexture(gl.TEXTURE_2D, b.offscr1.tex)
b.enableTextureRenderTarget(&b.offscr2)
gl.Clear(gl.COLOR_BUFFER_BIT)
b.box3(sizea, false)
gl.BindTexture(gl.TEXTURE_2D, b.offscr2.tex)
b.enableTextureRenderTarget(&b.offscr1)
Expand Down

0 comments on commit 7213b3e

Please sign in to comment.