Copy.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package h3d.pass;
  2. #if macro
  3. class Copy {
  4. public static function run( from : h3d.mat.Texture, to : h3d.mat.Texture, ?blend : h3d.mat.BlendMode, ?pass : h3d.mat.Pass ) {
  5. throw "assert";
  6. }
  7. }
  8. #else
  9. private class ArrayCopyShader extends h3d.shader.ScreenShader {
  10. static var SRC = {
  11. @param var texture : Sampler2DArray;
  12. @param var layer : Int;
  13. function fragment() {
  14. pixelColor = texture.get(vec3(calculatedUV, layer));
  15. }
  16. }
  17. }
  18. class ArrayCopy extends ScreenFx<ArrayCopyShader> {
  19. public function new() {
  20. super(new ArrayCopyShader());
  21. }
  22. public function apply( from : h3d.mat.TextureArray, fromLayer : Int, to, ?blend : h3d.mat.BlendMode, ?customPass : h3d.mat.Pass, ?layer : Int) {
  23. if( to != null )
  24. engine.pushTarget(to, layer != null ? layer : 0);
  25. shader.texture = from;
  26. shader.layer = fromLayer;
  27. if( customPass != null ) {
  28. if( blend != null ) customPass.setBlendMode(blend);
  29. var h = @:privateAccess customPass.shaders;
  30. while( h.next != null )
  31. h = h.next;
  32. h.next = @:privateAccess pass.shaders;
  33. var old = pass;
  34. pass = customPass;
  35. render();
  36. pass = old;
  37. h.next = null;
  38. } else {
  39. pass.setBlendMode(blend == null ? None : blend);
  40. render();
  41. }
  42. shader.texture = null;
  43. shader.layer = 0;
  44. if( to != null )
  45. engine.popTarget();
  46. }
  47. public static function run( from : h3d.mat.TextureArray, fromLayer : Int, to : h3d.mat.Texture, ?blend : h3d.mat.BlendMode, ?pass : h3d.mat.Pass, ?layer : Int ) {
  48. var engine = h3d.Engine.getCurrent();
  49. if( to != null && from != null && (blend == null || blend == None) && pass == null && engine.driver.copyTexture(from, to) )
  50. return;
  51. var inst : ArrayCopy = @:privateAccess engine.resCache.get(ArrayCopy);
  52. if( inst == null ) {
  53. inst = new ArrayCopy();
  54. @:privateAccess engine.resCache.set(ArrayCopy, inst);
  55. }
  56. return inst.apply(from, fromLayer, to, blend, pass, layer);
  57. }
  58. }
  59. private class CopyShader extends h3d.shader.ScreenShader {
  60. static var SRC = {
  61. @param var texture : Sampler2D;
  62. function fragment() {
  63. pixelColor = texture.get(calculatedUV);
  64. }
  65. }
  66. }
  67. class Copy extends ScreenFx<CopyShader> {
  68. public function new() {
  69. super(new CopyShader());
  70. }
  71. public function apply( from, to, ?blend : h3d.mat.BlendMode, ?customPass : h3d.mat.Pass, ?layer :Int) {
  72. if( to != null )
  73. engine.pushTarget(to, layer != null ? layer : 0);
  74. shader.texture = from;
  75. if( customPass != null ) {
  76. if( blend != null ) customPass.setBlendMode(blend);
  77. var h = @:privateAccess customPass.shaders;
  78. while( h.next != null )
  79. h = h.next;
  80. h.next = @:privateAccess pass.shaders;
  81. var old = pass;
  82. pass = customPass;
  83. render();
  84. pass = old;
  85. h.next = null;
  86. } else {
  87. pass.setBlendMode(blend == null ? None : blend);
  88. render();
  89. }
  90. shader.texture = null;
  91. if( to != null )
  92. engine.popTarget();
  93. }
  94. public static function run( from : h3d.mat.Texture, to : h3d.mat.Texture, ?blend : h3d.mat.BlendMode, ?pass : h3d.mat.Pass, ?layer : Int ) {
  95. var engine = h3d.Engine.getCurrent();
  96. if( to != null && from != null && (blend == null || blend == None) && pass == null && layer == null && engine.driver.copyTexture(from, to) )
  97. return;
  98. var inst : Copy = @:privateAccess engine.resCache.get(Copy);
  99. if( inst == null ) {
  100. inst = new Copy();
  101. @:privateAccess engine.resCache.set(Copy, inst);
  102. }
  103. return inst.apply(from, to, blend, pass, layer);
  104. }
  105. }
  106. #end