2
0

RenderTexture2D.cs 5.5 KB

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