Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Texture.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.Runtime.InteropServices;
6 
7 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
8 using OpenTK.Graphics.ES30;
9 using RenderbufferStorage = OpenTK.Graphics.ES30.RenderbufferInternalFormat;
10 using PixelFormatGl = OpenTK.Graphics.ES30.PixelFormat;
11 #if SILICONSTUDIO_PLATFORM_IOS
12 using ExtTextureFormatBgra8888 = OpenTK.Graphics.ES30.All;
13 using ImgTextureCompressionPvrtc = OpenTK.Graphics.ES30.All;
14 using OesPackedDepthStencil = OpenTK.Graphics.ES30.All;
15 #elif SILICONSTUDIO_PLATFORM_ANDROID
16 using ExtTextureFormatBgra8888 = OpenTK.Graphics.ES20.ExtTextureFormatBgra8888;
17 using OesCompressedEtc1Rgb8Texture = OpenTK.Graphics.ES20.OesCompressedEtc1Rgb8Texture;
18 #endif
19 #else
20 using System;
21 using OpenTK.Graphics.OpenGL;
22 using PixelFormatGl = OpenTK.Graphics.OpenGL.PixelFormat;
23 #endif
24 
25 namespace SiliconStudio.Paradox.Graphics
26 {
27  /// <summary>
28  /// Abstract class for all textures
29  /// </summary>
30  public partial class Texture
31  {
32  private RenderTarget cachedRenderTarget;
33 
34  internal SamplerState BoundSamplerState;
35 
36  public PixelInternalFormat InternalFormat { get; set; }
37  public PixelFormatGl FormatGl { get; set; }
38  public PixelType Type { get; set; }
39  public TextureTarget Target { get; set; }
40  public int DepthPitch { get; set; }
41  public int RowPitch { get; set; }
42 
43 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
44  public IntPtr StagingData { get; set; }
45 #endif
46 
47  public virtual void Recreate(DataBox[] dataBoxes = null)
48  {
49  throw new NotImplementedException();
50  }
51 
52  /// <inheritdoc/>
53  protected override void DestroyImpl()
54  {
55 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
56  if (StagingData != IntPtr.Zero)
57  {
58  Marshal.FreeHGlobal(StagingData);
59  StagingData = IntPtr.Zero;
60  }
61 #endif
62 
63  using (GraphicsDevice.UseOpenGLCreationContext())
64  {
65  if (Description.Usage == GraphicsResourceUsage.Staging)
66  {
67  GL.DeleteBuffers(1, ref resourceId);
68  }
69  else
70  {
71  GL.DeleteTextures(1, ref resourceId);
72  }
73  }
74 
75  resourceId = 0;
76 
77  base.DestroyImpl();
78  }
79 
80  public RenderTarget ToRenderTarget(ViewType viewType, int arraySlize, int mipSlice)
81  {
82  return new RenderTarget(GraphicsDevice, this, ViewType.Single, 0, 0);
83  }
84 
85  internal RenderTarget GetCachedRenderTarget()
86  {
87  if (cachedRenderTarget == null)
88  {
89  cachedRenderTarget = new RenderTarget(GraphicsDevice, this, ViewType.Single, 0, 0);
90  }
91 
92  return cachedRenderTarget;
93  }
94 
95  protected static void ConvertDepthFormat(GraphicsDevice graphicsDevice, PixelFormat requestedFormat, out RenderbufferStorage depthFormat, out RenderbufferStorage stencilFormat)
96  {
97  // Default: non-separate depth/stencil
98  stencilFormat = 0;
99 
100  switch (requestedFormat)
101  {
102  case PixelFormat.D16_UNorm:
103  depthFormat = RenderbufferStorage.DepthComponent16;
104  break;
105 #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
106  case PixelFormat.D24_UNorm_S8_UInt:
107  depthFormat = RenderbufferStorage.Depth24Stencil8;
108  break;
109  case PixelFormat.D32_Float:
110  depthFormat = RenderbufferStorage.DepthComponent32;
111  break;
112  case PixelFormat.D32_Float_S8X24_UInt:
113  depthFormat = RenderbufferStorage.Depth32fStencil8;
114  break;
115 #else
116  case PixelFormat.D24_UNorm_S8_UInt:
117  if (graphicsDevice.HasPackedDepthStencilExtension)
118  {
119  depthFormat = RenderbufferStorage.Depth24Stencil8;
120  }
121  else
122  {
123  depthFormat = graphicsDevice.HasDepth24 ? RenderbufferStorage.DepthComponent24 : RenderbufferStorage.DepthComponent16;
124  stencilFormat = RenderbufferStorage.StencilIndex8;
125  }
126  break;
127  case PixelFormat.D32_Float:
128  case PixelFormat.D32_Float_S8X24_UInt:
129  throw new NotSupportedException("Only 16 bits depth buffer or 24-8 bits depth-stencil buffer is supported on OpenGLES2");
130 #endif
131  default:
132  throw new NotImplementedException();
133  }
134  }
135 
136 
137  protected static void ConvertPixelFormat(GraphicsDevice graphicsDevice, PixelFormat inputFormat, out PixelInternalFormat internalFormat, out PixelFormatGl format, out PixelType type, out int pixelSize, out bool compressed)
138  {
139  compressed = false;
140 
141  switch (inputFormat)
142  {
143  case PixelFormat.R8G8B8A8_UNorm:
144  internalFormat = PixelInternalFormat.Rgba;
145  format = PixelFormatGl.Rgba;
146  type = PixelType.UnsignedByte;
147  pixelSize = 4;
148  break;
149  case PixelFormat.D16_UNorm:
150  internalFormat = PixelInternalFormat.Rgba;
151  format = PixelFormatGl.Rgba;
152  type = PixelType.UnsignedByte;
153  pixelSize = 2;
154  break;
155  case PixelFormat.A8_UNorm:
156 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
157  internalFormat = PixelInternalFormat.Alpha;
158  format = PixelFormatGl.Alpha;
159 #else
160  internalFormat = PixelInternalFormat.R8;
161  format = PixelFormatGl.Red;
162 #endif
163  type = PixelType.UnsignedByte;
164  pixelSize = 1;
165  break;
166  case PixelFormat.R8_UNorm:
167 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
168  internalFormat = PixelInternalFormat.Luminance;
169  format = PixelFormatGl.Luminance;
170 #else
171  internalFormat = PixelInternalFormat.R8;
172  format = PixelFormatGl.Red;
173 #endif
174  type = PixelType.UnsignedByte;
175  pixelSize = 1;
176  break;
177  case PixelFormat.B8G8R8A8_UNorm:
178 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
179  if (!graphicsDevice.HasExtTextureFormatBGRA8888)
180  throw new NotSupportedException();
181 
182  // It seems iOS and Android expects different things
183 #if SILICONSTUDIO_PLATFORM_IOS
184  internalFormat = PixelInternalFormat.Rgba;
185 #else
186  internalFormat = (PixelInternalFormat)ExtTextureFormatBgra8888.BgraExt;
187 #endif
188  format = (PixelFormatGl)ExtTextureFormatBgra8888.BgraExt;
189 #else
190  internalFormat = PixelInternalFormat.Rgba;
191  format = PixelFormatGl.Bgra;
192 #endif
193  type = PixelType.UnsignedByte;
194  pixelSize = 4;
195  break;
196 #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
197  case PixelFormat.R32_UInt:
198  internalFormat = PixelInternalFormat.R32ui;
199  format = PixelFormatGl.RedInteger;
200  type = PixelType.UnsignedInt;
201  pixelSize = 4;
202  break;
203  case PixelFormat.R16G16B16A16_Float:
204  internalFormat = PixelInternalFormat.Rgba16f;
205  format = PixelFormatGl.Rgba;
206  type = PixelType.HalfFloat;
207  pixelSize = 8;
208  break;
209  case PixelFormat.R32_Float:
210  internalFormat = PixelInternalFormat.R32f;
211  format = PixelFormatGl.Red;
212  type = PixelType.Float;
213  pixelSize = 4;
214  break;
215  case PixelFormat.R32G32_Float:
216  internalFormat = PixelInternalFormat.Rg32f;
217  format = PixelFormatGl.Rg;
218  type = PixelType.Float;
219  pixelSize = 8;
220  break;
221  case PixelFormat.R32G32B32_Float:
222  internalFormat = PixelInternalFormat.Rgb32f;
223  format = PixelFormatGl.Rgb;
224  type = PixelType.Float;
225  pixelSize = 12;
226  break;
227  case PixelFormat.R32G32B32A32_Float:
228  internalFormat = PixelInternalFormat.Rgba32f;
229  format = PixelFormatGl.Rgba;
230  type = PixelType.Float;
231  pixelSize = 16;
232  break;
233  // TODO: Temporary depth format (need to decide relation between RenderTarget1D and Texture1D)
234  case (PixelFormat)40:
235  internalFormat = PixelInternalFormat.DepthComponent32f;
236  format = PixelFormatGl.DepthComponent;
237  type = PixelType.Float;
238  pixelSize = 4;
239  break;
240 #endif
241 #if SILICONSTUDIO_PLATFORM_ANDROID
242  case PixelFormat.ETC1:
243  // TODO: Runtime check for extension?
244  internalFormat = (PixelInternalFormat)OesCompressedEtc1Rgb8Texture.Etc1Rgb8Oes;
245  format = (PixelFormatGl)OesCompressedEtc1Rgb8Texture.Etc1Rgb8Oes;
246  compressed = true;
247  pixelSize = 2;
248  type = PixelType.UnsignedByte;
249  break;
250 #elif SILICONSTUDIO_PLATFORM_IOS
251  case PixelFormat.PVRTC_4bpp_RGB:
252  internalFormat = (PixelInternalFormat)ImgTextureCompressionPvrtc.CompressedRgbPvrtc4Bppv1Img;
253  format = (PixelFormatGl)ImgTextureCompressionPvrtc.CompressedRgbPvrtc4Bppv1Img;
254  compressed = true;
255  pixelSize = 4;
256  type = PixelType.UnsignedByte;
257  break;
258  case PixelFormat.PVRTC_2bpp_RGB:
259  internalFormat = (PixelInternalFormat)ImgTextureCompressionPvrtc.CompressedRgbPvrtc2Bppv1Img;
260  format = (PixelFormatGl)ImgTextureCompressionPvrtc.CompressedRgbPvrtc2Bppv1Img;
261  compressed = true;
262  pixelSize = 2;
263  type = PixelType.UnsignedByte;
264  break;
265  case PixelFormat.PVRTC_4bpp_RGBA:
266  internalFormat = (PixelInternalFormat)ImgTextureCompressionPvrtc.CompressedRgbaPvrtc4Bppv1Img;
267  format = (PixelFormatGl)ImgTextureCompressionPvrtc.CompressedRgbaPvrtc4Bppv1Img;
268  compressed = true;
269  pixelSize = 4;
270  type = PixelType.UnsignedByte;
271  break;
272  case PixelFormat.PVRTC_2bpp_RGBA:
273  internalFormat = (PixelInternalFormat)ImgTextureCompressionPvrtc.CompressedRgbaPvrtc2Bppv1Img;
274  format = (PixelFormatGl)ImgTextureCompressionPvrtc.CompressedRgbaPvrtc2Bppv1Img;
275  compressed = true;
276  pixelSize = 2;
277  type = PixelType.UnsignedByte;
278  break;
279 #endif
280  default:
281  throw new InvalidOperationException("Unsupported texture format");
282  }
283  }
284 
285  private bool IsFlippedTexture()
286  {
287  return GraphicsDevice.BackBuffer.Texture == this || GraphicsDevice.DepthStencilBuffer.Texture == this;
288  }
289  }
290 }
291 
292 #endif
_In_ size_t pixelSize
Definition: DirectXTexP.h:116
GraphicsResourceUsage
Identifies expected resource use during rendering. The usage directly reflects whether a resource is ...
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...
Creates a render target buffer.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32