Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

major speedup from mipmaps, add benchmark #21

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion boxy.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ requires "nim >= 1.2.2"
requires "pixie >= 3.0.0"
requires "vmath >= 1.1.0"
requires "opengl >= 1.2.3"
requires "bitty >= 0.1.2"
requires "bitty >= 0.1.3"
Binary file removed mask.png
Binary file not shown.
39 changes: 26 additions & 13 deletions src/boxy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ proc `*`(a, b: Color): Color {.inline.} =
result.b = a.b * b.b
result.a = a.a * b.a

proc tileWidth(boxy: Boxy, imageInfo: ImageInfo): int {.inline.} =
proc tileWidth(boxy: Boxy, width: int): int {.inline.} =
## Number of tiles wide.
ceil(imageInfo.size.x / boxy.tileSize).int
ceil(width / boxy.tileSize).int

proc tileHeight(boxy: Boxy, imageInfo: ImageInfo): int {.inline.} =
proc tileHeight(boxy: Boxy, height: int): int {.inline.} =
## Number of tiles high.
ceil(imageInfo.size.y / boxy.tileSize).int
ceil(height / boxy.tileSize).int

proc readAtlas*(boxy: Boxy): Image =
## Read the current atlas content.
Expand Down Expand Up @@ -342,10 +342,10 @@ proc grow(boxy: Boxy) =
)

proc takeFreeTile(boxy: Boxy): int =
for index in 0 ..< boxy.maxTiles:
if not boxy.takenTiles[index]:
boxy.takenTiles[index] = true
return index
let (found, index) = boxy.takenTiles.firstFalse
if found:
boxy.takenTiles.unsafeSetTrue(index)
return index

boxy.grow()
boxy.takeFreeTile()
Expand All @@ -356,7 +356,7 @@ proc removeImage*(boxy: Boxy, key: string) =
for tileLevel in boxy.entries[key].tiles:
for tile in tileLevel:
if tile.kind == tkIndex:
boxy.takenTiles[tile.index] = false
boxy.takenTiles.unsafeSetFalse(tile.index)
boxy.entries.del(key)

proc addImage*(boxy: Boxy, key: string, image: Image, genMipmaps = true) =
Expand All @@ -375,8 +375,8 @@ proc addImage*(boxy: Boxy, key: string, image: Image, genMipmaps = true) =
imageInfo.tiles.add(@[])

# Split the image into tiles.
for y in 0 ..< boxy.tileHeight(imageInfo):
for x in 0 ..< boxy.tileWidth(imageInfo):
for y in 0 ..< boxy.tileHeight(image.height):
for x in 0 ..< boxy.tileWidth(image.width):
let tileImage = image.superImage(
x * boxy.tileSize - tileMargin div 2,
y * boxy.tileSize - tileMargin div 2,
Expand Down Expand Up @@ -666,8 +666,21 @@ proc drawImage*(
boxy.saveTransform()
boxy.scale(vec2(levelPow2, levelPow2))

for y in 0 ..< boxy.tileHeight(imageInfo):
for x in 0 ..< boxy.tileWidth(imageInfo):
var
width = imageInfo.size.x
height = imageInfo.size.y
for _ in 0 ..< level:
if width mod 2 != 0:
width = width div 2 + 1
else:
width = width div 2
if height mod 2 != 0:
height = height div 2 + 1
else:
height = height div 2

for y in 0 ..< boxy.tileHeight(width):
for x in 0 ..< boxy.tileWidth(height):
let
tile = imageInfo.tiles[level][i]
posAt = pos + vec2(x * boxy.tileSize, y * boxy.tileSize)
Expand Down
15 changes: 15 additions & 0 deletions tests/benchmark.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import benchy, boxy, opengl, windy

let window = newWindow("Windy + Boxy", ivec2(1280, 800))
makeContextCurrent(window)
loadExtensions()

let
bxy = newBoxy()
image = readImage("docs/boxyBanner.png")

timeIt "add":
bxy.addImage("boxyBanner", image)


#grow