3 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL
6 using System.Runtime.InteropServices;
9 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
10 using OpenTK.Graphics.ES30;
12 using RenderbufferStorage = OpenTK.Graphics.ES30.RenderbufferInternalFormat;
14 using OpenTK.Graphics.OpenGL;
18 namespace SiliconStudio.
Paradox.Graphics
23 public partial class Texture2DBase
26 internal int ResouceIdStencil;
28 internal bool IsRenderbuffer
32 return (Description.Flags &
TextureFlags.ShaderResource) == 0
33 && (Description.Flags & TextureFlags.DepthStencil) ==
TextureFlags.DepthStencil;
37 protected internal Texture2DBase(GraphicsDevice device, TextureDescription description2D, TextureTarget textureTarget, DataBox[] dataBoxes = null,
bool initialize =
true) : base(device, description2D,
ViewType.
Full, 0, 0)
44 protected internal Texture2DBase(GraphicsDevice device, Texture2DBase texture) : base(device, texture,
ViewType.
Full, 0, 0)
46 this.Target = texture.Target;
47 this.resourceId = texture.ResourceId;
55 protected virtual void Init(DataBox[] textureDatas)
57 using (var creationContext = GraphicsDevice.UseOpenGLCreationContext())
60 if ((Description.Flags &
TextureFlags.ShaderResource) == 0)
62 if ((Description.Flags &
TextureFlags.DepthStencil) == TextureFlags.DepthStencil)
64 RenderbufferStorage depth, stencil;
65 ConvertDepthFormat(GraphicsDevice, Description.Format, out depth, out stencil);
68 GL.GenRenderbuffers(1, out resourceId);
69 GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, resourceId);
70 GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, depth, Description.Width, Description.Height);
71 if (OpenGLConvertExtensions.GetErrorCode() != ErrorCode.NoError)
72 throw new InvalidOperationException(
"Could not create render buffer");
77 GL.GenRenderbuffers(1, out ResouceIdStencil);
78 GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, ResouceIdStencil);
79 GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, stencil, Description.Width, Description.Height);
80 if (OpenGLConvertExtensions.GetErrorCode() != ErrorCode.NoError)
81 throw new InvalidOperationException(
"Could not create render buffer");
84 GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);
89 PixelInternalFormat internalFormat;
94 ConvertPixelFormat(GraphicsDevice, Description.Format, out internalFormat, out format, out type, out pixelSize, out compressed);
96 InternalFormat = internalFormat;
99 DepthPitch = Description.Width*Description.Height*
pixelSize;
104 #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
105 GL.GenBuffers(1, out resourceId);
106 GL.BindBuffer(BufferTarget.PixelPackBuffer, resourceId);
107 GL.BufferData(BufferTarget.PixelPackBuffer, (IntPtr)DepthPitch, IntPtr.Zero,
108 BufferUsageHint.StreamRead);
109 GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
111 StagingData = Marshal.AllocHGlobal(DepthPitch);
119 if (!creationContext.UseDeviceCreationContext)
120 GraphicsDevice.UseTemporaryFirstTexture();
122 GL.GenTextures(1, out textureId);
123 GL.BindTexture(TextureTarget.Texture2D, textureId);
129 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
130 (int)TextureMinFilter.Nearest);
131 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
132 (int)TextureMagFilter.Nearest);
133 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS,
134 (int)TextureWrapMode.ClampToEdge);
135 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT,
136 (int)TextureWrapMode.ClampToEdge);
138 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
139 else if (Description.MipLevels <= 1)
141 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
142 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
146 #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
147 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0);
148 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, Description.MipLevels - 1);
151 if (Description.MipLevels == 0)
152 throw new NotImplementedException();
154 for (
int i = 0; i < Description.MipLevels; ++i)
156 IntPtr data = IntPtr.Zero;
157 var width = CalculateMipSize(Description.Width, i);
158 var height = CalculateMipSize(Description.Height, i);
159 if (textureDatas != null && i < textureDatas.Length)
161 if (!compressed && textureDatas[i].RowPitch != width *
pixelSize)
162 throw new NotSupportedException(
"Can't upload texture with pitch in glTexImage2D.");
164 data = textureDatas[i].DataPointer;
168 GL.CompressedTexImage2D(TextureTarget.Texture2D, i, internalFormat,
169 width, height, 0, textureDatas[i].SlicePitch, data);
173 GL.TexImage2D(TextureTarget.Texture2D, i, internalFormat,
174 width, height, 0,
format, type, data);
177 GL.BindTexture(TextureTarget.Texture2D, 0);
179 resourceId = textureId;
184 public override void Recreate(DataBox[] dataBoxes = null)
190 protected internal override bool OnRecreate()
196 if (ParentTexture != null)
199 throw new NotImplementedException();
201 resourceId = ParentTexture.ResourceId;
222 protected int ComputeLevelCount(
int mipLevels)
226 return (
int)Math.Ceiling(Math.Log(Math.Max(Description.Width, Description.Height)) / Math.Log(2.0));
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.
Gets a texture view for the whole texture for all mips/arrays dimensions.
GraphicsResourceLifetimeState
Describes the lifetime state of a graphics resource.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
PixelFormat
Defines various types of pixel formats.