TextureCache.hx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package h3d.impl;
  2. class TextureCache {
  3. var cache : Array<h3d.mat.Texture>;
  4. var position : Int = 0;
  5. var defaultDepthBuffer : h3d.mat.Texture;
  6. var ctx : h3d.impl.RenderContext;
  7. public var defaultFormat : hxd.PixelFormat;
  8. public function new(ctx) {
  9. this.ctx = ctx;
  10. cache = [];
  11. var engine = h3d.Engine.getCurrent();
  12. defaultFormat = h3d.mat.Texture.nativeFormat;
  13. defaultDepthBuffer = h3d.mat.Texture.getDefaultDepth();
  14. }
  15. public inline function get( index = 0 ) {
  16. return cache[index];
  17. }
  18. public function getNamed( name : String ) {
  19. for( i in 0...position )
  20. if( cache[i].name == name )
  21. return cache[i];
  22. return null;
  23. }
  24. public function set( t, index ) {
  25. cache[index] = t;
  26. }
  27. public function begin() {
  28. // dispose extra textures we didn't use in previous run
  29. while( cache.length > position ) {
  30. var t = cache.pop();
  31. if( t != null ) t.dispose();
  32. }
  33. position = 0;
  34. }
  35. function lookupTarget( name, width, height, format, isCube ) {
  36. var t = cache[position];
  37. // look for a suitable candidate
  38. for( i in position+1...cache.length ) {
  39. var t2 = cache[i];
  40. if( t2 != null && !t2.isDisposed() && t2.width == width && t2.height == height && t2.format == format && isCube == t2.flags.has(Cube) ) {
  41. // swap
  42. cache[position] = t2;
  43. cache[i] = t;
  44. return t2;
  45. }
  46. }
  47. // same name, most likely resolution changed, dispose before allocating new
  48. if( t != null && t.name == name ) {
  49. t.dispose();
  50. t = null;
  51. }
  52. var flags : Array<h3d.mat.Data.TextureFlags> = [Target];
  53. if( isCube ) flags.push(Cube);
  54. var newt = new h3d.mat.Texture(width, height, flags, format);
  55. // make the texture disposable if we're out of memory
  56. newt.realloc = function() {};
  57. if( t != null )
  58. cache.insert(position,newt);
  59. else
  60. cache[position] = newt;
  61. return newt;
  62. }
  63. public function allocTarget( name : String, width : Int, height : Int, defaultDepth=true, ?format:hxd.PixelFormat, isCube = false ) {
  64. var t = cache[position];
  65. if( format == null ) format = defaultFormat;
  66. if( t == null || t.isDisposed() || t.width != width || t.height != height || t.format != format || isCube != t.flags.has(Cube) )
  67. t = lookupTarget(name,width,height,format,isCube);
  68. t.depthBuffer = defaultDepth ? defaultDepthBuffer : null;
  69. t.setName(name);
  70. position++;
  71. return t;
  72. }
  73. public function allocTargetScale( name : String, scale : Float, defaultDepth=true, ?format:hxd.PixelFormat ) {
  74. var e = h3d.Engine.getCurrent();
  75. return allocTarget(name, Math.ceil(e.width * scale), Math.ceil(e.height * scale), defaultDepth, format);
  76. }
  77. public function allocTileTarget( name : String, tile : h2d.Tile, defaultDepth=false, ?format:hxd.PixelFormat ) {
  78. return allocTarget( name, tile.iwidth, tile.iheight, defaultDepth, format );
  79. }
  80. public function dispose() {
  81. for( t in cache )
  82. t.dispose();
  83. cache = [];
  84. }
  85. }