Skip to content

Commit cc81d58

Browse files
MeshBatch lod selection from cpu.
1 parent 31ccc25 commit cc81d58

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

h3d/scene/MeshBatch.hx

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ enum MeshBatchFlag {
55
EnableGpuUpdate;
66
EnableStorageBuffer;
77
HasPrimitiveOffset;
8+
EnableCpuLod;
89
}
910

1011
/**
@@ -90,11 +91,38 @@ class MeshBatch extends MultiMaterial {
9091
meshBatchFlags.set(EnableStorageBuffer);
9192
}
9293

94+
public function enableCpuLod() {
95+
var prim = getPrimitive();
96+
var lodCount = prim.lodCount();
97+
if ( lodCount <= 1 )
98+
return;
99+
var prim = Std.downcast(prim, h3d.prim.HMDModel);
100+
if ( prim == null )
101+
return;
102+
if ( primitiveSubParts == null ) {
103+
primitiveSubParts = [];
104+
for ( m in 0...materials.length ) {
105+
var primitiveSubPart = new MeshBatchPart();
106+
primitiveSubPart.indexStart = prim.getMaterialIndexStart(m, 0);
107+
primitiveSubPart.indexCount = prim.getMaterialIndexCount(m, 0);
108+
primitiveSubPart.lodIndexCount = [for (i in 0...prim.lodCount() ) prim.getMaterialIndexCount(m, i)];
109+
primitiveSubPart.lodIndexStart = [for (i in 0...prim.lodCount() ) prim.getMaterialIndexStart(m, i) ];
110+
primitiveSubPart.lodConfig = prim.getLodConfig();
111+
primitiveSubPart.baseVertex = 0;
112+
primitiveSubPart.bounds = prim.getBounds();
113+
114+
primitiveSubParts.push(primitiveSubPart);
115+
}
116+
}
117+
meshBatchFlags.set(EnableCpuLod);
118+
}
119+
93120
function getPrimitive() return @:privateAccess instanced.primitive;
94121
function storageBufferEnabled() return meshBatchFlags.has(EnableStorageBuffer);
95122
function gpuUpdateEnabled() return meshBatchFlags.has(EnableGpuUpdate);
96123
function getMaxElements() return storageBufferEnabled() ? MAX_STORAGE_BUFFER_ELEMENTS : MAX_BUFFER_ELEMENTS;
97124
function hasPrimitiveOffset() return meshBatchFlags.has(HasPrimitiveOffset);
125+
function cpuLodEnabled() return meshBatchFlags.has(EnableCpuLod);
98126

99127
public function begin( emitCountTip = -1 ) : Int {
100128
instanceCount = 0;
@@ -282,6 +310,10 @@ class MeshBatch extends MultiMaterial {
282310
var primitiveSubPart = primitiveSubParts[mid];
283311
var indexCount = primitiveSubPart.indexCount;
284312
var indexStart = primitiveSubPart.indexStart;
313+
if ( curLod >= 0 && cpuLodEnabled() ) {
314+
indexStart = primitiveSubPart.lodIndexStart[curLod];
315+
indexCount = primitiveSubPart.lodIndexCount[curLod];
316+
}
285317
psBytes.setInt32(p, indexCount);
286318
psBytes.setInt32(p + 4, 1);
287319
psBytes.setInt32(p + 8, indexStart);
@@ -329,18 +361,20 @@ class MeshBatch extends MultiMaterial {
329361
if( primitiveSubBytes != null ) {
330362
if( p.instanceBuffers == null )
331363
p.instanceBuffers = [];
332-
var buf = p.instanceBuffers[index];
333-
if ( buf != null && buf.commandCount != count ) {
334-
buf.dispose();
335-
buf = null;
364+
var ibuf = p.instanceBuffers[index];
365+
if ( ibuf != null && ibuf.commandCount != count ) {
366+
ibuf.dispose();
367+
ibuf = null;
336368
}
337-
if( buf == null ) {
338-
buf = new h3d.impl.InstanceBuffer();
369+
var ibufUpload = needUpload || ibuf == null;
370+
if ( ibuf == null )
371+
ibuf = new h3d.impl.InstanceBuffer();
372+
if ( ibufUpload ) {
339373
var sub = primitiveSubBytes[p.matIndex].sub(start*20,count*20);
340374
for( i in 0...count )
341375
sub.setInt32(i*20+16, i);
342-
buf.setBuffer(count, sub);
343-
p.instanceBuffers[index] = buf;
376+
ibuf.setBuffer(count, sub);
377+
p.instanceBuffers[index] = ibuf;
344378
}
345379
}
346380

0 commit comments

Comments
 (0)