Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Buffer.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 using SiliconStudio.Core;
7 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
8 using OpenTK.Graphics.ES30;
9 using BufferUsageHint = OpenTK.Graphics.ES30.BufferUsage;
10 #else
11 using OpenTK.Graphics.OpenGL;
12 #endif
13 
14 namespace SiliconStudio.Paradox.Graphics
15 {
16  public partial class Buffer
17  {
18  internal BufferTarget bufferTarget;
19  internal BufferUsageHint bufferUsageHint;
20 
21 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
22  // Special case: ConstantBuffer are faked with a byte array on OpenGL ES 2.0.
23  internal IntPtr StagingData { get; set; }
24 #endif
25 
26  /// <summary>
27  /// Initializes a new instance of the <see cref="Buffer" /> class.
28  /// </summary>
29  /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
30  /// <param name="description">The description.</param>
31  /// <param name="bufferFlags">Type of the buffer.</param>
32  /// <param name="viewFormat">The view format.</param>
33  /// <param name="dataPointer">The data pointer.</param>
34  protected Buffer(GraphicsDevice device, BufferDescription description, BufferFlags bufferFlags, PixelFormat viewFormat, IntPtr dataPointer)
35  : base(device)
36  {
37  Description = description;
38  BufferFlags = bufferFlags;
39  ViewFormat = viewFormat;
40 
41  Recreate(dataPointer);
42  }
43 
44  public void Recreate(IntPtr dataPointer)
45  {
46  if ((BufferFlags & BufferFlags.VertexBuffer) == BufferFlags.VertexBuffer)
47  {
48  bufferTarget = BufferTarget.ArrayBuffer;
49  }
50  else if ((BufferFlags & BufferFlags.IndexBuffer) == BufferFlags.IndexBuffer)
51  {
52  bufferTarget = BufferTarget.ElementArrayBuffer;
53  }
54 
55  if ((BufferFlags & BufferFlags.ConstantBuffer) == BufferFlags.ConstantBuffer)
56  {
57 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
58  // Special case: ConstantBuffer are faked with a byte array on OpenGL ES 2.0.
59  StagingData = Marshal.AllocHGlobal(Description.SizeInBytes);
60 #else
61  bufferTarget = BufferTarget.UniformBuffer;
62 #endif
63  }
64  else if (Description.Usage == GraphicsResourceUsage.Dynamic)
65  {
66 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
67  // OpenGL ES might not always support MapBuffer (TODO: Use MapBufferOES if available)
68  StagingData = Marshal.AllocHGlobal(Description.SizeInBytes);
69 #endif
70  }
71 
72  Init(dataPointer);
73  }
74 
75  /// <inheritdoc/>
76  protected internal override bool OnRecreate()
77  {
78  base.OnRecreate();
79 
80  if (Description.Usage == GraphicsResourceUsage.Immutable
81  || Description.Usage == GraphicsResourceUsage.Default)
82  return false;
83 
84  Recreate(IntPtr.Zero);
85 
86  return true;
87  }
88 
89  /// <inheritdoc/>
90  protected override void Destroy()
91  {
92 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
93  if (StagingData != IntPtr.Zero)
94  {
95  Marshal.FreeHGlobal(StagingData);
96  StagingData = IntPtr.Zero;
97  }
98 #endif
99 
100  using (GraphicsDevice.UseOpenGLCreationContext())
101  {
102  GL.DeleteBuffers(1, ref resourceId);
103  }
104 
105  resourceId = 0;
106 
107  base.Destroy();
108  }
109 
110  protected void Init(IntPtr dataPointer)
111  {
112 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
113  if ((Description.BufferFlags & BufferFlags.ConstantBuffer) == BufferFlags.ConstantBuffer
114  || Description.Usage == GraphicsResourceUsage.Dynamic)
115  {
116  if (dataPointer != IntPtr.Zero)
117  {
118  // Special case: ConstantBuffer are faked with a byte array on OpenGL ES 2.0.
119  unsafe
120  {
121  Utilities.CopyMemory(StagingData, dataPointer, Description.SizeInBytes);
122  }
123  }
124  return;
125  }
126 #endif
127  switch (Description.Usage)
128  {
129  case GraphicsResourceUsage.Default:
130  case GraphicsResourceUsage.Immutable:
131  bufferUsageHint = BufferUsageHint.StaticDraw;
132  break;
133  case GraphicsResourceUsage.Dynamic:
134  case GraphicsResourceUsage.Staging:
135  bufferUsageHint = BufferUsageHint.DynamicDraw;
136  break;
137  default:
138  throw new ArgumentOutOfRangeException("description.Usage");
139  }
140 
141  using (var creationContext = GraphicsDevice.UseOpenGLCreationContext())
142  {
143  // If we're on main context, unbind VAO before binding context.
144  // It will be bound again on next draw.
145  if (!creationContext.UseDeviceCreationContext)
146  GraphicsDevice.UnbindVertexArrayObject();
147 
148  GL.GenBuffers(1, out resourceId);
149  GL.BindBuffer(bufferTarget, resourceId);
150  GL.BufferData(bufferTarget, (IntPtr)Description.SizeInBytes, dataPointer, bufferUsageHint);
151  GL.BindBuffer(bufferTarget, 0);
152  }
153  }
154  }
155 }
156 #endif
ComponentBase.Destroy() event.
GraphicsResourceUsage
Identifies expected resource use during rendering. The usage directly reflects whether a resource is ...
SiliconStudio.Paradox.Graphics.Buffer Buffer
Definition: BasicEffect.cs:15
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32