Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GpuTextureSerializer2.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using SiliconStudio.Core.Diagnostics;
5 using SiliconStudio.Core.Serialization;
6 using SiliconStudio.Core.Serialization.Contents;
7 
8 namespace SiliconStudio.Paradox.Graphics.Data
9 {
10  public class GpuTextureSerializer2<T> : ContentSerializerBase<T> where T : Texture
11  {
12  private readonly GraphicsDevice graphicsDevice;
13 
14  public override Type SerializationType
15  {
16  get
17  {
18  return typeof(Image);
19  }
20  }
21 
22  public GpuTextureSerializer2(GraphicsDevice graphicsDevice)
23  {
24  this.graphicsDevice = graphicsDevice;
25  }
26 
27  public override void Serialize(ContentSerializerContext context, SerializationStream stream, ref T texture)
28  {
29  if (context.Mode == ArchiveMode.Serialize)
30  throw new NotImplementedException();
31 
32  var assetManager = context.AssetManager;
33  var url = context.Url;
34 
35  using (var textureData = Image.Load(stream.NativeStream))
36  {
37  try
38  {
39  texture = (T)Texture.New(graphicsDevice, textureData);
40 
41  // Setup reload callback (reload from asset manager)
42  texture.Reload = (graphicsResource) =>
43  {
44  // TODO: Avoid loading/unloading the same data
45  var textureDataReloaded = assetManager.Load<Image>(url);
46  ((Texture)graphicsResource).Recreate(textureDataReloaded.ToDataBox());
47  assetManager.Unload(textureDataReloaded);
48  };
49  }
50  catch (Exception ex)
51  {
52  GlobalLogger.GetLogger("GPUTexture").Error("Unable to load Texture {0}. See Debug for more information", ex, context.Url);
53  }
54  }
55  }
56 
57  public override object Construct(SiliconStudio.Core.Serialization.Contents.ContentSerializerContext context)
58  {
59  return null;
60  }
61  }
62 }
Provides method to instantiate an image 1D/2D/3D supporting TextureArray and mipmaps on the CPU or to...
Definition: Image.cs:88
static Texture New(GraphicsDevice device, Image image, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Loads a texture from a stream.
Definition: Texture.cs:646
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
Base class for implementation of SerializationStream.
override void Serialize(ContentSerializerContext context, SerializationStream stream, ref T texture)
An error message (level 4).
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
Definition: ArchiveMode.cs:8
override object Construct(SiliconStudio.Core.Serialization.Contents.ContentSerializerContext context)
Base class for texture resources.
Definition: Texture.cs:38