/// <summary> /// Initializes a new instance of the <see cref="Buffer" /> class. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="description">The description.</param> /// <param name="bufferFlags">Type of the buffer.</param> /// <param name="viewFormat">The view format.</param> /// <param name="dataPointer">The data pointer.</param> protected Buffer(GraphicsDevice device, BufferDescription description, BufferFlags bufferFlags, PixelFormat viewFormat, IntPtr dataPointer) : base(device) { Description = description; BufferFlags = bufferFlags; ViewFormat = viewFormat; #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES int pixelSize; bool isCompressed; OpenGLConvertExtensions.ConvertPixelFormat(device, viewFormat, out internalFormat, out glPixelFormat, out type, out pixelSize, out isCompressed); #endif Recreate(dataPointer); }
/// <summary> /// Initializes a new instance of the <see cref="Buffer" /> class. /// </summary> /// <param name="description">The description.</param> /// <param name="viewFlags">Type of the buffer.</param> /// <param name="viewFormat">The view format.</param> /// <param name="dataPointer">The data pointer.</param> protected Buffer InitializeFromImpl(BufferDescription description, BufferFlags viewFlags, PixelFormat viewFormat, IntPtr dataPointer) { bufferDescription = description; ViewFlags = viewFlags; #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES int pixelSize; bool isCompressed; OpenGLConvertExtensions.ConvertPixelFormat(GraphicsDevice, ref viewFormat, out internalFormat, out glPixelFormat, out type, out pixelSize, out isCompressed); #endif ViewFormat = viewFormat; Recreate(dataPointer); return(this); }
private void InitializeFromImpl(DataBox[] dataBoxes = null) { if (ParentTexture != null) { resourceId = ParentTexture.ResourceId; // copy parameters InternalFormat = ParentTexture.InternalFormat; FormatGl = ParentTexture.FormatGl; Type = ParentTexture.Type; Target = ParentTexture.Target; DepthPitch = ParentTexture.DepthPitch; RowPitch = ParentTexture.RowPitch; IsDepthBuffer = ParentTexture.IsDepthBuffer; HasStencil = ParentTexture.HasStencil; IsRenderbuffer = ParentTexture.IsRenderbuffer; resourceIdStencil = ParentTexture.ResourceIdStencil; pixelBufferObjectId = ParentTexture.PixelBufferObjectId; } if (resourceId == 0) { switch (Dimension) { case TextureDimension.Texture1D: #if !SILICONSTUDIO_PLATFORM_MONO_MOBILE Target = TextureTarget.Texture1D; break; #endif case TextureDimension.Texture2D: Target = TextureTarget.Texture2D; break; case TextureDimension.Texture3D: Target = TextureTarget.Texture3D; break; case TextureDimension.TextureCube: Target = TextureTarget.TextureCubeMap; break; default: throw new ArgumentOutOfRangeException(); } PixelInternalFormat internalFormat; PixelFormatGl format; PixelType type; int pixelSize; bool compressed; OpenGLConvertExtensions.ConvertPixelFormat(GraphicsDevice, Description.Format, out internalFormat, out format, out type, out pixelSize, out compressed); InternalFormat = internalFormat; FormatGl = format; Type = type; DepthPitch = Description.Width * Description.Height * pixelSize; RowPitch = Description.Width * pixelSize; if ((Description.Flags & TextureFlags.DepthStencil) != 0) { IsDepthBuffer = true; HasStencil = InternalHasStencil(Format); } else { IsDepthBuffer = false; HasStencil = false; } using (GraphicsDevice.UseOpenGLCreationContext()) { // Depth texture are render buffer for now // TODO: enable switch if ((Description.Flags & TextureFlags.DepthStencil) != 0 && (Description.Flags & TextureFlags.ShaderResource) == 0) { RenderbufferStorage depth, stencil; ConvertDepthFormat(GraphicsDevice, Description.Format, out depth, out stencil); GL.GenRenderbuffers(1, out resourceId); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, resourceId); GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, depth, Width, Height); // separate stencil if (stencil != 0) { GL.GenRenderbuffers(1, out resourceIdStencil); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, resourceIdStencil); GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, stencil, Width, Height); } GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0); IsRenderbuffer = true; return; } else { GL.GenTextures(1, out resourceId); GL.BindTexture(Target, resourceId); IsRenderbuffer = false; } // No filtering on depth buffer if ((Description.Flags & (TextureFlags.RenderTarget | TextureFlags.DepthStencil)) != TextureFlags.None) { GL.TexParameter(Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); GL.TexParameter(Target, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(Target, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); BoundSamplerState = GraphicsDevice.SamplerStates.PointClamp; } #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES else if (Description.MipLevels <= 1) { GL.TexParameter(Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); } #endif #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES if (!GraphicsDevice.IsOpenGLES2) #endif { GL.TexParameter(Target, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(Target, TextureParameterName.TextureMaxLevel, Description.MipLevels - 1); } if (Description.MipLevels == 0) { throw new NotImplementedException(); } var setSize = TextureSetSize(Target); for (var arrayIndex = 0; arrayIndex < Description.ArraySize; ++arrayIndex) { var offsetArray = arrayIndex * Description.MipLevels; for (int i = 0; i < Description.MipLevels; ++i) { IntPtr data = IntPtr.Zero; var width = CalculateMipSize(Description.Width, i); var height = CalculateMipSize(Description.Height, i); if (dataBoxes != null && i < dataBoxes.Length) { if (setSize > 1 && !compressed && dataBoxes[i].RowPitch != width * pixelSize) { throw new NotSupportedException("Can't upload texture with pitch in glTexImage2D."); } // Might be possible, need to check API better. data = dataBoxes[offsetArray + i].DataPointer; } if (setSize == 2) { var dataSetTarget = GetTextureTargetForDataSet2D(Target, arrayIndex); if (compressed) { GL.CompressedTexImage2D(dataSetTarget, i, (CompressedInternalFormat2D)internalFormat, width, height, 0, dataBoxes[offsetArray + i].SlicePitch, data); } else { GL.TexImage2D(dataSetTarget, i, (TextureComponentCount2D)internalFormat, width, height, 0, format, type, data); } } else if (setSize == 3) { var dataSetTarget = GetTextureTargetForDataSet3D(Target); var depth = Target == TextureTarget.Texture2DArray ? Description.Depth : CalculateMipSize(Description.Depth, i); // no depth mipmaps in Texture2DArray if (compressed) { GL.CompressedTexImage3D(dataSetTarget, i, (CompressedInternalFormat3D)internalFormat, width, height, depth, 0, dataBoxes[offsetArray + i].SlicePitch, data); } else { GL.TexImage3D(dataSetTarget, i, (TextureComponentCount3D)internalFormat, width, height, depth, 0, format, type, data); } } #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES else if (setSize == 1) { if (compressed) { GL.CompressedTexImage1D(TextureTarget.Texture1D, i, internalFormat, width, 0, dataBoxes[offsetArray + i].SlicePitch, data); } else { GL.TexImage1D(TextureTarget.Texture1D, i, internalFormat, width, 0, format, type, data); } } #endif } } GL.BindTexture(Target, 0); InitializePixelBufferObject(); } } }