Skip to content

Commit f97fe40

Browse files
committed
Batcher : Use allocator for buffer allocation.
1 parent e9c77c7 commit f97fe40

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

h3d/scene/Batcher.hx

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -403,21 +403,25 @@ private class Batch {
403403
if ( !instancesDirty )
404404
return;
405405
instancesDirty = false;
406+
var alloc = hxd.impl.Allocator.get();
406407

407408
if ( cullingData == null || cullingData.vertices < totalInstanceCount ) {
408-
cullingData?.dispose();
409-
cullingData = new h3d.Buffer(totalInstanceCount, CULLING_DATA_FMT, [UniformBuffer]);
409+
if ( cullingData != null )
410+
alloc.disposeBuffer(cullingData);
411+
cullingData = alloc.allocBuffer(totalInstanceCount, CULLING_DATA_FMT, Uniform);
410412
}
411413

412414
if ( batchInfos == null || batchInfos.vertices < totalInstanceCount ) {
413-
batchInfos?.dispose();
414-
batchInfos = new h3d.Buffer(totalInstanceCount, BATCH_INFOS_FMT, [UniformBuffer, ReadWriteBuffer]);
415+
if ( batchInfos != null )
416+
alloc.disposeBuffer(batchInfos);
417+
batchInfos = alloc.allocBuffer(totalInstanceCount, BATCH_INFOS_FMT, UniformReadWrite);
415418
}
416419

417420
var groupsNeeded = groups.length;
418421
if ( groupsInfos == null || groupsInfos.vertices < groupsNeeded ) {
419-
groupsInfos?.dispose();
420-
groupsInfos = new h3d.Buffer(groupsNeeded, GROUP_INFOS, [UniformBuffer]);
422+
if ( groupsInfos != null )
423+
alloc.disposeBuffer(groupsInfos);
424+
groupsInfos = alloc.allocBuffer(groupsNeeded, GROUP_INFOS, Uniform);
421425
}
422426

423427
var groupsInfosBytes = haxe.io.Bytes.alloc(groupsNeeded * 4);
@@ -487,15 +491,21 @@ private class Batch {
487491
if ( totalInstanceCount != 0 )
488492
throw "Instance leak in batcher";
489493
totalInstanceCount = 0;
490-
cullingData?.dispose();
491-
cullingData = null;
492-
batchInfos?.dispose();
493-
batchInfos = null;
494+
var alloc = hxd.impl.Allocator.get();
495+
if ( cullingData != null ) {
496+
alloc.disposeBuffer(cullingData);
497+
cullingData = null;
498+
}
499+
if ( batchInfos != null ) {
500+
alloc.disposeBuffer(batchInfos);
501+
batchInfos = null;
502+
}
503+
if ( groupsInfos != null ) {
504+
alloc.disposeBuffer(groupsInfos);
505+
groupsInfos = null;
506+
}
494507
instancesDirty = false;
495-
groupsInfos?.dispose();
496-
groupsInfos = null;
497508
needLogicNormal = false;
498-
499509
}
500510
}
501511

@@ -798,17 +808,20 @@ private class BatchPass {
798808
if ( !instancesDirty )
799809
return;
800810
instancesDirty = false;
811+
var alloc = hxd.impl.Allocator.get();
801812

802813
var instanceDataSize = totalInstanceCount * batchShader.paramsSize;
803814
if ( instancesData == null || instancesData.vertices < instanceDataSize ) {
804-
instancesData?.dispose();
805-
instancesData = new h3d.Buffer( instanceDataSize, hxd.BufferFormat.VEC4_DATA, [UniformBuffer, ReadWriteBuffer] );
815+
if ( instancesData != null )
816+
alloc.disposeBuffer(instancesData);
817+
instancesData = alloc.allocBuffer( instanceDataSize, hxd.BufferFormat.VEC4_DATA, UniformReadWrite );
806818
batchShader.Batch_StorageBuffer = instancesData;
807819
}
808820

809821
if ( instancesInfos == null || instancesInfos.vertices < totalInstanceCount ) {
810-
instancesInfos?.dispose();
811-
instancesInfos = new h3d.Buffer(totalInstanceCount, PASS_INSTANCES_INFOS_FMT, [UniformBuffer]);
822+
if ( instancesInfos != null )
823+
alloc.disposeBuffer(instancesInfos);
824+
instancesInfos = alloc.allocBuffer( totalInstanceCount, PASS_INSTANCES_INFOS_FMT, Uniform );
812825
}
813826

814827
var instanceCursor = 0;
@@ -819,8 +832,9 @@ private class BatchPass {
819832
}
820833

821834
if ( commandBuffer == null || commandBuffer.vertices < totalInstanceCount ) {
822-
commandBuffer?.dispose();
823-
commandBuffer = new h3d.Buffer(totalInstanceCount, INDIRECT_DRAW_ARGUMENTS_FMT, [UniformBuffer, ReadWriteBuffer]);
835+
if ( commandBuffer != null )
836+
alloc.disposeBuffer(commandBuffer);
837+
commandBuffer = alloc.allocBuffer( totalInstanceCount, INDIRECT_DRAW_ARGUMENTS_FMT, UniformReadWrite );
824838
if ( command == null )
825839
command = new h3d.impl.InstanceBuffer();
826840
@:privateAccess command.data = commandBuffer.vbuf;
@@ -855,10 +869,22 @@ private class BatchPass {
855869
}
856870

857871
public function dispose() {
858-
instancesData?.dispose();
859-
instancesInfos?.dispose();
860-
commandBuffer?.dispose();
872+
var alloc = hxd.impl.Allocator.get();
873+
if ( instancesData != null ) {
874+
alloc.disposeBuffer(instancesData);
875+
instancesData = null;
876+
}
877+
if ( instancesInfos != null ) {
878+
alloc.disposeBuffer(instancesInfos);
879+
instancesInfos = null;
880+
}
881+
if ( commandBuffer != null ) {
882+
alloc.disposeBuffer(commandBuffer);
883+
commandBuffer = null;
884+
}
861885
countBuffer?.dispose();
886+
countBuffer = null;
887+
command = null;
862888
}
863889
}
864890

0 commit comments

Comments
 (0)