Improved CTexture creation

This commit is contained in:
Flamefire committed 2013-05-06 23:35:18 +02:00
1 parent e43f5e7b0a
commit dd96b61cae
12 files changed
+52 -53

No files matched your search

+2
View File
@@ -166,6 +166,8 @@ namespace Vocaluxe.Base
CVideo.GetFrame(_VideoStream, ref _VideoTexture, videoTime, out videoTime); CVideo.GetFrame(_VideoStream, ref _VideoTexture, videoTime, out videoTime);
} }
if (_VideoTexture == null)
return;
RectangleF bounds = new RectangleF(0f, 0f, CSettings.RenderW, CSettings.RenderH); RectangleF bounds = new RectangleF(0f, 0f, CSettings.RenderW, CSettings.RenderH);
RectangleF rect; RectangleF rect;
CHelper.SetRect(bounds, out rect, _VideoTexture.OrigAspect, EAspect.Crop); CHelper.SetRect(bounds, out rect, _VideoTexture.OrigAspect, EAspect.Crop);
+12 -13
View File
@@ -679,21 +679,21 @@ namespace Vocaluxe.Lib.Draw
/// </summary> /// </summary>
public CTexture CopyScreen() public CTexture CopyScreen()
{ {
CTexture texture = new CTexture(-1, _W, _H) {W2 = _CheckForNextPowerOf2(_W), H2 = _CheckForNextPowerOf2(_H)}; CTexture texture = _GetNewTexture(_W, _H);
Surface backbufferSurface = _Device.GetBackBuffer(0, 0); Surface backbufferSurface = _Device.GetBackBuffer(0, 0);
Texture tex = new Texture(_Device, _W, _H, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed); Texture tex = new Texture(_Device, texture.W2, texture.H2, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed);
Surface textureSurface = tex.GetSurfaceLevel(0); Surface textureSurface = tex.GetSurfaceLevel(0);
Surface.FromSurface(textureSurface, backbufferSurface, Filter.Default, 0, new Rectangle(0, 0, _W, _H), new Rectangle(0, 0, _W, _H)); Surface.FromSurface(textureSurface, backbufferSurface, Filter.Default, 0, new Rectangle(0, 0, _W, _H), new Rectangle(0, 0, _W, _H));
backbufferSurface.Dispose(); backbufferSurface.Dispose();
lock (_MutexTexture) lock (_MutexTexture)
{ {
_D3DTextures.Add(_IDs.Peek(), tex);
texture.Index = _IDs.Dequeue(); texture.Index = _IDs.Dequeue();
_D3DTextures.Add(texture.Index, tex);
_Textures[texture.Index] = texture; _Textures[texture.Index] = texture;
return texture;
} }
return texture;
} }
/// <summary> /// <summary>
@@ -856,16 +856,11 @@ namespace Vocaluxe.Lib.Draw
break; break;
} }
CTexture texture = new CTexture(-1, bmp.Width, bmp.Height) {UseFullTexture = true};
int w = Math.Min(bmp.Width, maxSize); int w = Math.Min(bmp.Width, maxSize);
int h = Math.Min(bmp.Height, maxSize); int h = Math.Min(bmp.Height, maxSize);
w = _CheckForNextPowerOf2(w); w = _CheckForNextPowerOf2(w);
h = _CheckForNextPowerOf2(h); h = _CheckForNextPowerOf2(h);
texture.W2 = w;
texture.H2 = h;
Bitmap bmp2 = null; Bitmap bmp2 = null;
byte[] data; byte[] data;
try try
@@ -896,12 +891,13 @@ namespace Vocaluxe.Lib.Draw
bmp2.Dispose(); bmp2.Dispose();
} }
CTexture texture = new CTexture(bmp.Width, bmp.Height, w, h,true);
return _AddTexture(texture, w, data); return _AddTexture(texture, w, data);
} }
public CTexture AddTexture(int w, int h, byte[] data) public CTexture AddTexture(int w, int h, byte[] data)
{ {
CTexture texture = new CTexture(-1, w, h) {W2 = _CheckForNextPowerOf2(w), H2 = _CheckForNextPowerOf2(h)}; CTexture texture = _GetNewTexture(w, h);
return _AddTexture(texture, w, data); return _AddTexture(texture, w, data);
} }
@@ -936,8 +932,6 @@ namespace Vocaluxe.Lib.Draw
rect.Data.Position += rect.Pitch; rect.Data.Position += rect.Pitch;
} }
t.UnlockRectangle(0); t.UnlockRectangle(0);
texture.TexturePath = String.Empty;
} }
catch (Exception) catch (Exception)
{ {
@@ -950,7 +944,7 @@ namespace Vocaluxe.Lib.Draw
public CTexture EnqueueTexture(int w, int h, byte[] data) public CTexture EnqueueTexture(int w, int h, byte[] data)
{ {
CTexture texture = new CTexture(-1, w, h); CTexture texture = _GetNewTexture(w, h);
STextureQueue queue = new STextureQueue {Data = data, Height = h, Width = w}; STextureQueue queue = new STextureQueue {Data = data, Height = h, Width = w};
lock (_MutexTexture) lock (_MutexTexture)
@@ -963,6 +957,11 @@ namespace Vocaluxe.Lib.Draw
} }
return texture; return texture;
} }
private CTexture _GetNewTexture(int w, int h)
{
return new CTexture(w, h, _CheckForNextPowerOf2(w), _CheckForNextPowerOf2(h));
}
#endregion adding #endregion adding
#region updating #region updating
+7 -2
View File
@@ -291,7 +291,7 @@ namespace Vocaluxe.Lib.Draw
{ {
Bitmap bmp = new Bitmap(_Backbuffer); Bitmap bmp = new Bitmap(_Backbuffer);
_Bitmaps.Add(bmp); _Bitmaps.Add(bmp);
CTexture texture = new CTexture(_Bitmaps.Count - 1, bmp.Width, bmp.Height); CTexture texture = _GetNewTexture(bmp.Width, bmp.Height);
// Add to Texture List // Add to Texture List
_Textures[texture.Index] = texture; _Textures[texture.Index] = texture;
@@ -335,7 +335,7 @@ namespace Vocaluxe.Lib.Draw
{ {
Bitmap bmp2 = new Bitmap(bmp); Bitmap bmp2 = new Bitmap(bmp);
_Bitmaps.Add(bmp2); _Bitmaps.Add(bmp2);
CTexture texture = new CTexture(_Bitmaps.Count - 1, bmp.Width, bmp.Height); CTexture texture = _GetNewTexture(bmp.Width, bmp.Height);
// Add to Texture List // Add to Texture List
_Textures[texture.Index] = texture; _Textures[texture.Index] = texture;
@@ -343,6 +343,11 @@ namespace Vocaluxe.Lib.Draw
return texture; return texture;
} }
private CTexture _GetNewTexture(int w, int h)
{
return new CTexture(w, h) {Index = _Bitmaps.Count - 1};
}
public CTexture AddTexture(string texturePath) public CTexture AddTexture(string texturePath)
{ {
if (File.Exists(texturePath)) if (File.Exists(texturePath))
+16 -26
View File
@@ -491,14 +491,11 @@ namespace Vocaluxe.Lib.Draw
public CTexture CopyScreen() public CTexture CopyScreen()
{ {
CTexture texture = new CTexture(-1, _W, _H); CTexture texture = _GetNewTexture(_W, _H);
int id = GL.GenTexture(); int id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id); GL.BindTexture(TextureTarget.Texture2D, id);
texture.ID = id; texture.ID = id;
texture.W2 = MathHelper.NextPowerOfTwo(texture.OrigSize.Width);
texture.H2 = MathHelper.NextPowerOfTwo(texture.OrigSize.Height);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texture.W2, texture.H2, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texture.W2, texture.H2, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
GL.CopyTexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, _X, _Y, _W, _H); GL.CopyTexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, _X, _Y, _W, _H);
@@ -699,20 +696,16 @@ namespace Vocaluxe.Lib.Draw
break; break;
} }
CTexture texture = new CTexture(-1, bmp.Width, bmp.Height) {UseFullTexture = true};
int id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id);
texture.ID = id;
int w = Math.Min(bmp.Width, maxSize); int w = Math.Min(bmp.Width, maxSize);
int h = Math.Min(bmp.Height, maxSize); int h = Math.Min(bmp.Height, maxSize);
w = MathHelper.NextPowerOfTwo(w); w = MathHelper.NextPowerOfTwo(w);
h = MathHelper.NextPowerOfTwo(h); h = MathHelper.NextPowerOfTwo(h);
texture.W2 = w; CTexture texture = new CTexture(bmp.Width, bmp.Height, w, h, true);
texture.H2 = h;
int id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id);
texture.ID = id;
Bitmap bmp2 = null; Bitmap bmp2 = null;
try try
{ {
@@ -764,9 +757,9 @@ namespace Vocaluxe.Lib.Draw
public CTexture AddTexture(int w, int h, byte[] data) public CTexture AddTexture(int w, int h, byte[] data)
{ {
CTexture texture = new CTexture(-1, w, h); CTexture texture = _GetNewTexture(w, h);
_CreateTexture(texture, data); _CreateTexture(texture, w, h, data);
lock (_MutexTexture) lock (_MutexTexture)
{ {
@@ -777,10 +770,8 @@ namespace Vocaluxe.Lib.Draw
return texture; return texture;
} }
private void _CreateTexture(CTexture texture, byte[] data) private void _CreateTexture(CTexture texture, int w, int h, byte[] data)
{ {
int w = texture.OrigSize.Width;
int h = texture.OrigSize.Height;
if (_UsePBO) if (_UsePBO)
{ {
try try
@@ -801,9 +792,6 @@ namespace Vocaluxe.Lib.Draw
GL.BindTexture(TextureTarget.Texture2D, id); GL.BindTexture(TextureTarget.Texture2D, id);
texture.ID = id; texture.ID = id;
texture.W2 = MathHelper.NextPowerOfTwo(w);
texture.H2 = MathHelper.NextPowerOfTwo(h);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texture.W2, texture.H2, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texture.W2, texture.H2, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, w, h, PixelFormat.Bgra, PixelType.UnsignedByte, data); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, w, h, PixelFormat.Bgra, PixelType.UnsignedByte, data);
@@ -818,14 +806,11 @@ namespace Vocaluxe.Lib.Draw
//GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); //GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D); GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, 0); GL.BindTexture(TextureTarget.Texture2D, 0);
// Add to Texture List
texture.TexturePath = String.Empty;
} }
public CTexture EnqueueTexture(int w, int h, byte[] data) public CTexture EnqueueTexture(int w, int h, byte[] data)
{ {
CTexture texture = new CTexture(-1, w, h); CTexture texture = _GetNewTexture(w, h);
STextureQueue queue = new STextureQueue {Data = data, Height = h, Width = w}; STextureQueue queue = new STextureQueue {Data = data, Height = h, Width = w};
lock (_MutexTexture) lock (_MutexTexture)
@@ -838,6 +823,11 @@ namespace Vocaluxe.Lib.Draw
return texture; return texture;
} }
private CTexture _GetNewTexture(int w, int h)
{
return new CTexture(w, h, MathHelper.NextPowerOfTwo(w), MathHelper.NextPowerOfTwo(h));
}
#endregion adding #endregion adding
#region updating #region updating
@@ -1183,7 +1173,7 @@ namespace Vocaluxe.Lib.Draw
continue; continue;
if (q.Width == texture.OrigSize.Width && q.Height == texture.OrigSize.Height) if (q.Width == texture.OrigSize.Width && q.Height == texture.OrigSize.Height)
_CreateTexture(texture, q.Data); _CreateTexture(texture, q.Width, q.Height, q.Data);
else else
CLog.LogError("Wrong texture size in queue!"); CLog.LogError("Wrong texture size in queue!");
} }
-1
View File
@@ -20,7 +20,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using VocaluxeLib.Draw; using VocaluxeLib.Draw;
using VocaluxeLib.Menu;
namespace Vocaluxe.Lib.Video namespace Vocaluxe.Lib.Video
{ {
@@ -26,7 +26,6 @@ using System.Threading;
using Vocaluxe.Base; using Vocaluxe.Base;
using Vocaluxe.Lib.Video.Acinerella; using Vocaluxe.Lib.Video.Acinerella;
using VocaluxeLib.Draw; using VocaluxeLib.Draw;
using VocaluxeLib.Menu;
namespace Vocaluxe.Lib.Video namespace Vocaluxe.Lib.Video
{ {
@@ -21,7 +21,6 @@ using System;
using Vocaluxe.Base; using Vocaluxe.Base;
using Vocaluxe.Lib.Video.Gstreamer; using Vocaluxe.Lib.Video.Gstreamer;
using VocaluxeLib.Draw; using VocaluxeLib.Draw;
using VocaluxeLib.Menu;
namespace Vocaluxe.Lib.Video namespace Vocaluxe.Lib.Video
{ {
-1
View File
@@ -18,7 +18,6 @@
#endregion #endregion
using VocaluxeLib.Draw; using VocaluxeLib.Draw;
using VocaluxeLib.Menu;
namespace Vocaluxe.Lib.Video namespace Vocaluxe.Lib.Video
{ {
-1
View File
@@ -25,7 +25,6 @@ using System.Drawing.Imaging;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Vocaluxe.Base; using Vocaluxe.Base;
using VocaluxeLib.Draw; using VocaluxeLib.Draw;
using VocaluxeLib.Menu;
namespace Vocaluxe.Lib.Webcam namespace Vocaluxe.Lib.Webcam
{ {
-1
View File
@@ -20,7 +20,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using VocaluxeLib.Draw; using VocaluxeLib.Draw;
using VocaluxeLib.Menu;
namespace Vocaluxe.Lib.Webcam namespace Vocaluxe.Lib.Webcam
{ {
-1
View File
@@ -22,7 +22,6 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Windows.Forms; using System.Windows.Forms;
using Vocaluxe.Base; using Vocaluxe.Base;
using VocaluxeLib.Draw;
using VocaluxeLib.Menu; using VocaluxeLib.Menu;
using VocaluxeLib.Menu.SongMenu; using VocaluxeLib.Menu.SongMenu;
+15 -5
View File
@@ -65,6 +65,7 @@ namespace VocaluxeLib.Draw
/// </summary> /// </summary>
public float HeightRatio { get; private set; } public float HeightRatio { get; private set; }
/*
/// <summary> /// <summary>
/// Internal use. Specifies if full texture memory should be used /// Internal use. Specifies if full texture memory should be used
/// Set to true if you resized the original image to the maximum /// Set to true if you resized the original image to the maximum
@@ -87,16 +88,25 @@ namespace VocaluxeLib.Draw
} }
} }
} }
*/
public CTexture(int index, int origWidth = 1, int origHeight = 1) /// <summary>
/// Creates a new texture reference
/// </summary>
/// <param name="origWidth">Original width (Bitmap size)</param>
/// <param name="origHeight">Original height (Bitmap size)</param>
/// <param name="texWidth">Width in video memory</param>
/// <param name="texHeight">Height in video memory</param>
/// <param name="useFullTexture">True if it is using the full size of the video memory (no blank parts due to Pow-2-Issue)</param>
public CTexture(int origWidth, int origHeight, int texWidth = 0, int texHeight = 0, bool useFullTexture = false)
{ {
Index = index;
OrigSize = new Size(origWidth, origHeight); OrigSize = new Size(origWidth, origHeight);
Rect = new SRectF(0f, 0f, origWidth, origHeight, 0f); Rect = new SRectF(0f, 0f, origWidth, origHeight, 0f);
_UseFullTexture = useFullTexture;
_W2 = origWidth; //IMPORTANT: Use setter here to calculate ratios!
_H2 = origHeight; W2 = (texWidth > 0) ? texWidth : origWidth;
H2 = (texHeight > 0) ? texHeight : origHeight;
} }
} }
} }