InstanceBuffer.hx 2.0 KB

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