Skip to content

Commit 95aba2e

Browse files
committed
Batcher: use round/Pow2 sizes for more cache friendliness
1 parent dfa22ce commit 95aba2e

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

h3d/scene/Batcher.hx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,11 +925,15 @@ private class BatchPass {
925925
instancesDirty = false;
926926
var alloc = hxd.impl.Allocator.get();
927927

928+
inline function allocBuffer(size, format, flags) {
929+
return alloc.allocBuffer(hxd.impl.Allocator.roundPOT(size), format, flags);
930+
}
931+
928932
var instanceDataSize = totalInstanceCount * batchShader.paramsSize;
929933
if ( instancesData == null || instancesData.vertices < instanceDataSize ) {
930934
if ( instancesData != null )
931935
alloc.disposeBuffer(instancesData);
932-
instancesData = alloc.allocBuffer( instanceDataSize, hxd.BufferFormat.VEC4_DATA, UniformReadWrite );
936+
instancesData = allocBuffer( instanceDataSize, hxd.BufferFormat.VEC4_DATA, UniformReadWrite );
933937
}
934938
batchShader.Batch_StorageBuffer = instancesData;
935939

@@ -938,14 +942,14 @@ private class BatchPass {
938942
if ( syncIDs == null || syncIDs.vertices < totalInstanceCount ) {
939943
if ( syncIDs != null )
940944
alloc.disposeBuffer(syncIDs);
941-
syncIDs = alloc.allocBuffer( totalInstanceCount, hxd.BufferFormat.INDEX32, Uniform );
945+
syncIDs = allocBuffer( totalInstanceCount, hxd.BufferFormat.INDEX32, Uniform );
942946
}
943947
}
944948

945949
if ( instancesInfos == null || instancesInfos.vertices < totalInstanceCount ) {
946950
if ( instancesInfos != null )
947951
alloc.disposeBuffer(instancesInfos);
948-
instancesInfos = alloc.allocBuffer( totalInstanceCount, PASS_INSTANCES_INFOS_FMT, Uniform );
952+
instancesInfos = allocBuffer( totalInstanceCount, PASS_INSTANCES_INFOS_FMT, Uniform );
949953
}
950954

951955
var instanceCursor = 0;
@@ -960,7 +964,7 @@ private class BatchPass {
960964
if ( commandBuffer == null || commandBuffer.vertices < totalInstanceCount ) {
961965
if ( commandBuffer != null )
962966
alloc.disposeBuffer(commandBuffer);
963-
commandBuffer = alloc.allocBuffer( totalInstanceCount, INDIRECT_DRAW_ARGUMENTS_FMT, UniformReadWrite );
967+
commandBuffer = allocBuffer( totalInstanceCount, INDIRECT_DRAW_ARGUMENTS_FMT, UniformReadWrite );
964968
if ( command == null )
965969
command = new h3d.impl.InstanceBuffer();
966970
@:privateAccess command.data = commandBuffer.vbuf;

hxd/impl/Allocator.hx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,10 @@ class Allocator {
104104
return inst;
105105
}
106106

107+
public static function roundPOT(v: Int, limit=1024*1024) {
108+
return if(v < limit)
109+
hxd.Math.nextPOT(v);
110+
else
111+
Math.ceil(v / limit) * limit;
112+
}
107113
}

0 commit comments

Comments
 (0)