Skip to content

Commit c7c1fdb

Browse files
Split command buffers and count buffers usage in MeshBatch.hx
1 parent f993b10 commit c7c1fdb

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

h3d/scene/GPUMeshBatch.hx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ class GPUMeshBatch extends MeshBatch {
5454
function getLodCount() return gpuLodEnabled ? getPrimitive().lodCount() : 1;
5555
override function updateHasPrimitiveOffset() meshBatchFlags.set(HasPrimitiveOffset);
5656
override function useCommandBuffer() return true;
57+
override function useCountBuffer() {
58+
#if hlsdl
59+
return h3d.impl.GlDriver.hasMultiIndirectCount;
60+
#else
61+
return true;
62+
#end
63+
}
5764

5865
override function begin( emitCountTip = -1) {
5966
if ( !gpuLodEnabled && !gpuCullingEnabled )
@@ -261,22 +268,14 @@ class GPUMeshBatch extends MeshBatch {
261268
p.countBuffers[i].uploadBytes(countBytes, 0, 1);
262269
computeShader.countBuffer = p.countBuffers[i];
263270
computeShader.startInstanceOffset = emittedCount;
264-
computeShader.ENABLE_COUNT_BUFFER = isCountBufferAllowed();
271+
computeShader.ENABLE_COUNT_BUFFER = useCountBuffer();
265272
ctx.computeList(@:privateAccess p.computePass.shaders);
266273
ctx.computeDispatch(count);
267274
emittedCount += count;
268275
}
269276
}
270277
}
271278

272-
inline function isCountBufferAllowed() {
273-
#if hlsdl
274-
return h3d.impl.GlDriver.hasMultiIndirectCount;
275-
#else
276-
return true;
277-
#end
278-
}
279-
280279
override function cleanPasses() {
281280
if ( instanced.commands != null )
282281
@:privateAccess instanced.commands.data = null;

h3d/scene/MeshBatch.hx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class MeshBatch extends MultiMaterial {
9696
function getMaxElements() return storageBufferEnabled() ? MAX_STORAGE_BUFFER_ELEMENTS : MAX_BUFFER_ELEMENTS;
9797
function hasPrimitiveOffset() return meshBatchFlags.has(HasPrimitiveOffset);
9898
function useCommandBuffer() return false;
99+
function useCountBuffer() return false;
99100

100101
public function begin( emitCountTip = -1 ) : Int {
101102
instanceCount = 0;
@@ -338,15 +339,15 @@ class MeshBatch extends MultiMaterial {
338339
var commandCountAllocated = hxd.Math.imin( hxd.Math.nextPOT( count ), p.maxInstance );
339340
if ( p.commandBuffers == null) {
340341
p.commandBuffers = [];
341-
p.countBuffers = [];
342+
if ( useCountBuffer() )
343+
p.countBuffers = [];
342344
}
343345
var buf = p.commandBuffers[index];
344-
var cbuf = p.countBuffers[index];
345346
if ( buf == null ) {
346347
buf = alloc.allocBuffer( commandCountAllocated, INDIRECT_DRAW_ARGUMENTS_FMT, UniformReadWrite );
347-
cbuf = alloc.allocBuffer( 1, hxd.BufferFormat.VEC4_DATA, UniformReadWrite );
348348
p.commandBuffers[index] = buf;
349-
p.countBuffers[index] = cbuf;
349+
if ( useCountBuffer() )
350+
p.countBuffers[index] = alloc.allocBuffer( 1, hxd.BufferFormat.VEC4_DATA, UniformReadWrite );
350351
}
351352
else if ( buf.vertices < commandCountAllocated ) {
352353
alloc.disposeBuffer( buf );
@@ -507,7 +508,8 @@ class MeshBatch extends MultiMaterial {
507508
instanced.setCommand(p.matIndex, instanced.screenRatioToLod(curScreenRatio), count);
508509
if ( useCommandBuffer() ) {
509510
@:privateAccess instanced.commands.data = p.commandBuffers[bufferIndex].vbuf;
510-
@:privateAccess instanced.commands.countBuffer = p.countBuffers[bufferIndex].vbuf;
511+
if ( useCountBuffer() )
512+
@:privateAccess instanced.commands.countBuffer = p.countBuffers[bufferIndex].vbuf;
511513
}
512514
} else
513515
instanced.commands = p.instanceBuffers[bufferIndex];
@@ -597,13 +599,15 @@ class BatchData {
597599

598600
public function clean() {
599601
var alloc = hxd.impl.Allocator.get();
600-
if ( commandBuffers != null && commandBuffers.length > 0 ) {
602+
if ( commandBuffers != null ) {
601603
for ( buf in commandBuffers )
602604
alloc.disposeBuffer(buf);
603-
commandBuffers.resize(0);
605+
commandBuffers = null;
606+
}
607+
if ( countBuffers != null ) {
604608
for ( buf in countBuffers )
605609
alloc.disposeBuffer(buf);
606-
countBuffers.resize(0);
610+
countBuffers = null;
607611
}
608612

609613
pass.removeShader(shader);

0 commit comments

Comments
 (0)