MeshBatch.hx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. package h3d.scene;
  2. enum MeshBatchFlag {
  3. EnableResizeDown;
  4. EnableGpuUpdate;
  5. EnableStorageBuffer;
  6. HasPrimitiveOffset;
  7. EnableCpuLod;
  8. ForceGpuUpdate;
  9. }
  10. /**
  11. h3d.scene.MeshBatch allows to draw multiple meshed in a single draw call.
  12. See samples/MeshBatch.hx for an example.
  13. **/
  14. class MeshBatch extends MultiMaterial {
  15. static var modelViewID = hxsl.Globals.allocID("global.modelView");
  16. static var modelViewInverseID = hxsl.Globals.allocID("global.modelViewInverse");
  17. static var previousModelViewID = hxsl.Globals.allocID("global.previousModelView");
  18. static var MAX_BUFFER_ELEMENTS = 4096;
  19. static var MAX_STORAGE_BUFFER_ELEMENTS = 128 * 1024 * 1024 >> 2;
  20. static var BATCH_START_FMT = hxd.BufferFormat.make([{ name : "Batch_Start", type : DFloat }]);
  21. var instanced : h3d.prim.Instanced;
  22. var dataPasses : BatchData;
  23. var needUpload = false;
  24. var instancedParams : hxsl.Cache.BatchInstanceParams;
  25. var meshBatchFlags(default, null) : haxe.EnumFlags<MeshBatchFlag>;
  26. /**
  27. Set if shader list or shader constants has changed, before calling begin()
  28. **/
  29. public var shadersChanged = true;
  30. /**
  31. The number of instances on this batch
  32. **/
  33. public var instanceCount(default,null) : Int = 0;
  34. /**
  35. * If set, use this position in emitInstance() instead MeshBatch absolute position
  36. **/
  37. public var worldPosition : Matrix;
  38. var invWorldPosition : Matrix;
  39. /**
  40. Tells the mesh batch to draw only a subpart of the primitive.
  41. One primitiveSubPart per material.
  42. **/
  43. public var primitiveSubParts : Array<MeshBatchPart>;
  44. var primitiveSubBytes : Array<haxe.io.Bytes>;
  45. /**
  46. If set, exact bounds will be recalculated during emitInstance (default true)
  47. **/
  48. public var calcBounds = true;
  49. /**
  50. With EnableCpuLod, set the lod of the next emitInstance.
  51. Without EnableCpuLod and not using primitiveSubParts, set the lod of the whole batch.
  52. */
  53. public var curLod : Int = -1;
  54. public function new( primitive, ?material, ?parent ) {
  55. instanced = new h3d.prim.Instanced();
  56. instanced.commands = new h3d.impl.InstanceBuffer();
  57. instanced.setMesh(primitive);
  58. super(instanced, material == null ? null : [material], parent);
  59. for( p in this.material.getPasses() )
  60. @:privateAccess p.batchMode = true;
  61. }
  62. /**
  63. * Buffer of per instance params such as position is created as a storage buffer
  64. * allowing for huge amount of instances.
  65. */
  66. public function enableStorageBuffer() {
  67. meshBatchFlags.set(EnableStorageBuffer);
  68. }
  69. /**
  70. * Buffer of per instance params such as position is created with its own format
  71. * allowing compute shaders to update those parameters.
  72. */
  73. public function enableGpuUpdate() {
  74. meshBatchFlags.set(EnableGpuUpdate);
  75. meshBatchFlags.set(EnableStorageBuffer);
  76. }
  77. /**
  78. * Force PerInstance to be setup by a compute shader.
  79. * Don't support without Storage Buffer to simplify implementation.
  80. */
  81. public function forceGpuUpdate() {
  82. meshBatchFlags.set(EnableGpuUpdate);
  83. meshBatchFlags.set(EnableStorageBuffer);
  84. meshBatchFlags.set(ForceGpuUpdate);
  85. }
  86. public function enableCpuLod() {
  87. var prim = getPrimitive();
  88. var lodCount = prim.lodCount();
  89. if ( lodCount <= 1 )
  90. return;
  91. if ( partsFromPrimitive(prim) )
  92. meshBatchFlags.set(EnableCpuLod);
  93. }
  94. function getPrimitive() return @:privateAccess instanced.primitive;
  95. function storageBufferEnabled() return meshBatchFlags.has(EnableStorageBuffer);
  96. function gpuUpdateEnabled() return meshBatchFlags.has(EnableGpuUpdate);
  97. function gpuUpdateForced() return meshBatchFlags.has(ForceGpuUpdate);
  98. function getMaxElements() return storageBufferEnabled() ? MAX_STORAGE_BUFFER_ELEMENTS : MAX_BUFFER_ELEMENTS;
  99. function hasPrimitiveOffset() return meshBatchFlags.has(HasPrimitiveOffset);
  100. function cpuLodEnabled() return meshBatchFlags.has(EnableCpuLod);
  101. inline function shouldResizeDown( currentSize : Int, minSize : Int ) : Bool {
  102. return meshBatchFlags.has(EnableResizeDown) && currentSize > minSize << 1;
  103. }
  104. public function begin( emitCountTip = -1 ) : Int {
  105. instanceCount = 0;
  106. instanced.initBounds();
  107. if( shadersChanged ) {
  108. initShadersMapping();
  109. shadersChanged = false;
  110. }
  111. if( emitCountTip < 0 )
  112. emitCountTip = 128;
  113. var p = dataPasses;
  114. var alloc = hxd.impl.Allocator.get();
  115. while( p != null ) {
  116. var size = emitCountTip * p.paramsCount * 4;
  117. if( p.data == null || p.data.length < size || shouldResizeDown(p.data.length, size) ) {
  118. if( p.data != null ) alloc.disposeFloats(p.data);
  119. p.data = alloc.allocFloats(size);
  120. }
  121. p = p.next;
  122. }
  123. return emitCountTip;
  124. }
  125. function initShadersMapping() {
  126. var scene = getScene();
  127. if( scene == null ) return;
  128. cleanPasses();
  129. updateHasPrimitiveOffset();
  130. for( index in 0...materials.length ) {
  131. var mat = materials[index];
  132. if( mat == null ) continue;
  133. var prim = getPrimitive();
  134. var matCount = prim.getMaterialIndexCount(index);
  135. var matStart = prim.getMaterialIndexStart(index);
  136. for( p in mat.getPasses() ) @:privateAccess {
  137. var ctx = scene.renderer.getPassByName(p.name);
  138. if( ctx == null ) continue;
  139. var output = ctx.output;
  140. var shaders = p.getShadersRec();
  141. var rt = output.compileShaders(scene.ctx.globals, shaders, Default);
  142. var shader = output.shaderCache.makeBatchShader(rt, shaders, instancedParams);
  143. var b = createBatchData();
  144. b.indexCount = matCount;
  145. b.indexStart = matStart;
  146. b.paramsCount = shader.paramsSize;
  147. b.maxInstance = Std.int( getMaxElements() / b.paramsCount);
  148. b.bufferFormat = hxd.BufferFormat.VEC4_DATA;
  149. if( b.maxInstance <= 0 )
  150. throw "Mesh batch shaders needs at least one perInstance parameter";
  151. b.params = shader.params;
  152. b.shader = shader;
  153. b.pass = p;
  154. b.matIndex = index;
  155. b.shaders = [null/*link shader*/];
  156. p.dynamicParameters = true;
  157. p.batchMode = true;
  158. if ( gpuUpdateEnabled() )
  159. calcBufferFormat(b);
  160. b.next = dataPasses;
  161. dataPasses = b;
  162. var sl = shaders;
  163. while( sl != null ) {
  164. b.shaders.push(sl.s);
  165. sl = sl.next;
  166. }
  167. shader.Batch_UseStorage = storageBufferEnabled();
  168. shader.Batch_Count = storageBufferEnabled() ? 0 : b.maxInstance * b.paramsCount;
  169. shader.Batch_HasOffset = hasPrimitiveOffset();
  170. shader.constBits = (shader.Batch_Count << 2) | (shader.Batch_UseStorage ? ( 1 << 1 ) : 0) | (shader.Batch_HasOffset ? 1 : 0);
  171. shader.updateConstants(null);
  172. }
  173. }
  174. // add batch shaders
  175. var p = dataPasses;
  176. while( p != null ) {
  177. @:privateAccess p.pass.addSelfShader(p.shader);
  178. p = p.next;
  179. }
  180. }
  181. function updateHasPrimitiveOffset() meshBatchFlags.setTo(HasPrimitiveOffset, primitiveSubParts != null);
  182. function createBatchData() {
  183. return new BatchData();
  184. }
  185. function calcBufferFormat(b : BatchData) {
  186. var pl = [];
  187. var p = b.params;
  188. while( p != null ) {
  189. pl.push(p);
  190. p = p.next;
  191. }
  192. pl.sort(function(p1,p2) return p1.pos - p2.pos);
  193. var fmt : Array<hxd.BufferFormat.BufferInput> = [];
  194. var curPos = 0;
  195. var paddingIndex = 0;
  196. for( p in pl ) {
  197. var paddingSize = p.pos - curPos;
  198. if ( paddingSize > 0 ) {
  199. var paddingType : hxsl.Ast.Type = switch ( paddingSize ) {
  200. case 1:
  201. TFloat;
  202. case 2,3:
  203. TVec(paddingSize, VFloat);
  204. default:
  205. throw "Buffer has padding";
  206. }
  207. var t = hxd.BufferFormat.InputFormat.fromHXSL(paddingType);
  208. fmt.push(new hxd.BufferFormat.BufferInput("padding_"+paddingIndex,t));
  209. paddingIndex++;
  210. curPos = p.pos;
  211. }
  212. var name = p.name;
  213. var prev = fmt.length;
  214. switch( p.type ) {
  215. case TMat3:
  216. for( i in 0...3 )
  217. fmt.push(new hxd.BufferFormat.BufferInput(name+"__m"+i,DVec3));
  218. case TMat3x4:
  219. for( i in 0...3 )
  220. fmt.push(new hxd.BufferFormat.BufferInput(name+"__m"+i,DVec4));
  221. case TMat4:
  222. for( i in 0...4 )
  223. fmt.push(new hxd.BufferFormat.BufferInput(name+"__m"+i,DVec4));
  224. default:
  225. var t = hxd.BufferFormat.InputFormat.fromHXSL(p.type);
  226. fmt.push(new hxd.BufferFormat.BufferInput(p.name,t));
  227. }
  228. for( i in prev...fmt.length )
  229. curPos += fmt[i].getBytesSize() >> 2;
  230. }
  231. if ( curPos & 3 != 0) {
  232. var paddingSize = 4 - (curPos & 3);
  233. var paddingType : hxsl.Ast.Type = switch ( paddingSize ) {
  234. case 1:
  235. TFloat;
  236. case 2,3:
  237. TVec(paddingSize, VFloat);
  238. default:
  239. throw "Buffer has padding";
  240. }
  241. var t = hxd.BufferFormat.InputFormat.fromHXSL(paddingType);
  242. fmt.push(new hxd.BufferFormat.BufferInput("padding_"+paddingIndex,t));
  243. }
  244. b.bufferFormat = hxd.BufferFormat.make(fmt);
  245. }
  246. public function emitInstance() {
  247. if( primitiveSubParts != null )
  248. emitPrimitiveSubParts();
  249. if(!gpuUpdateForced()){
  250. if( worldPosition == null ) syncPos();
  251. if (primitiveSubParts == null && calcBounds)
  252. instanced.addInstanceBounds(worldPosition == null ? absPos : worldPosition);
  253. var p = dataPasses;
  254. while( p != null ) {
  255. syncData(p);
  256. p = p.next;
  257. }
  258. }
  259. instanceCount++;
  260. }
  261. function emitPrimitiveSubParts() {
  262. if(calcBounds) @:privateAccess {
  263. for ( primitiveSubPart in primitiveSubParts ) {
  264. instanced.tmpBounds.load(primitiveSubPart.bounds);
  265. instanced.tmpBounds.transform(worldPosition == null ? absPos : worldPosition);
  266. instanced.bounds.add(instanced.tmpBounds);
  267. }
  268. }
  269. if( primitiveSubBytes == null ) {
  270. if ( primitiveSubParts.length != materials.length )
  271. throw "Instancing using primitive sub parts must match material count";
  272. primitiveSubBytes = [for ( i in 0...primitiveSubParts.length ) haxe.io.Bytes.alloc(128)];
  273. instanced.commands = null;
  274. }
  275. var instanceSize = h3d.impl.InstanceBuffer.ELEMENT_SIZE;
  276. for ( i in 0...primitiveSubBytes.length ) {
  277. if( primitiveSubBytes[i].length < (instanceCount+1) * instanceSize ) {
  278. var next = haxe.io.Bytes.alloc(Std.int(primitiveSubBytes[i].length*3/2));
  279. next.blit(0, primitiveSubBytes[i], 0, instanceCount * instanceSize);
  280. primitiveSubBytes[i] = next;
  281. }
  282. }
  283. var p = instanceCount * instanceSize;
  284. for ( mid => psBytes in primitiveSubBytes ) {
  285. var primitiveSubPart = primitiveSubParts[mid];
  286. var indexCount = primitiveSubPart.indexCount;
  287. var indexStart = primitiveSubPart.indexStart;
  288. if ( curLod >= 0 && cpuLodEnabled() ) {
  289. indexStart = primitiveSubPart.lodIndexStart[curLod];
  290. indexCount = primitiveSubPart.lodIndexCount[curLod];
  291. }
  292. psBytes.setInt32(p, indexCount);
  293. psBytes.setInt32(p + 4, 1);
  294. psBytes.setInt32(p + 8, indexStart);
  295. psBytes.setInt32(p + 12, primitiveSubPart.baseVertex);
  296. psBytes.setInt32(p + 16, instanceCount);
  297. }
  298. }
  299. override function sync(ctx:RenderContext) {
  300. super.sync(ctx);
  301. if( instanceCount == 0 ) return;
  302. flush();
  303. }
  304. public function flush() {
  305. var p = dataPasses;
  306. var alloc = hxd.impl.Allocator.get();
  307. var prim = getPrimitive();
  308. var instanceSize = h3d.impl.InstanceBuffer.ELEMENT_SIZE;
  309. while( p != null ) {
  310. var index = 0;
  311. var start = 0;
  312. while( start < instanceCount ) {
  313. var upload = needUpload;
  314. var buf = p.buffers[index];
  315. var count = instanceCount - start;
  316. if( count > p.maxInstance )
  317. count = p.maxInstance;
  318. var maxVertexCount = gpuUpdateEnabled() ? p.maxInstance : getMaxElements();
  319. var vertexCount = Std.int( count * (( 4 * p.paramsCount ) / p.bufferFormat.stride) );
  320. var vertexCountAllocated = #if js Std.int( MAX_BUFFER_ELEMENTS * 4 / p.bufferFormat.stride ) #else hxd.Math.imin( hxd.Math.nextPOT( vertexCount ), maxVertexCount ) #end;
  321. if( buf == null || buf.isDisposed() || buf.vertices < vertexCountAllocated ) {
  322. var bufferFlags : hxd.impl.Allocator.BufferFlags = storageBufferEnabled() ? UniformReadWrite : UniformDynamic;
  323. if ( buf != null )
  324. alloc.disposeBuffer(buf);
  325. buf = alloc.allocBuffer( vertexCountAllocated, p.bufferFormat,bufferFlags );
  326. p.buffers[index] = buf;
  327. upload = true;
  328. }
  329. if( upload && !gpuUpdateForced())
  330. buf.uploadFloats(p.data, start * p.paramsCount * 4, vertexCount);
  331. if( primitiveSubBytes != null ) {
  332. if( p.instanceBuffers == null )
  333. p.instanceBuffers = [];
  334. var ibuf = p.instanceBuffers[index];
  335. if ( ibuf == null )
  336. ibuf = new h3d.impl.InstanceBuffer();
  337. var ibufUpload = needUpload || ibuf.commandCount != count;
  338. if ( ibufUpload ) {
  339. var psBytes = primitiveSubBytes[p.matIndex];
  340. if ( start > 0 && count < instanceCount ) {
  341. psBytes = psBytes.sub(start*instanceSize,count*instanceSize);
  342. for( i in 0...count )
  343. psBytes.setInt32(i*instanceSize+16, i);
  344. }
  345. var ibufMaxCommandCount = ibuf.maxCommandCount;
  346. if ( shouldResizeDown(ibufMaxCommandCount, count) || count > ibufMaxCommandCount) {
  347. ibuf.allocFromBytes(count, psBytes);
  348. } else {
  349. ibuf.uploadBytes(count, psBytes);
  350. }
  351. p.instanceBuffers[index] = ibuf;
  352. }
  353. }
  354. onFlushBuffer(p, index, count);
  355. start += count;
  356. index++;
  357. }
  358. onFlushPass(p);
  359. while( p.buffers.length > index )
  360. alloc.disposeBuffer( p.buffers.pop() );
  361. p = p.next;
  362. }
  363. if( hasPrimitiveOffset() ) {
  364. var offsets = prim.resolveBuffer("Batch_Start");
  365. if( offsets == null || offsets.vertices < instanceCount || offsets.isDisposed() ) {
  366. if( offsets != null ) {
  367. offsets.dispose();
  368. prim.removeBuffer(offsets);
  369. }
  370. var tmp = haxe.io.Bytes.alloc(4 * instanceCount);
  371. for( i in 0...instanceCount )
  372. tmp.setFloat(i<<2, i);
  373. offsets = new h3d.Buffer(instanceCount, BATCH_START_FMT);
  374. offsets.uploadBytes(tmp,0,instanceCount);
  375. prim.addBuffer(offsets);
  376. }
  377. }
  378. needUpload = false;
  379. }
  380. function onFlushBuffer(p : BatchData, index : Int, count : Int) {}
  381. function onFlushPass(p : BatchData) {}
  382. function syncData( batch : BatchData ) {
  383. var startPos = batch.paramsCount * instanceCount << 2;
  384. // in case we are bigger than emitCountTip
  385. if( startPos + (batch.paramsCount<<2) > batch.data.length )
  386. batch.data.grow(batch.data.length << 1);
  387. var p = batch.params;
  388. var buf = batch.data;
  389. var shaders = batch.shaders;
  390. var calcInv = false;
  391. while( p != null ) {
  392. var bufLoader = new hxd.FloatBufferLoader(buf, startPos + p.pos);
  393. if( p.perObjectGlobal != null ) {
  394. if ( p.perObjectGlobal.gid == modelViewID ) {
  395. bufLoader.loadMatrix(worldPosition != null ? worldPosition : absPos);
  396. } else if ( p.perObjectGlobal.gid == modelViewInverseID ) {
  397. if( worldPosition == null )
  398. bufLoader.loadMatrix(getInvPos());
  399. else {
  400. if( !calcInv ) {
  401. calcInv = true;
  402. if( invWorldPosition == null ) invWorldPosition = new h3d.Matrix();
  403. invWorldPosition.initInverse(worldPosition);
  404. }
  405. bufLoader.loadMatrix(invWorldPosition);
  406. }
  407. } else if ( p.perObjectGlobal.gid == previousModelViewID )
  408. bufLoader.loadMatrix(worldPosition != null ? worldPosition : absPos );
  409. else
  410. throw "Unsupported global param "+p.perObjectGlobal.path;
  411. p = p.next;
  412. continue;
  413. }
  414. var curShader = shaders[p.instance];
  415. switch( p.type ) {
  416. case TVec(size, _):
  417. switch( size ) {
  418. case 2:
  419. var v : h3d.Vector = curShader.getParamValue(p.index);
  420. bufLoader.loadVec2(v);
  421. case 3:
  422. var v : h3d.Vector = curShader.getParamValue(p.index);
  423. bufLoader.loadVec3(v);
  424. case 4:
  425. var v : h3d.Vector4 = curShader.getParamValue(p.index);
  426. bufLoader.loadVec4(v);
  427. }
  428. case TFloat:
  429. bufLoader.loadFloat(curShader.getParamFloatValue(p.index));
  430. case TMat4:
  431. var m : h3d.Matrix = curShader.getParamValue(p.index);
  432. bufLoader.loadMatrix(m);
  433. default:
  434. throw "Unsupported batch type "+p.type;
  435. }
  436. p = p.next;
  437. }
  438. needUpload = true;
  439. }
  440. override function emit(ctx:RenderContext) {
  441. if( instanceCount == 0 ) return;
  442. var p = dataPasses;
  443. while( p != null ) {
  444. var pass = p.pass;
  445. // check that the pass is still enable
  446. var material = materials[p.matIndex];
  447. if( material != null && material.getPass(pass.name) != null )
  448. emitPass(ctx, p);
  449. p = p.next;
  450. }
  451. }
  452. function emitPass(ctx : RenderContext, p : BatchData) {
  453. for( i => buf in p.buffers )
  454. ctx.emitPass(p.pass, this).index = i | (p.matIndex << 16);
  455. }
  456. override function draw(ctx:RenderContext) {
  457. var p = dataPasses;
  458. while( true ) {
  459. if( p.pass == ctx.drawPass.pass ) {
  460. var bufferIndex = ctx.drawPass.index & 0xFFFF;
  461. if ( storageBufferEnabled() )
  462. p.shader.Batch_StorageBuffer = p.buffers[bufferIndex];
  463. else
  464. p.shader.Batch_Buffer = p.buffers[bufferIndex];
  465. if( p.instanceBuffers == null )
  466. setPassCommand(p, bufferIndex);
  467. else
  468. instanced.commands = p.instanceBuffers[bufferIndex];
  469. break;
  470. }
  471. p = p.next;
  472. }
  473. ctx.uploadParams();
  474. var prev = ctx.drawPass.index;
  475. ctx.drawPass.index >>= 16;
  476. super.draw(ctx);
  477. ctx.drawPass.index = prev;
  478. }
  479. function setPassCommand(p : BatchData, bufferIndex : Int) {
  480. var count = hxd.Math.imin( instanceCount - p.maxInstance * bufferIndex, p.maxInstance );
  481. instanced.setCommand(p.matIndex, curLod >= 0 ? curLod : 0, count);
  482. }
  483. function partsFromPrimitive(prim : h3d.prim.MeshPrimitive) {
  484. var hmd = Std.downcast(prim, h3d.prim.HMDModel);
  485. if ( hmd == null )
  486. return false;
  487. if ( primitiveSubParts == null ) {
  488. primitiveSubParts = [];
  489. for ( m in 0...materials.length ) {
  490. var primitiveSubPart = new MeshBatchPart();
  491. primitiveSubPart.indexStart = hmd.getMaterialIndexStart(m, 0);
  492. primitiveSubPart.indexCount = hmd.getMaterialIndexCount(m, 0);
  493. primitiveSubPart.lodIndexCount = [for (i in 0...hmd.lodCount() ) hmd.getMaterialIndexCount(m, i)];
  494. primitiveSubPart.lodIndexStart = [for (i in 0...hmd.lodCount() ) hmd.getMaterialIndexStart(m, i) ];
  495. primitiveSubPart.lodConfig = hmd.getLodConfig();
  496. primitiveSubPart.baseVertex = 0;
  497. primitiveSubPart.bounds = hmd.getBounds();
  498. primitiveSubParts.push(primitiveSubPart);
  499. }
  500. }
  501. return true;
  502. }
  503. override function addBoundsRec( b : h3d.col.Bounds, relativeTo: h3d.Matrix ) {
  504. var old = primitive;
  505. primitive = null;
  506. super.addBoundsRec(b, relativeTo);
  507. primitive = old;
  508. if( primitive == null || flags.has(FIgnoreBounds) )
  509. return;
  510. // already transformed in absolute
  511. var bounds = primitive.getBounds();
  512. if( relativeTo == null )
  513. b.add(bounds);
  514. else
  515. b.addTransform(bounds, relativeTo);
  516. }
  517. override function onRemove() {
  518. super.onRemove();
  519. cleanPasses();
  520. }
  521. public function disposeBuffers() {
  522. if( instanceCount == 0 ) return;
  523. var p = dataPasses;
  524. var alloc = hxd.impl.Allocator.get();
  525. while( p != null ) {
  526. for ( b in p.buffers )
  527. alloc.disposeBuffer(b);
  528. p.buffers.resize(0);
  529. p = p.next;
  530. }
  531. }
  532. function cleanPasses() {
  533. while( dataPasses != null ) {
  534. dataPasses.clean();
  535. dataPasses = dataPasses.next;
  536. }
  537. if( instanced.commands != null )
  538. instanced.commands.dispose();
  539. primitiveSubBytes = null;
  540. shadersChanged = true;
  541. }
  542. }
  543. class BatchData {
  544. public var paramsCount : Int;
  545. public var maxInstance : Int;
  546. public var matIndex : Int;
  547. public var indexCount : Int;
  548. public var indexStart : Int;
  549. public var instanceBuffers : Array<h3d.impl.InstanceBuffer>;
  550. public var buffers : Array<h3d.Buffer> = [];
  551. public var bufferFormat : hxd.BufferFormat;
  552. public var data : hxd.FloatBuffer;
  553. public var params : hxsl.RuntimeShader.AllocParam;
  554. public var shader : hxsl.BatchShader;
  555. public var shaders : Array<hxsl.Shader>;
  556. public var pass : h3d.mat.Pass;
  557. public var next : BatchData;
  558. public function new() {
  559. }
  560. public function clean() {
  561. var alloc = hxd.impl.Allocator.get();
  562. pass.removeShader(shader);
  563. for( b in buffers )
  564. alloc.disposeBuffer(b);
  565. if( instanceBuffers != null ) {
  566. for( b in instanceBuffers )
  567. b.dispose();
  568. }
  569. alloc.disposeFloats(data);
  570. }
  571. }
  572. class MeshBatchPart {
  573. public var indexStart : Int;
  574. public var indexCount : Int;
  575. public var lodIndexStart : Array<Int>;
  576. public var lodIndexCount : Array<Int>;
  577. public var lodConfig : Array<Float>;
  578. public var baseVertex : Int;
  579. public var bounds : h3d.col.Bounds;
  580. public function new() {
  581. }
  582. public function clone() {
  583. var cl = new MeshBatchPart();
  584. cl.indexStart = indexStart;
  585. cl.indexCount = indexCount;
  586. cl.lodIndexStart = lodIndexStart;
  587. cl.lodIndexCount = lodIndexCount;
  588. cl.lodConfig = lodConfig;
  589. cl.baseVertex = baseVertex;
  590. cl.bounds = bounds;
  591. return cl;
  592. }
  593. }