InstanceBuffer.hx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package h3d.impl;
  2. @:allow(h3d.impl.Driver)
  3. class InstanceBuffer {
  4. var countBuffer : Dynamic;
  5. var data : Dynamic;
  6. var driver : h3d.impl.Driver;
  7. var indexCount : Int;
  8. var startIndex : Int;
  9. public var triCount(default,null) : Int = 0;
  10. public var commandCount(default, null) : Int;
  11. public function new() {
  12. }
  13. public function setCommand( commandCount : Int, indexCount : Int, startIndex=0 ) {
  14. this.commandCount = commandCount;
  15. this.indexCount = indexCount;
  16. this.triCount = Std.int((commandCount * indexCount) / 3);
  17. this.startIndex = startIndex;
  18. }
  19. /**
  20. Bytes are structures of 5 i32 with the following values:
  21. - indexCount : number of indexes per instance
  22. - instanceCount : number of indexed draws
  23. - startIndexLocation : offset in indexes
  24. - baseVertexLocation : offset in buffer
  25. - startInstanceLocation : offset in per instance buffer
  26. **/
  27. public function setBuffer( commandCount : Int, bytes : haxe.io.Bytes ) {
  28. dispose();
  29. for( i in 0...commandCount ) {
  30. var idxCount = bytes.getInt32(i * 20);
  31. var instCount = bytes.getInt32(i * 20 + 4);
  32. var tri = Std.int((idxCount * instCount) / 3);
  33. triCount += tri;
  34. }
  35. this.commandCount = commandCount;
  36. this.indexCount = 0;
  37. driver = h3d.Engine.getCurrent().driver;
  38. driver.allocInstanceBuffer(this, bytes);
  39. }
  40. public function dispose() {
  41. if( data != null ) driver.disposeInstanceBuffer(this);
  42. }
  43. }