4using SharpDX.Direct3D11;
6using Silk.NET.OpenGLES;
8using System.Runtime.InteropServices;
10using SixLabors.ImageSharp;
11using SixLabors.ImageSharp.PixelFormats;
23 public SharpDX.Direct3D11.Texture2D
m_texture;
24 public ShaderResourceView m_textureView;
45 m_texture.DebugName = value;
99 default:
throw new InvalidOperationException(
"Unsupported surface format.");
110 public virtual void SetData<T>(
int mipLevel, T[] source,
int sourceStartIndex = 0) where T : unmanaged {
112 GCHandle gCHandle = GCHandle.Alloc(source, GCHandleType.Pinned);
115 SetDataInternal(mipLevel, gCHandle.AddrOfPinnedObject() + sourceStartIndex * num);
122 public virtual void SetData(
int mipLevel, nint source) {
130 DataBox dataBox =
new(source, num, 0);
137 TextureTarget.Texture2D,
153 DataBox dataBox =
new((nint)source, num, 0);
160 TextureTarget.Texture2D,
179 source.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> memory);
188 Utilities.Swap(ref texture1.m_textureView, ref texture2.m_textureView);
215 Texture2DDescription texture2DDescription =
new() {
217 BindFlags = flag ? BindFlags.ShaderResource | BindFlags.RenderTarget : BindFlags.ShaderResource,
218 CpuAccessFlags = CpuAccessFlags.None,
221 OptionFlags = flag ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None,
222 SampleDescription =
new SampleDescription(1, 0),
223 Usage = ResourceUsage.Default,
238 TextureTarget.Texture2D,
277 if (mipLevelsCount > 1) {
279 for (
int i = 0; i < array.Length; i++) {
280 texture2D.
SetData(i, array[i].Pixels);
286 texture2D.Tag = image;
293 if (mipLevelsCount > 1) {
298 GLWrapper.
GL.GenerateMipmap(TextureTarget.Texture2D);
301 texture2D.Tag = image;
308 if (mipLevelsCount > 1) {
313 GLWrapper.
GL.GenerateMipmap(TextureTarget.Texture2D);
316 texture2D.Tag =
new Image(image);
320 public static Texture2D Load(Stream stream,
bool premultiplyAlpha =
false,
int mipLevelsCount = 1) {
322 if (premultiplyAlpha) {
325 return Load(image, mipLevelsCount);
328 public static Texture2D Load(
string fileName,
bool premultiplyAlpha =
false,
int mipLevelsCount = 1) {
330 return Load(stream, premultiplyAlpha, mipLevelsCount);
337 for (
int i = 0; i < array.Length; i++) {
346 throw new ArgumentOutOfRangeException(nameof(width));
349 throw new ArgumentOutOfRangeException(nameof(height));
351 if (mipLevelsCount < 1) {
352 throw new ArgumentOutOfRangeException(nameof(mipLevelsCount));
355 throw new ArgumentException(nameof(colorFormat));
360 if (mipLevelsCount > 1) {
362 for (
int num2 =
MathUtils.
Max(width, height); num2 >= 1; num2 /= 2) {
378 int num4 = size * num2 * num3;
379 ArgumentNullException.ThrowIfNull(source);
382 throw new ArgumentOutOfRangeException(nameof(mipLevel));
385 throw new ArgumentException(
"Source array element size is larger than pixel size.");
387 if (size % num != 0) {
388 throw new ArgumentException(
"Pixel size is not an integer multiple of source array element size.");
390 if (sourceStartIndex < 0
391 || (source.Length - sourceStartIndex) * num < num4) {
392 throw new InvalidOperationException(
"Not enough data in source array.");
398 if (source == IntPtr.Zero) {
399 throw new ArgumentNullException(nameof(source));
403 throw new ArgumentOutOfRangeException(nameof(mipLevel));
408 if (texture1 ==
null) {
409 throw new ArgumentNullException(nameof(texture1));
411 if (texture2 ==
null) {
412 throw new ArgumentNullException(nameof(texture2));
414 if (texture1.GetType() != typeof(
Texture2D)) {
415 throw new ArgumentException(
"texture1");
417 if (texture2.GetType() != typeof(
Texture2D)) {
418 throw new ArgumentException(
"texture2");
426 ArgumentNullException.ThrowIfNull(source);
static DeviceContext2 Context
static Format TranslateColorFormat(ColorFormat colorFormat)
static void BindTexture(TextureTarget target, int texture, bool forceBind)
static void DeleteTexture(int texture)
override void HandleDeviceLost()
virtual void VerifyParametersSetData(int mipLevel, nint source)
virtual void AllocateTexture()
static Texture2D Load(Stream stream, bool premultiplyAlpha=false, int mipLevelsCount=1)
static Texture2D Load(string fileName, bool premultiplyAlpha=false, int mipLevelsCount=1)
virtual void SetData(Image< Rgba32 > source)
virtual void VerifyParametersSetData< T >(int mipLevel, T[] source, int sourceStartIndex=0)
static void SwapTexture2D(Texture2D texture1, Texture2D texture2)
ColorFormat m_colorFormat
virtual void SetDataInternal(int mipLevel, nint source)
virtual unsafe void SetData(int mipLevel, Image< Rgba32 > source)
virtual void SetData(int mipLevel, nint source)
Texture2D(int width, int height, int mipLevelsCount, ColorFormat colorFormat)
static void VerifyParametersSwap(Texture2D texture1, Texture2D texture2)
override int GetGpuMemoryUsage()
static Texture2D Load(LegacyImage image, int mipLevelsCount=1)
static Texture2D Load(Image image, int mipLevelsCount=1)
static Texture2D Load(Image< Rgba32 > image, int mipLevelsCount=1)
static Texture2D Load(Color color, int width, int height)
virtual unsafe void SetDataInternal(int mipLevel, void *source)
PixelFormat m_pixelFormat
override void HandleDeviceReset()
virtual void InitializeTexture2D(int width, int height, int mipLevelsCount, ColorFormat colorFormat)
static void Swap(Texture2D texture1, Texture2D texture2)
void VerifyParametersSetData(Image< Rgba32 > source)
virtual void SetData< T >(int mipLevel, T[] source, int sourceStartIndex=0)
static int Min(int x1, int x2)
static int Max(int x1, int x2)
static Stream OpenFile(string path, OpenFileMode openFileMode)