Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Texture1D.OpenGL.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 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL
4 using System;
5 using System.IO;
6 
7 using OpenTK.Graphics;
8 
9 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
10 using OpenTK.Graphics.ES30;
11 using PixelFormatGl = OpenTK.Graphics.ES30.PixelFormat;
12 #else
13 using OpenTK.Graphics.OpenGL;
14 using PixelFormatGl = OpenTK.Graphics.OpenGL.PixelFormat;
15 #endif
16 
17 namespace SiliconStudio.Paradox.Graphics
18 {
19  /// <summary>
20  /// Represents a 1D grid of texels.
21  /// </summary>
22  public partial class Texture1D
23  {
24 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
25  private const TextureTarget TextureTarget1D = TextureTarget.Texture2D;
26 #else
27  private const TextureTarget TextureTarget1D = TextureTarget.Texture1D;
28 #endif
29 
30  protected internal Texture1D(GraphicsDevice device, TextureDescription description1D, DataBox[] dataBox = null)
31  : base(device, description1D, ViewType.Full, 0, 0)
32  {
33  Target = TextureTarget1D;
34  }
35 
36  protected internal Texture1D(GraphicsDevice device, Texture1D texture) : base(device, texture, ViewType.Full, 0, 0)
37  {
38  Target = TextureTarget1D;
39  }
40 
41  public override Texture ToTexture(ViewType viewType, int arraySlice, int mipMapSlice)
42  {
43  // Exists since OpenGL 4.3
44  if (viewType != ViewType.Full || arraySlice != 0 || mipMapSlice != 0)
45  throw new NotImplementedException();
46 
47  return new Texture1D(GraphicsDevice, this);
48  }
49 
50  /// <summary>
51  /// Inits this instance with the specified texture datas.
52  /// </summary>
53  /// <param name="textureDatas">The texture datas.</param>
54  protected virtual void Init(DataRectangle[] textureDatas)
55  {
56  using (var creationContext = GraphicsDevice.UseOpenGLCreationContext())
57  {
58  PixelInternalFormat internalFormat;
59  PixelFormatGl format;
60  PixelType type;
61  int pixelSize;
62  bool compressed;
63  ConvertPixelFormat(GraphicsDevice, Description.Format, out internalFormat, out format, out type, out pixelSize, out compressed);
64 
65  InternalFormat = internalFormat;
66  FormatGl = format;
67  Type = type;
68 
69  int textureId;
70 
71  // If we're on main context, change internal states so that texture is bound again
72  if (!creationContext.UseDeviceCreationContext)
73  GraphicsDevice.UseTemporaryFirstTexture();
74 
75  GL.GenTextures(1, out textureId);
76  GL.BindTexture(TextureTarget1D, textureId);
77 
78  // No filtering on depth buffer
79  if (format == PixelFormatGl.DepthComponent)
80  {
81  GL.TexParameter(TextureTarget1D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
82  GL.TexParameter(TextureTarget1D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
83  GL.TexParameter(TextureTarget1D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
84  GL.TexParameter(TextureTarget1D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
85  }
86 
87  for (int i = 0; i < Description.MipLevels; ++i)
88  {
89  IntPtr data = IntPtr.Zero;
90  var width = CalculateMipSize(Description.Width, i);
91  if (textureDatas != null && i < textureDatas.Length)
92  {
93  if (textureDatas[i].Pitch != width * pixelSize)
94  throw new NotSupportedException("Can't upload texture with pitch in glTexImage1D."); // Might be possible, need to check API better.
95  data = textureDatas[i].DataPointer;
96  }
97 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
98  GL.TexImage2D(TextureTarget1D, i, internalFormat, width, 1, 0, format, type, data);
99 #else
100  GL.TexImage1D(TextureTarget1D, i, internalFormat, width, 0, format, type, data);
101 #endif
102  }
103  GL.BindTexture(TextureTarget1D, 0);
104 
105  resourceId = textureId;
106  }
107  }
108 
109  /// <summary>
110  /// Computes the mip level count.
111  /// </summary>
112  /// <param name="mipLevels">The mip levels.</param>
113  /// <returns></returns>
114  protected int ComputeLevelCount(int mipLevels)
115  {
116  if (mipLevels > 0)
117  return mipLevels;
118  return (int)Math.Ceiling(Math.Log(Description.Width) / Math.Log(2.0));
119  }
120  }
121 }
122 
123 #endif
_In_ size_t pixelSize
Definition: DirectXTexP.h:116
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.
ViewType
Defines how a view is selected from a resource.
Definition: ViewType.cs:31
Same as Deferred mode, except sprites are sorted by texture prior to drawing. This can improve perfor...
Gets a texture view for the whole texture for all mips/arrays dimensions.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
The texture dimension is 1D.
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32