graphics: Refactoring: Simplify NextPowerOf2Int

This commit is contained in:
Hajime Hoshi 2016-10-23 01:58:58 +09:00
parent e72ccee61b
commit 2c844ec70c
4 changed files with 18 additions and 17 deletions

View File

@ -89,8 +89,8 @@ func (t *textureQuads) vertices() []uint8 {
vertices := make([]uint8, l*size) vertices := make([]uint8, l*size)
p := t.parts p := t.parts
w, h := t.width, t.height w, h := t.width, t.height
width2p := int(graphics.NextPowerOf2Int32(int32(w))) width2p := graphics.NextPowerOf2Int(w)
height2p := int(graphics.NextPowerOf2Int32(int32(h))) height2p := graphics.NextPowerOf2Int(h)
n := 0 n := 0
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
dx0, dy0, dx1, dy1 := p.Dst(i) dx0, dy0, dx1, dy1 := p.Dst(i)

View File

@ -260,8 +260,8 @@ func adjustImageForTexture(img *image.RGBA) *image.RGBA {
adjustedImageBounds := image.Rectangle{ adjustedImageBounds := image.Rectangle{
image.ZP, image.ZP,
image.Point{ image.Point{
int(NextPowerOf2Int32(int32(width))), NextPowerOf2Int(width),
int(NextPowerOf2Int32(int32(height))), NextPowerOf2Int(height),
}, },
} }
if img.Bounds() == adjustedImageBounds { if img.Bounds() == adjustedImageBounds {
@ -309,8 +309,8 @@ type newImageCommand struct {
} }
func (c *newImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error { func (c *newImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error {
w := int(NextPowerOf2Int32(int32(c.width))) w := NextPowerOf2Int(c.width)
h := int(NextPowerOf2Int32(int32(c.height))) h := NextPowerOf2Int(c.height)
if w < 1 { if w < 1 {
return errors.New("graphics: width must be equal or more than 1.") return errors.New("graphics: width must be equal or more than 1.")
} }

View File

@ -14,12 +14,13 @@
package graphics package graphics
func NextPowerOf2Int32(x int32) int32 { func NextPowerOf2Int(x int) int {
x -= 1 if x <= 0 {
x |= (x >> 1) panic("x must be positive")
x |= (x >> 2) }
x |= (x >> 4) r := 1
x |= (x >> 8) for r < x {
x |= (x >> 16) r <<= 1
return x + 1 }
return r
} }

View File

@ -21,8 +21,8 @@ import (
func TestNextPowerOf2(t *testing.T) { func TestNextPowerOf2(t *testing.T) {
testCases := []struct { testCases := []struct {
expected int32 expected int
arg int32 arg int
}{ }{
{256, 255}, {256, 255},
{256, 256}, {256, 256},
@ -30,7 +30,7 @@ func TestNextPowerOf2(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
got := NextPowerOf2Int32(testCase.arg) got := NextPowerOf2Int(testCase.arg)
wanted := testCase.expected wanted := testCase.expected
if wanted != got { if wanted != got {
t.Errorf("Clp(%d) = %d, wanted %d", testCase.arg, got, wanted) t.Errorf("Clp(%d) = %d, wanted %d", testCase.arg, got, wanted)