From 847ba9de0c32b2e074e2b48dc14100f731a2b799 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 3 Dec 2017 03:51:42 +0900 Subject: [PATCH] graphics: Refactoring: Move adjustment logic for glClearColor to opengl package --- internal/graphics/command.go | 22 ---------------------- internal/math/math.go | 1 + internal/opengl/context.go | 28 ++++++++++++++++++++++++++++ internal/opengl/context_desktop.go | 5 ++++- internal/opengl/context_js.go | 5 ++++- internal/opengl/context_mobile.go | 5 ++++- 6 files changed, 41 insertions(+), 25 deletions(-) diff --git a/internal/graphics/command.go b/internal/graphics/command.go index 0740d9f19..8a9b1b22c 100644 --- a/internal/graphics/command.go +++ b/internal/graphics/command.go @@ -205,28 +205,6 @@ func (c *fillCommand) Exec(indexOffsetInBytes int) error { g := float32(cg) / max b := float32(cb) / max a := float32(ca) / max - - // On some machines like MacBook Pro, exact 0 and exact 1 might cause problems - // at glClear() (#452). - if r == 0 { - r = math.Nextafter32(r, 1) - } - if g == 0 { - g = math.Nextafter32(g, 1) - } - if b == 0 { - b = math.Nextafter32(b, 1) - } - if r == 1 { - r = math.Nextafter32(r, 0) - } - if g == 1 { - g = math.Nextafter32(g, 0) - } - if b == 1 { - b = math.Nextafter32(b, 0) - } - if err := opengl.GetContext().FillFramebuffer(r, g, b, a); err != nil { return err } diff --git a/internal/math/math.go b/internal/math/math.go index 3b9a0bb50..c0353ff4b 100644 --- a/internal/math/math.go +++ b/internal/math/math.go @@ -14,6 +14,7 @@ package math +// NextPowerOf2Int returns a nearest power of 2 to x. func NextPowerOf2Int(x int) int { if x <= 0 { panic("x must be positive") diff --git a/internal/opengl/context.go b/internal/opengl/context.go index 5478f4a2b..92d21be0c 100644 --- a/internal/opengl/context.go +++ b/internal/opengl/context.go @@ -14,6 +14,34 @@ package opengl +import ( + "math" +) + +var ( + zeroPlus float32 + oneMinus float32 +) + +func init() { + zeroPlus = math.Nextafter32(0, 1) + oneMinus = math.Nextafter32(1, 0) +} + +// adjustForClearColor adjust the value x for glClearColor function. +// +// On some machines like MacBook Pro, exact 0 and exact 1 might cause problems +// at glClear() (#452). +func adjustForClearColor(x float32) float32 { + if x <= 0 { + return zeroPlus + } + if x >= 1 { + return oneMinus + } + return x +} + var ( Nearest Filter Linear Filter diff --git a/internal/opengl/context_desktop.go b/internal/opengl/context_desktop.go index 2eabe9080..47674a03b 100644 --- a/internal/opengl/context_desktop.go +++ b/internal/opengl/context_desktop.go @@ -283,7 +283,10 @@ func (c *Context) setViewportImpl(width, height int) { func (c *Context) FillFramebuffer(r, g, b, a float32) error { return c.runOnContextThread(func() error { - gl.ClearColor(r, g, b, a) + gl.ClearColor(adjustForClearColor(r), + adjustForClearColor(g), + adjustForClearColor(b), + adjustForClearColor(a)) gl.Clear(gl.COLOR_BUFFER_BIT) return nil }) diff --git a/internal/opengl/context_js.go b/internal/opengl/context_js.go index 59f8c125d..f42f6107b 100644 --- a/internal/opengl/context_js.go +++ b/internal/opengl/context_js.go @@ -255,7 +255,10 @@ func (c *Context) setViewportImpl(width, height int) { func (c *Context) FillFramebuffer(r, g, b, a float32) error { // TODO: Use f? gl := c.gl - gl.ClearColor(r, g, b, a) + gl.ClearColor(adjustForClearColor(r), + adjustForClearColor(g), + adjustForClearColor(b), + adjustForClearColor(a)) gl.Clear(gl.COLOR_BUFFER_BIT) return nil } diff --git a/internal/opengl/context_mobile.go b/internal/opengl/context_mobile.go index 72ff8c15b..43c13a735 100644 --- a/internal/opengl/context_mobile.go +++ b/internal/opengl/context_mobile.go @@ -222,7 +222,10 @@ func (c *Context) setViewportImpl(width, height int) { func (c *Context) FillFramebuffer(r, g, b, a float32) error { gl := c.gl - gl.ClearColor(r, g, b, a) + gl.ClearColor(adjustForClearColor(r), + adjustForClearColor(g), + adjustForClearColor(b), + adjustForClearColor(a)) gl.Clear(mgl.COLOR_BUFFER_BIT) return nil }