Advertisement
Guest User

Untitled

a guest
Mar 28th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Windows.Threading;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. namespace BOSS.ScreeN.BlueSkyEngine.Core
  10. {
  11. /// <summary>
  12. /// Extensions to the Microsoft.Xna.Framework.Content.ContentManager class.
  13. /// </summary>
  14. static class CContentManager
  15. {
  16.  
  17. /// <summary>
  18. /// Loads the asset asynchronously on another thread.
  19. /// Only one type of asset can be loaded, but can be multiple instances of the same type.
  20. /// </summary>
  21. /// <typeparam name="T">The type of the asset to load</typeparam>
  22. /// <param name="contentManager">The content manager that will load the asset</param>
  23. /// <param name="assetName">Array containint the paths and names of the assets (without the extension) relative to the root directory of the content manager</param>
  24. /// <param name="action">Callback that is called when the asset is loaded</param>
  25. public static void Load<T>(this ContentManager contentManager, List<string> assetName, Action<List<T>> action)
  26. {
  27.  
  28. ThreadPool.QueueUserWorkItem((s) =>
  29. {
  30.  
  31. IGraphicsDeviceService serv = contentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
  32.  
  33. List<T> assests = new List<T>();
  34. //load assets
  35. for (int i = 0; i < assetName.Count; i++)
  36. {
  37.  
  38. assests.Add(contentManager.Load<T>(assetName[i]));
  39. }
  40. //report done
  41. if (action != null)
  42. {
  43. Dispatcher.CurrentDispatcher.Invoke(action, assests);
  44.  
  45. }
  46.  
  47. });
  48.  
  49. }
  50. /// <summary>
  51. /// Load single asset into the content manager, using a worker thread
  52. /// </summary>
  53. /// <typeparam name="T"></typeparam>
  54. /// <param name="contentManager"></param>
  55. /// <param name="assetName"></param>
  56. /// <param name="action"></param>
  57. public static void Load<T>(this ContentManager contentManager, string assetName, Action<T> action)
  58. {
  59. ThreadPool.QueueUserWorkItem((s) =>
  60. {
  61.  
  62. T assests;
  63. //load assets
  64.  
  65. assests = contentManager.Load<T>(assetName);
  66.  
  67. //report done
  68. if (action != null)
  69. {
  70. Dispatcher.CurrentDispatcher.Invoke(action, assests);
  71.  
  72. }
  73.  
  74. });
  75. }
  76. /// <summary>
  77. /// Load a texture into the content manager with a worker thread, using Cpu mipmap generation
  78. /// </summary>
  79. /// <param name="contentManager"></param>
  80. /// <param name="assetName"></param>
  81. /// <param name="action"></param>
  82. public static void LoadTexture(this ContentManager contentManager, string assetName, Action<Texture2D> action)
  83. {
  84. ThreadPool.QueueUserWorkItem((s) =>
  85. {
  86.  
  87. Texture2D assest;
  88. //load assets
  89.  
  90. assest = BuildMipMapCPU(contentManager.Load<Texture2D>(assetName));
  91.  
  92. //report done
  93. if (action != null)
  94. {
  95. Dispatcher.CurrentDispatcher.Invoke(action, assest);
  96.  
  97. }
  98.  
  99. });
  100. }
  101. /// <summary>
  102. /// Load textures into the content manager with a worker thread, using Cpu mipmap generation
  103. /// </summary>
  104. /// <param name="contentManager"></param>
  105. /// <param name="assetName"></param>
  106. /// <param name="action"></param>
  107. public static void LoadTextures(this ContentManager contentManager, List<string> assetName, Action<List<Texture2D>> action)
  108. {
  109. lock (Threading.BackgroundContext)
  110. {
  111. ThreadPool.QueueUserWorkItem((s) =>
  112. {
  113. IGraphicsDeviceService serv = contentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
  114.  
  115. List<Texture2D> assests = new List<Texture2D>();
  116. //load assets
  117.  
  118. for (int i = 0; i < assetName.Count; i++)
  119. assests.Add(BuildMipMapCPU(contentManager.Load<Texture2D>(assetName[i])));
  120.  
  121. //report done
  122. if (action != null)
  123. {
  124. Dispatcher.CurrentDispatcher.Invoke(action, assests);
  125.  
  126. }
  127.  
  128. });
  129. }
  130. }
  131. /// <summary>
  132. /// generates mipmap for a texture
  133. /// </summary>
  134. /// <param name="original"></param>
  135. /// <returns></returns>
  136. private static Texture2D BuildMipMapCPU(Texture2D original)
  137. {
  138. return CMipMapGenerator.AddMipMaps(original);
  139. }
  140.  
  141. /// <summary>
  142. /// Generates a mipmap for a texture with the gpu, using rendertargets, might not work in all graphics cards
  143. /// </summary>
  144. /// <param name="contentManager"></param>
  145. /// <param name="original"></param>
  146. /// <param name="gd"></param>
  147. /// <param name="sp"></param>
  148. /// <param name="rt"></param>
  149. /// <returns></returns>
  150. public static Texture2D BuildMipMapGPU(this ContentManager contentManager, Texture2D original, GraphicsDevice gd,
  151. SpriteBatch sp, RenderTarget2D rt)
  152. {
  153. // create mip mapped texture
  154. SamplerState oldSS = gd.SamplerStates[0];
  155. RasterizerState oldrs = gd.RasterizerState;
  156.  
  157. SamplerState newss = SamplerState.LinearClamp; // todo which is best?
  158.  
  159. gd.SetRenderTarget(rt);
  160.  
  161. sp.Begin(SpriteSortMode.Immediate, BlendState.Opaque, newss, DepthStencilState.None, RasterizerState.CullNone, effect: null);
  162. sp.Draw(original, new Vector2(0, 0), Color.White);
  163. sp.End();
  164.  
  165. gd.SetRenderTarget(null);
  166.  
  167. gd.DepthStencilState = DepthStencilState.Default;
  168. gd.BlendState = BlendState.Opaque;
  169. gd.SamplerStates[0] = oldSS;
  170. gd.RasterizerState = oldrs;
  171.  
  172. // since rendertarget textures are volatile (contents get lost on device) we have to copy data in new texture
  173. Texture2D mergedTexture = new Texture2D(gd, original.Width, original.Height, true, SurfaceFormat.Color);
  174. Color[] content = new Color[original.Width * original.Height];
  175.  
  176. for (int i = 0; i < rt.LevelCount; i++)
  177. {
  178. int n = rt.Width * rt.Height / ((1 << i) * (1 << i));
  179. rt.GetData<Color>(i, null, content, 0, n);
  180. mergedTexture.SetData<Color>(i, null, content, 0, n);
  181. }
  182.  
  183. return mergedTexture;
  184.  
  185. }
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement