Allocator.hx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package hxd.impl;
  2. enum abstract BufferFlags(Int) {
  3. public var Dynamic = 0;
  4. public var Static = 1;
  5. public var UniformDynamic = 2;
  6. public inline function toInt() : Int {
  7. return this;
  8. }
  9. }
  10. class Allocator {
  11. public function new() {
  12. }
  13. // GPU
  14. public function allocBuffer( vertices : Int, format, flags : BufferFlags = Dynamic ) : h3d.Buffer {
  15. return new h3d.Buffer(vertices, format,
  16. switch( flags ) {
  17. case Static: null;
  18. case Dynamic: [Dynamic];
  19. case UniformDynamic: [UniformBuffer,Dynamic];
  20. });
  21. }
  22. public function ofFloats( v : hxd.FloatBuffer, format : hxd.BufferFormat, flags : BufferFlags = Dynamic ) {
  23. var nvert = Std.int(v.length / format.stride);
  24. return ofSubFloats(v, nvert, format, flags);
  25. }
  26. public function ofSubFloats( v : hxd.FloatBuffer, vertices : Int, format, flags : BufferFlags = Dynamic ) {
  27. var b = allocBuffer(vertices, format, flags);
  28. b.uploadFloats(v, 0, vertices);
  29. return b;
  30. }
  31. public function disposeBuffer( b : h3d.Buffer ) {
  32. b.dispose();
  33. }
  34. public function allocIndexBuffer( count : Int ) {
  35. return new h3d.Indexes(count);
  36. }
  37. public function ofIndexes( ib: hxd.IndexBuffer, length = -1) {
  38. if( length < 0 && ib != null ) length = ib.length;
  39. var idx = allocIndexBuffer( length );
  40. idx.uploadIndexes(ib, 0, length);
  41. return idx;
  42. }
  43. public function disposeIndexBuffer( i : h3d.Indexes ) {
  44. i.dispose();
  45. }
  46. public function onContextLost() {
  47. }
  48. // CPU
  49. public function allocFloats( count : Int ) : hxd.FloatBuffer {
  50. return new hxd.FloatBuffer(count);
  51. }
  52. public function disposeFloats( f : hxd.FloatBuffer ) {
  53. }
  54. public function allocIndexes( count : Int ) {
  55. return new hxd.IndexBuffer(count);
  56. }
  57. public function disposeIndexes( i : hxd.IndexBuffer ) {
  58. }
  59. static var inst : Allocator;
  60. public static function set( a : Allocator ) {
  61. inst = a;
  62. }
  63. public static function get() : Allocator {
  64. if( inst == null ) inst = new Allocator();
  65. return inst;
  66. }
  67. }