RenderTexture2D.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. /// <summary>
  9. /// A two dimensional render texture. Allows rendering to be performed into a 2D texture which can then be read, as
  10. /// opposed to rendering directly into the frame buffer.
  11. /// </summary>
  12. public class RenderTexture2D : RenderTexture
  13. {
  14. /// <summary>
  15. /// Creates a new 2D render texture.
  16. /// </summary>
  17. /// <param name="format">Pixel format of the texture. Format must be a valid uncompressed color format.</param>
  18. /// <param name="width">Width of the texture in pixels.</param>
  19. /// <param name="height">Height of the texture in pixels.</param>
  20. /// <param name="numSamples">Number of samples contained per pixel.</param>
  21. /// <param name="gammaCorrection">Determines should the pixels written on the texture be gamma corrected.</param>
  22. /// <param name="createDepth">Should the render texture also contain a depth/stencil buffer.</param>
  23. /// <param name="depthStencilFormat">Format of the depth/stencil buffer, if <paramref name="createDepth"/> is
  24. /// enabled. Format must be a valid depth/stencil format.</param>
  25. public RenderTexture2D(PixelFormat format, int width, int height, int numSamples = 1,
  26. bool gammaCorrection = false, bool createDepth = true, PixelFormat depthStencilFormat = PixelFormat.D24S8)
  27. {
  28. Internal_CreateDetailed(this, format, width, height, numSamples, gammaCorrection, createDepth, depthStencilFormat);
  29. }
  30. /// <summary>
  31. /// Creates a new 2D render texture using existing textures as render destinations.
  32. /// </summary>
  33. /// <param name="colorSurface">Color texture to render color data to.</param>
  34. /// <param name="depthStencilSurface">Optional depth/stencil texture to render depth/stencil data to.</param>
  35. public RenderTexture2D(Texture2D colorSurface, Texture2D depthStencilSurface = null)
  36. {
  37. IntPtr[] colorSurfaceInstances = new IntPtr[1];
  38. colorSurfaceInstances[0] = colorSurface.GetCachedPtr();
  39. IntPtr depthStencilInstance = IntPtr.Zero;
  40. if (depthStencilSurface != null)
  41. depthStencilInstance = depthStencilSurface.GetCachedPtr();
  42. Internal_Create(this, colorSurfaceInstances, depthStencilInstance);
  43. }
  44. /// <summary>
  45. /// Creates a new 2D render texture using one or multiple color textures and a depth/stencil texture.
  46. /// </summary>
  47. /// <param name="colorSurfaces">Color texture(s) to render color data to. </param>
  48. /// <param name="depthStencilSurface">>Optional depth/stencil texture to render depth/stencil data to.</param>
  49. public RenderTexture2D(Texture2D[] colorSurfaces, Texture2D depthStencilSurface = null)
  50. {
  51. IntPtr[] colorSurfaceInstances = new IntPtr[colorSurfaces.Length];
  52. for(int i = 0; i < colorSurfaces.Length; i++)
  53. colorSurfaceInstances[i] = colorSurfaces[i] != null ? colorSurfaces[i].GetCachedPtr() : IntPtr.Zero;
  54. IntPtr depthStencilInstance = IntPtr.Zero;
  55. if (depthStencilSurface != null)
  56. depthStencilInstance = depthStencilSurface.GetCachedPtr();
  57. Internal_Create(this, colorSurfaceInstances, depthStencilInstance);
  58. }
  59. /// <summary>
  60. /// Returns the primary color surface that contains rendered color data.
  61. /// </summary>
  62. public Texture2D colorSurface
  63. {
  64. get
  65. {
  66. Texture2D[] surfaces;
  67. Internal_GetColorSurfaces(mCachedPtr, out surfaces);
  68. return surfaces[0];
  69. }
  70. }
  71. /// <summary>
  72. /// Returns all of the color surfaces.
  73. /// </summary>
  74. public Texture2D[] colorSurfaces
  75. {
  76. get
  77. {
  78. Texture2D[] surfaces;
  79. Internal_GetColorSurfaces(mCachedPtr, out surfaces);
  80. return surfaces;
  81. }
  82. }
  83. /// <summary>
  84. /// Returns the depth/stencil surface that contains rendered depth and stencil data.
  85. /// </summary>
  86. public Texture2D depthStencilSurface
  87. {
  88. get
  89. {
  90. Texture2D surface;
  91. Internal_GetDepthStencilSurface(mCachedPtr, out surface);
  92. return surface;
  93. }
  94. }
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern void Internal_CreateDetailed(RenderTexture2D instance, PixelFormat format,
  97. int width, int height, int numSamples, bool gammaCorrection, bool createDepth, PixelFormat depthStencilFormat);
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. private static extern void Internal_Create(RenderTexture2D instance, IntPtr[] colorSurfaces, IntPtr depthStencilSurface);
  100. [MethodImpl(MethodImplOptions.InternalCall)]
  101. private static extern void Internal_GetColorSurfaces(IntPtr thisPtr, out Texture2D[] surfaces);
  102. [MethodImpl(MethodImplOptions.InternalCall)]
  103. private static extern void Internal_GetDepthStencilSurface(IntPtr thisPtr, out Texture2D surface);
  104. }
  105. }