Skip to content

Commit

Permalink
Perf: Replace nextPowerOfTwo with bit.ceil
Browse files Browse the repository at this point in the history
Same thing, but much more efficient (uses C++ numerics).
  • Loading branch information
rdw-software committed Feb 22, 2024
1 parent 7708ca3 commit acec742
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions Core/FileFormats/RagnarokGND.lua
Original file line number Diff line number Diff line change
Expand Up @@ -786,31 +786,14 @@ function RagnarokGND:ComputeFlatFaceNormalRight(gridU, gridV)
return rightFaceNormal
end

local function nextPowerOfTwo(n)
if n <= 0 then
return 1
end

if bit.band(n, (n - 1)) == 0 then
return n
end

local power = 1
while power < n do
power = power * 2
end

return power
end

function RagnarokGND:GenerateLightmapTextureImage(posterizationLevel)
posterizationLevel = posterizationLevel or RagnarokGND.LIGHTMAP_POSTERIZATION_LEVEL
console.startTimer("Generate combined lightmap texture image")

local width = 2048 -- TBD: Should use MAX_TEXTURE_DIMENSION?
local numSlicesPerRow = 2048 / 8
local numRows = math.ceil(self.lightmapFormat.numSlices / numSlicesPerRow)
local height = nextPowerOfTwo(numRows * 8)
local height = bit.ceil(numRows * 8)

printf("[RagnarokGND] Computed lightmap texture dimensions: %dx%d", width, height)
local numBytesWritten = 0
Expand Down Expand Up @@ -873,7 +856,7 @@ function RagnarokGND:ComputeLightmapTextureCoords(lightmapSliceID)
local textureWidth = 2048 -- TBD: Should use MAX_TEXTURE_DIMENSION?
local numSlicesPerRow = 2048 / 8
local numRows = math.ceil(self.lightmapFormat.numSlices / numSlicesPerRow)
local textureHeight = nextPowerOfTwo(numRows * 8)
local textureHeight = bit.ceil(numRows * 8)
local sliceSize = 8

local sliceU = lightmapSliceID % numSlicesPerRow
Expand Down

0 comments on commit acec742

Please sign in to comment.