Skip to content

Commit e47450f

Browse files
author
borisrp
committed
Add uploadInstanceBufferBytes to driver to prevent realloc of InstanceBuffer if the new one is smaller than the previous one.
1 parent f43cf53 commit e47450f

File tree

5 files changed

+68
-11
lines changed

5 files changed

+68
-11
lines changed

h3d/impl/DX12Driver.hx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,13 @@ class DX12Driver extends h3d.impl.Driver {
15231523
b.data = buf;
15241524
}
15251525

1526+
override function uploadInstanceBufferBytes(b : InstanceBuffer, startVertex : Int, vertexCount : Int, buf : haxe.io.Bytes, bufPos : Int ) {
1527+
transition(b.data, COPY_DEST);
1528+
flushTransitions();
1529+
var strideBytes = 5 * 4;
1530+
updateBuffer(b.data, @:privateAccess buf.b.offset(bufPos), startVertex * strideBytes, vertexCount * strideBytes);
1531+
}
1532+
15261533
override function disposeBuffer(v:Buffer) {
15271534
disposeResource(v.vbuf);
15281535
}

h3d/impl/DirectXDriver.hx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,12 @@ class DirectXDriver extends h3d.impl.Driver {
14031403
b.data = dx.Driver.createBuffer(b.commandCount * 5 * 4, Default, UnorderedAccess, None, DrawIndirectArgs, 4, buf);
14041404
}
14051405

1406+
override function uploadInstanceBufferBytes(b : InstanceBuffer, startVertex : Int, vertexCount : Int, buf : haxe.io.Bytes, bufPos : Int ) {
1407+
if( hasDeviceError ) return;
1408+
var strideBytes = 5 * 4;
1409+
updateBuffer(b.data, @:privateAccess buf.b.offset(bufPos), startVertex * strideBytes, vertexCount * strideBytes);
1410+
}
1411+
14061412
override function disposeInstanceBuffer(b:InstanceBuffer) {
14071413
(b.data : dx.Resource).release();
14081414
b.data = null;

h3d/impl/Driver.hx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ class Driver {
237237
public function allocInstanceBuffer( b : h3d.impl.InstanceBuffer, bytes : haxe.io.Bytes ) {
238238
}
239239

240+
public function uploadInstanceBufferBytes(b : h3d.impl.InstanceBuffer, startVertex : Int, vertexCount : Int, buf : haxe.io.Bytes, bufPos : Int ) {
241+
}
242+
240243
public function disposeTexture( t : h3d.mat.Texture ) {
241244
}
242245

h3d/impl/GlDriver.hx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,33 @@ class GlDriver extends Driver {
15791579
b.data = data;
15801580
}
15811581

1582+
// override function uploadBufferBytes( b : h3d.Buffer, startVertex : Int, vertexCount : Int, buf : haxe.io.Bytes, bufPos : Int ) {
1583+
// var stride = b.format.strideBytes;
1584+
// var type = b.flags.has(IndexBuffer) ? GL.ELEMENT_ARRAY_BUFFER : GL.ARRAY_BUFFER;
1585+
// gl.bindBuffer(type, b.vbuf);
1586+
// #if hl
1587+
// gl.bufferSubData(type, startVertex * stride, streamData(buf.getData(),bufPos,vertexCount * stride), bufPos * STREAM_POS, vertexCount * stride);
1588+
// #else
1589+
// var sub = new Uint8Array(buf.getData(), bufPos, vertexCount * stride);
1590+
// gl.bufferSubData(type, startVertex * stride, sub);
1591+
// #end
1592+
// gl.bindBuffer(type, null);
1593+
// if( b.flags.has(IndexBuffer) ) curIndexBuffer = null;
1594+
// }
1595+
1596+
override function uploadInstanceBufferBytes(b : InstanceBuffer, startVertex : Int, vertexCount : Int, buf : haxe.io.Bytes, bufPos : Int ) {
1597+
var stride = 5*4;
1598+
var type = GL.DRAW_INDIRECT_BUFFER;
1599+
#if hl
1600+
gl.bindBuffer(type, b.data);
1601+
gl.bufferSubData(type, startVertex * stride, streamData(buf.getData(),bufPos,vertexCount * stride), bufPos * STREAM_POS, vertexCount * stride);
1602+
#else
1603+
var sub = new Uint8Array(buf.getData(), bufPos, vertexCount * stride);
1604+
gl.bufferSubData(type, startVertex * stride, sub);
1605+
#end
1606+
gl.bindBuffer(type, null);
1607+
}
1608+
15821609
override function disposeInstanceBuffer(b:InstanceBuffer) {
15831610
b.data = null;
15841611
}

h3d/impl/InstanceBuffer.hx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ class InstanceBuffer {
2121
this.startIndex = startIndex;
2222
}
2323

24+
function updateTriCount(commandCount : Int, bytes : haxe.io.Bytes)
25+
{
26+
triCount = 0;
27+
for( i in 0...commandCount ) {
28+
var idxCount = bytes.getInt32(i * 20);
29+
var instCount = bytes.getInt32(i * 20 + 4);
30+
var tri = Std.int((idxCount * instCount) / 3);
31+
triCount += tri;
32+
}
33+
}
34+
2435
/**
2536
Bytes are structures of 5 i32 with the following values:
2637
- indexCount : number of indexes per instance
@@ -30,19 +41,22 @@ class InstanceBuffer {
3041
- startInstanceLocation : offset in per instance buffer
3142
**/
3243
public function setBuffer( commandCount : Int, bytes : haxe.io.Bytes ) {
33-
dispose();
44+
if(commandCount > this.commandCount){
45+
dispose();
3446

35-
for( i in 0...commandCount ) {
36-
var idxCount = bytes.getInt32(i * 20);
37-
var instCount = bytes.getInt32(i * 20 + 4);
38-
var tri = Std.int((idxCount * instCount) / 3);
39-
triCount += tri;
40-
}
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 {
4153

42-
this.commandCount = commandCount;
43-
this.indexCount = 0;
44-
driver = h3d.Engine.getCurrent().driver;
45-
driver.allocInstanceBuffer(this, bytes);
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+
}
4660
}
4761

4862
public function dispose() {

0 commit comments

Comments
 (0)