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)
p := t.parts
w, h := t.width, t.height
width2p := int(graphics.NextPowerOf2Int32(int32(w)))
height2p := int(graphics.NextPowerOf2Int32(int32(h)))
width2p := graphics.NextPowerOf2Int(w)
height2p := graphics.NextPowerOf2Int(h)
n := 0
for i := 0; i < l; i++ {
dx0, dy0, dx1, dy1 := p.Dst(i)

View File

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

View File

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

View File

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