Browse Source

Add readBufferBytes into drivers.

borisrp 4 months ago
parent
commit
6043ce9449
2 changed files with 41 additions and 0 deletions
  1. 32 0
      h3d/impl/DX12Driver.hx
  2. 9 0
      h3d/impl/GlDriver.hx

+ 32 - 0
h3d/impl/DX12Driver.hx

@@ -1568,6 +1568,38 @@ class DX12Driver extends h3d.impl.Driver {
 		transition(b.vbuf, b.flags.has(IndexBuffer) ? INDEX_BUFFER : ((b.flags.has(ReadWriteBuffer)) ? UNORDERED_ACCESS : VERTEX_AND_CONSTANT_BUFFER));
 	}
 
+	override function readBufferBytes(b:Buffer, startVertex:Int, vertexCount:Int, buf:haxe.io.Bytes, bufPos:Int) {
+		var stride = b.format.strideBytes;
+
+		var totalSize = vertexCount*stride;
+
+		var desc = new ResourceDesc();
+		var flags = new haxe.EnumFlags();
+		desc.dimension = BUFFER;
+		desc.width = totalSize;
+		desc.height = 1;
+		desc.depthOrArraySize = 1;
+		desc.mipLevels = 1;
+		desc.sampleDesc.count = 1;
+		desc.layout = ROW_MAJOR;
+		tmp.heap.type = READBACK;
+		var tmpBuf = Driver.createCommittedResource(tmp.heap, flags, desc, COPY_DEST, null);
+
+		transition(b.vbuf, COPY_SOURCE);
+		flushTransitions();
+
+		frame.commandList.copyBufferRegion(tmpBuf, 0, b.vbuf.res, startVertex*stride, totalSize);
+
+		flushFrame();
+		waitGpu();
+
+		var output = tmpBuf.map(0, null);
+		@:privateAccess buf.b.blit(bufPos, output, 0, totalSize);
+		tmpBuf.release();
+
+		beginFrame();
+	}
+
 	// ------------ TEXTURES -------
 
 	function getTextureFormat( t : h3d.mat.Texture ) : DxgiFormat {

+ 9 - 0
h3d/impl/GlDriver.hx

@@ -1470,6 +1470,15 @@ class GlDriver extends Driver {
 		if( b.flags.has(IndexBuffer) ) curIndexBuffer = null;
 	}
 
+	override function readBufferBytes(b:h3d.Buffer, startVertex:Int, vertexCount:Int, buf:haxe.io.Bytes, bufPos:Int) {
+		var stride = b.format.strideBytes;
+		var totalSize = vertexCount*stride;
+		var type = b.flags.has(IndexBuffer) ? GL.ELEMENT_ARRAY_BUFFER : GL.ARRAY_BUFFER;
+		gl.bindBuffer(type, b.vbuf);
+		gl.getBufferSubData(type, startVertex*stride, @:privateAccess buf.b, bufPos*STREAM_POS, totalSize);
+		gl.bindBuffer(type, null);
+	}
+
 	override function uploadIndexData( i : h3d.Buffer, startIndice : Int, indiceCount : Int, buf : hxd.IndexBuffer, bufPos : Int ) {
 		var bits = i.format.strideBytes >> 1;
 		gl.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, i.vbuf);