Skip to content

Commit 8036b11

Browse files
author
borisrp
committed
MeshBatch decide if InstanceBuffer has to be reallocated of just reuploaded
1 parent b24c3c7 commit 8036b11

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

h3d/impl/InstanceBuffer.hx

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ package h3d.impl;
33
@:allow(h3d.impl.Driver)
44
class InstanceBuffer {
55

6+
/**
7+
Bytes are structures of 5 i32 with the following values:
8+
- indexCount : number of indexes per instance
9+
- instanceCount : number of indexed draws
10+
- startIndexLocation : offset in indexes
11+
- baseVertexLocation : offset in buffer
12+
- startInstanceLocation : offset in per instance buffer
13+
**/
14+
public static var ELEMENT_SIZE = 20;
15+
616
var countBuffer : Dynamic;
717
var data : Dynamic;
818
var driver : h3d.impl.Driver;
@@ -25,38 +35,29 @@ class InstanceBuffer {
2535
{
2636
triCount = 0;
2737
for( i in 0...commandCount ) {
28-
var idxCount = bytes.getInt32(i * 20);
29-
var instCount = bytes.getInt32(i * 20 + 4);
38+
var idxCount = bytes.getInt32(i * ELEMENT_SIZE);
39+
var instCount = bytes.getInt32(i * ELEMENT_SIZE + 4);
3040
var tri = Std.int((idxCount * instCount) / 3);
3141
triCount += tri;
3242
}
3343
}
3444

35-
/**
36-
Bytes are structures of 5 i32 with the following values:
37-
- indexCount : number of indexes per instance
38-
- instanceCount : number of indexed draws
39-
- startIndexLocation : offset in indexes
40-
- baseVertexLocation : offset in buffer
41-
- startInstanceLocation : offset in per instance buffer
42-
**/
43-
public function setBuffer( commandCount : Int, bytes : haxe.io.Bytes ) {
44-
if(commandCount > this.commandCount){
45-
dispose();
45+
public function uploadBytes(commandCount : Int, bytes : haxe.io.Bytes) {
46+
updateTriCount(commandCount, bytes);
47+
this.commandCount = commandCount;
48+
this.indexCount = 0;
49+
driver = h3d.Engine.getCurrent().driver;
50+
driver.uploadInstanceBufferBytes(this, 0, commandCount, bytes, 0);
51+
}
4652

47-
updateTriCount(commandCount, bytes);
48-
this.commandCount = commandCount;
49-
this.indexCount = 0;
50-
driver = h3d.Engine.getCurrent().driver;
51-
driver.allocInstanceBuffer(this, bytes);
52-
} else {
53+
public function allocFromBytes(commandCount : Int, bytes : haxe.io.Bytes) {
54+
dispose();
5355

54-
updateTriCount(commandCount, bytes);
55-
this.commandCount = commandCount;
56-
this.indexCount = 0;
57-
driver = h3d.Engine.getCurrent().driver;
58-
driver.uploadInstanceBufferBytes(this, 0, commandCount, bytes, 0);
59-
}
56+
updateTriCount(commandCount, bytes);
57+
this.commandCount = commandCount;
58+
this.indexCount = 0;
59+
driver = h3d.Engine.getCurrent().driver;
60+
driver.allocInstanceBuffer(this, bytes);
6061
}
6162

6263
public function dispose() {

h3d/scene/MeshBatch.hx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,15 @@ class MeshBatch extends MultiMaterial {
281281
primitiveSubBytes = [for ( i in 0...primitiveSubParts.length ) haxe.io.Bytes.alloc(128)];
282282
instanced.commands = null;
283283
}
284+
var instance_size = h3d.impl.InstanceBuffer.ELEMENT_SIZE;
284285
for ( i in 0...primitiveSubBytes.length ) {
285-
if( primitiveSubBytes[i].length < (instanceCount+1) * 20 ) {
286+
if( primitiveSubBytes[i].length < (instanceCount+1) * instance_size ) {
286287
var next = haxe.io.Bytes.alloc(Std.int(primitiveSubBytes[i].length*3/2));
287-
next.blit(0, primitiveSubBytes[i], 0, instanceCount * 20);
288+
next.blit(0, primitiveSubBytes[i], 0, instanceCount * instance_size);
288289
primitiveSubBytes[i] = next;
289290
}
290291
}
291-
var p = instanceCount * 20;
292+
var p = instanceCount * instance_size;
292293
for ( mid => psBytes in primitiveSubBytes ) {
293294
var primitiveSubPart = primitiveSubParts[mid];
294295
var indexCount = primitiveSubPart.indexCount;
@@ -316,6 +317,7 @@ class MeshBatch extends MultiMaterial {
316317
var alloc = hxd.impl.Allocator.get();
317318

318319
var prim = getPrimitive();
320+
var instance_size = h3d.impl.InstanceBuffer.ELEMENT_SIZE;
319321

320322
while( p != null ) {
321323
var index = 0;
@@ -355,11 +357,16 @@ class MeshBatch extends MultiMaterial {
355357
if ( ibufUpload ) {
356358
var psBytes = primitiveSubBytes[p.matIndex];
357359
if ( start > 0 && count < instanceCount ) {
358-
psBytes = psBytes.sub(start*20,count*20);
360+
psBytes = psBytes.sub(start*instance_size,count*instance_size);
359361
for( i in 0...count )
360-
psBytes.setInt32(i*20+16, i);
362+
psBytes.setInt32(i*instance_size+16, i);
363+
}
364+
365+
if(count < ibuf.commandCount && meshBatchFlags.has(EnableResizeDown)){
366+
ibuf.uploadBytes(count, psBytes);
367+
} else {
368+
ibuf.allocFromBytes(count, psBytes);
361369
}
362-
ibuf.setBuffer(count, psBytes);
363370
p.instanceBuffers[index] = ibuf;
364371
}
365372
}

0 commit comments

Comments
 (0)