Forráskód Böngészése

added readback buffer capacity in directx

ncannasse 8 éve
szülő
commit
064fcdd341
5 módosított fájl, 65 hozzáadás és 2 törlés
  1. 17 0
      h3d/Buffer.hx
  2. 4 0
      h3d/Indexes.hx
  3. 32 2
      h3d/impl/DirectXDriver.hx
  4. 8 0
      h3d/impl/Driver.hx
  5. 4 0
      h3d/impl/ManagedBuffer.hx

+ 17 - 0
h3d/Buffer.hx

@@ -115,6 +115,23 @@ class Buffer {
 		}
 	}
 
+	public function readBytes( bytes : haxe.io.Bytes, bytesPosition : Int, vertices : Int,  startVertice : Int = 0 ) {
+		var cur = this;
+		while( cur != null && startVertice >= cur.vertices ) {
+			startVertice -= cur.vertices;
+			cur = cur.next;
+		}
+		while( vertices > 0 ) {
+			if( cur == null ) throw "Too many vertices";
+			var count = vertices + startVertice > cur.vertices ? cur.vertices - startVertice : vertices;
+			cur.buffer.readVertexBytes(cur.position + startVertice, count, bytes, bytesPosition);
+			startVertice = 0;
+			bytesPosition += count * buffer.stride * 4;
+			vertices -= count;
+			cur = cur.next;
+		}
+	}
+
 	public static function ofFloats( v : hxd.FloatBuffer, stride : Int, ?flags, ?allocPos ) {
 		var nvert = Std.int(v.length / stride);
 		var b = new Buffer(nvert, stride, flags, allocPos);

+ 4 - 0
h3d/Indexes.hx

@@ -26,6 +26,10 @@ class Indexes {
 		mem.driver.uploadIndexBytes(this.ibuf, 0, indices, bytes, dataPos);
 	}
 
+	public function readBytes( bytes : haxe.io.Bytes, bytesPosition : Int, indices : Int, startIndice : Int = 0 ) {
+		mem.driver.readIndexBytes(this.ibuf, startIndice, indices, bytes, bytesPosition);
+	}
+
 	public function dispose() {
 		if( ibuf != null )
 			mem.deleteIndexes(this);

+ 32 - 2
h3d/impl/DirectXDriver.hx

@@ -364,14 +364,44 @@ class DirectXDriver extends h3d.impl.Driver {
 		updateBuffer(i.res, @:privateAccess buf.b.offset(bufPos << 1), startIndice << 1, indiceCount << 1);
 	}
 
-	override public function uploadVertexBuffer(v:VertexBuffer, startVertex:Int, vertexCount:Int, buf:hxd.FloatBuffer, bufPos:Int) {
+	override function uploadVertexBuffer(v:VertexBuffer, startVertex:Int, vertexCount:Int, buf:hxd.FloatBuffer, bufPos:Int) {
 		updateBuffer(v.res, hl.Bytes.getArray(buf.getNative()).offset(bufPos<<2), startVertex * v.stride << 2, vertexCount * v.stride << 2);
 	}
 
-	override public function uploadVertexBytes(v:VertexBuffer, startVertex:Int, vertexCount:Int, buf:haxe.io.Bytes, bufPos:Int) {
+	override function uploadVertexBytes(v:VertexBuffer, startVertex:Int, vertexCount:Int, buf:haxe.io.Bytes, bufPos:Int) {
 		updateBuffer(v.res, @:privateAccess buf.b.offset(bufPos << 2), startVertex * v.stride << 2, vertexCount * v.stride << 2);
 	}
 
+	override function readIndexBytes(v:IndexBuffer, startIndice:Int, indiceCount:Int, buf:haxe.io.Bytes, bufPos:Int) {
+		var tmp = dx.Driver.createBuffer(indiceCount << 1, Staging, None, CpuRead | CpuWrite, None, 0, null);
+		box.left = startIndice << 1;
+		box.top = 0;
+		box.front = 0;
+		box.right = (startIndice + indiceCount) << 1;
+		box.bottom = 1;
+		box.back = 1;
+		tmp.copySubresourceRegion(0, 0, 0, 0, v.res, 0, box);
+		var ptr = tmp.map(0, Read, true, null);
+		@:privateAccess buf.b.blit(bufPos, ptr, 0, indiceCount << 1);
+		tmp.unmap(0);
+		tmp.release();
+	}
+
+	override function readVertexBytes(v:VertexBuffer, startVertex:Int, vertexCount:Int, buf:haxe.io.Bytes, bufPos:Int) {
+		var tmp = dx.Driver.createBuffer(vertexCount * v.stride * 4, Staging, None, CpuRead | CpuWrite, None, 0, null);
+		box.left = startVertex * v.stride * 4;
+		box.top = 0;
+		box.front = 0;
+		box.right = (startVertex + vertexCount) * 4 * v.stride;
+		box.bottom = 1;
+		box.back = 1;
+		tmp.copySubresourceRegion(0, 0, 0, 0, v.res, 0, box);
+		var ptr = tmp.map(0, Read, true, null);
+		@:privateAccess buf.b.blit(bufPos, ptr, 0, vertexCount * v.stride * 4);
+		tmp.unmap(0);
+		tmp.release();
+	}
+
 	override function uploadTextureBitmap(t:h3d.mat.Texture, bmp:hxd.BitmapData, mipLevel:Int, side:Int) {
 		var pixels = bmp.getPixels();
 		uploadTexturePixels(t, pixels, mipLevel, side);

+ 8 - 0
h3d/impl/Driver.hx

@@ -233,6 +233,14 @@ class Driver {
 	public function uploadTexturePixels( t : h3d.mat.Texture, pixels : hxd.Pixels, mipLevel : Int, side : Int ) {
 	}
 
+	public function readVertexBytes( v : VertexBuffer, startVertex : Int, vertexCount : Int, buf : haxe.io.Bytes, bufPos : Int ) {
+		throw "Driver does not allow to read vertex bytes";
+	}
+
+	public function readIndexBytes( v : IndexBuffer, startVertex : Int, vertexCount : Int, buf : haxe.io.Bytes, bufPos : Int ) {
+		throw "Driver does not allow to read index bytes";
+	}
+
 	/**
 		Returns true if we could copy the texture, false otherwise (not supported by driver or mismatch in size/format)
 	**/

+ 4 - 0
h3d/impl/ManagedBuffer.hx

@@ -49,6 +49,10 @@ class ManagedBuffer {
 		mem.driver.uploadVertexBytes(vbuf, start, vertices, data, dataPos);
 	}
 
+	public function readVertexBytes( start : Int, vertices : Int, data : haxe.io.Bytes, dataPos = 0 ) {
+		mem.driver.readVertexBytes(vbuf, start, vertices, data, dataPos);
+	}
+
 	public function alloc(vertices,align) {
 		var p = allocPosition(vertices, align);
 		if( p < 0 )