TextureCache.hx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package h3d.impl;
  2. class TextureCache {
  3. var cache : Array<h3d.mat.Texture>;
  4. var position : Int = 0;
  5. var frame : Int;
  6. var defaultDepthBuffer : h3d.mat.DepthBuffer;
  7. public var defaultFormat : hxd.PixelFormat;
  8. public function new() {
  9. cache = [];
  10. var engine = h3d.Engine.getCurrent();
  11. defaultFormat = h3d.mat.Texture.nativeFormat;
  12. defaultDepthBuffer = h3d.mat.DepthBuffer.getDefault();
  13. }
  14. public inline function get( index = 0 ) {
  15. return cache[index];
  16. }
  17. public function set( t, index ) {
  18. cache[index] = t;
  19. }
  20. public function begin( ctx : h3d.impl.RenderContext ) {
  21. if( frame != ctx.frame ) {
  22. // dispose extra textures we didn't use
  23. while( cache.length > position ) {
  24. var t = cache.pop();
  25. if( t != null ) t.dispose();
  26. }
  27. frame = ctx.frame;
  28. position = 0;
  29. }
  30. }
  31. public function allocTarget( name : String, ctx : h3d.impl.RenderContext, width : Int, height : Int, defaultDepth=true, ?format:hxd.PixelFormat ) {
  32. begin(ctx);
  33. var t = cache[position];
  34. if( format == null ) format = defaultFormat;
  35. if( t == null || t.isDisposed() || t.width != width || t.height != height || t.format != format ) {
  36. if( t != null ) t.dispose();
  37. var flags : Array<h3d.mat.Data.TextureFlags> = [Target];
  38. t = new h3d.mat.Texture(width, height, flags, format);
  39. cache[position] = t;
  40. }
  41. t.depthBuffer = defaultDepth ? defaultDepthBuffer : null;
  42. t.setName(name);
  43. position++;
  44. return t;
  45. }
  46. public function dispose() {
  47. for( t in cache )
  48. t.dispose();
  49. cache = [];
  50. frame = -1;
  51. }
  52. }