Use better references to a single texture and unify more stuff

Also encapsulate everything graphic related in the Texture classes and not in the references
This commit is contained in:
Flamefire committed 2014-09-20 00:50:04 +02:00
1 parent cfb6d09d06
commit 1ce085b49f
8 files changed
+364 -298

No files matched your search

+62 -53
View File
@@ -42,7 +42,24 @@ namespace Vocaluxe.Lib.Draw
} }
} }
class CDirect3D : CDrawBaseWindows<Texture>, IDraw class CD3DTexture:CTextureBase,IDisposable
{
public readonly Texture D3DTexture;
public CD3DTexture(Device device, Size dataSize, int texWidth = 0, int texHeight = 0) : base(dataSize, texWidth, texHeight)
{
D3DTexture = new Texture(device, W2, H2, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed);
}
public void Dispose()
{
if(D3DTexture.Disposed)
throw new ObjectDisposedException(GetType().Name);
D3DTexture.Dispose();
RefCount = 0;
}
}
class CDirect3D : CDrawBaseWindows<CD3DTexture>, IDraw
{ {
#region private vars #region private vars
private readonly Direct3D _D3D; private readonly Direct3D _D3D;
@@ -168,9 +185,6 @@ namespace Vocaluxe.Lib.Draw
} }
} }
#region form events
#endregion form events
#region resize #region resize
/// <summary> /// <summary>
/// Resizes the viewport /// Resizes the viewport
@@ -468,37 +482,33 @@ namespace Vocaluxe.Lib.Draw
/// </summary> /// </summary>
public CTextureRef CopyScreen() public CTextureRef CopyScreen()
{ {
CTextureRef texture = _GetNewTextureRef(_W, _H); var tex = _CreateTexture(new Size(_W, _H));
Surface backbufferSurface = _Device.GetBackBuffer(0, 0); Surface backbufferSurface = _Device.GetBackBuffer(0, 0);
var tex = new Texture(_Device, texture.W2, texture.H2, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed); Surface textureSurface = tex.D3DTexture.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 (_Textures)
{
_Textures.Add(texture.ID, tex);
}
return texture; return _GetTextureReference(_W, _H, tex);
} }
/// <summary> /// <summary>
/// Copies the current frame into a texture /// Copies the current frame into a texture
/// </summary> /// </summary>
/// <param name="texture">The texture in which the frame is copied to</param> /// <param name="textureRef">The texture in which the frame is copied to</param>
public void CopyScreen(ref CTextureRef texture) public void CopyScreen(ref CTextureRef textureRef)
{ {
if (!_TextureExists(texture) || texture.DataSize.Width != GetScreenWidth() || texture.DataSize.Height != GetScreenHeight()) CD3DTexture texture;
if (!_GetTexture(textureRef, out texture) || texture.DataSize.Width != GetScreenWidth() || texture.DataSize.Height != GetScreenHeight())
{ {
RemoveTexture(ref texture); RemoveTexture(ref textureRef);
texture = CopyScreen(); textureRef = CopyScreen();
} }
else else
{ {
Surface backbufferSurface = _Device.GetBackBuffer(0, 0); Surface backbufferSurface = _Device.GetBackBuffer(0, 0);
Surface textureSurface = _Textures[texture.ID].GetSurfaceLevel(0); Surface textureSurface = texture.D3DTexture.GetSurfaceLevel(0);
Surface.FromSurface(textureSurface, backbufferSurface, Filter.Default, 0); Surface.FromSurface(textureSurface, backbufferSurface, Filter.Default, 0);
backbufferSurface.Dispose();
} }
} }
@@ -566,12 +576,17 @@ namespace Vocaluxe.Lib.Draw
#endregion Basic Draw Methods #endregion Basic Draw Methods
#region Textures #region Textures
protected override Texture _CreateTexture(CTextureRef texture, byte[] data) private CD3DTexture _CreateTexture(Size dataSize)
{
return new CD3DTexture(_Device, dataSize, _CheckForNextPowerOf2(dataSize.Width), _CheckForNextPowerOf2(dataSize.Height));
}
protected override CD3DTexture _CreateTexture(Size dataSize, byte[] data)
{ {
//Create a new texture in the managed pool, which does not need to be recreated on a lost device //Create a new texture in the managed pool, which does not need to be recreated on a lost device
//because a copy of the texture is hold in the Ram //because a copy of the texture is hold in the Ram
var t = new Texture(_Device, texture.W2, texture.H2, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed); CD3DTexture t = _CreateTexture(dataSize);
_WriteDataToTexture(t, texture.DataSize.Width, data); _WriteDataToTexture(t.D3DTexture, dataSize.Width, data);
return t; return t;
} }
@@ -597,14 +612,15 @@ namespace Vocaluxe.Lib.Draw
/// <summary> /// <summary>
/// Updates the data of a texture /// Updates the data of a texture
/// </summary> /// </summary>
/// <param name="texture">The texture to update</param> /// <param name="textureRef">The texture to update</param>
/// <param name="w"></param> /// <param name="w"></param>
/// <param name="h"></param> /// <param name="h"></param>
/// <param name="data">A byte array containing the new texture's data</param> /// <param name="data">A byte array containing the new texture's data</param>
/// <returns>True if succeeded</returns> /// <returns>True if succeeded</returns>
public override bool UpdateTexture(CTextureRef texture, int w, int h, byte[] data) public override bool UpdateTexture(CTextureRef textureRef, int w, int h, byte[] data)
{ {
if (!_TextureExists(texture)) CD3DTexture texture;
if (!_GetTexture(textureRef, out texture))
return false; return false;
if (texture.DataSize.Width != w || texture.DataSize.Height != h) if (texture.DataSize.Width != w || texture.DataSize.Height != h)
{ {
@@ -614,10 +630,7 @@ namespace Vocaluxe.Lib.Draw
return false; // Texture memory to big return false; // Texture memory to big
texture.DataSize = new Size(w, h); texture.DataSize = new Size(w, h);
} }
lock (_Textures) _WriteDataToTexture(texture.D3DTexture, w, data);
{
_WriteDataToTexture(_Textures[texture.ID], w, data);
}
return true; return true;
} }
@@ -648,17 +661,17 @@ namespace Vocaluxe.Lib.Draw
/// <summary> /// <summary>
/// Draws a texture /// Draws a texture
/// </summary> /// </summary>
/// <param name="texture">The texture to be drawn</param> /// <param name="textureRef">The texture to be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param> /// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param> /// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
/// <param name="mirrored">True if the texture should be mirrored</param> /// <param name="mirrored">True if the texture should be mirrored</param>
public void DrawTexture(CTextureRef texture, SRectF rect, SColorF color, bool mirrored = false) public void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, bool mirrored = false)
{ {
if (rect.W < 1 || rect.H < 1) if (rect.W < 1 || rect.H < 1)
return; return;
Texture t = _GetTexture(texture); CD3DTexture texture;
if (t == null) if (!_GetTexture(textureRef, out texture))
return; return;
//Calculate the position //Calculate the position
@@ -687,18 +700,18 @@ namespace Vocaluxe.Lib.Draw
vert[1] = new STexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y2), c.ToArgb()); vert[1] = new STexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y2), c.ToArgb());
vert[2] = new STexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb()); vert[2] = new STexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb());
vert[3] = new STexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), c.ToArgb()); vert[3] = new STexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), c.ToArgb());
_AddToVertexBuffer(vert, t, _CalculateRotationMatrix(rect.Rotation, rx1, rx2, ry1, ry2)); _AddToVertexBuffer(vert, texture.D3DTexture, _CalculateRotationMatrix(rect.Rotation, rx1, rx2, ry1, ry2));
} }
/// <summary> /// <summary>
/// Draws a texture /// Draws a texture
/// </summary> /// </summary>
/// <param name="texture">The texture to be drawn</param> /// <param name="textureRef">The texture to be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param> /// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param> /// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
/// <param name="bounds">A SRectF struct containing which part of the texture should be drawn</param> /// <param name="bounds">A SRectF struct containing which part of the texture should be drawn</param>
/// <param name="mirrored">True if the texture should be mirrored</param> /// <param name="mirrored">True if the texture should be mirrored</param>
public void DrawTexture(CTextureRef texture, SRectF rect, SColorF color, SRectF bounds, bool mirrored = false) public void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, SRectF bounds, bool mirrored = false)
{ {
if (Math.Abs(rect.W) < 1 || Math.Abs(rect.H) < 1 || Math.Abs(bounds.H) < 1 || Math.Abs(bounds.W) < 1 || if (Math.Abs(rect.W) < 1 || Math.Abs(rect.H) < 1 || Math.Abs(bounds.H) < 1 || Math.Abs(bounds.W) < 1 ||
Math.Abs(color.A) < 0.01) Math.Abs(color.A) < 0.01)
@@ -710,8 +723,8 @@ namespace Vocaluxe.Lib.Draw
if (bounds.Y > rect.Y + rect.H || bounds.Y + bounds.H < rect.Y) if (bounds.Y > rect.Y + rect.H || bounds.Y + bounds.H < rect.Y)
return; return;
Texture t = _GetTexture(texture); CD3DTexture texture;
if (t == null) if (!_GetTexture(textureRef, out texture))
return; return;
//Calculate the position //Calculate the position
@@ -764,22 +777,21 @@ namespace Vocaluxe.Lib.Draw
vert[1] = new STexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y2), c.ToArgb()); vert[1] = new STexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y2), c.ToArgb());
vert[2] = new STexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb()); vert[2] = new STexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb());
vert[3] = new STexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), c.ToArgb()); vert[3] = new STexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), c.ToArgb());
_AddToVertexBuffer(vert, t, _CalculateRotationMatrix(rect.Rotation, rx1, rx2, ry1, ry2)); _AddToVertexBuffer(vert, texture.D3DTexture, _CalculateRotationMatrix(rect.Rotation, rx1, rx2, ry1, ry2));
} }
/// <summary> /// <summary>
/// Draws a texture /// Draws a texture
/// </summary> /// </summary>
/// <param name="texture">The texture to be drawn</param> /// <param name="textureRef">The texture to be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param> /// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param> /// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
/// <param name="begin">A Value ranging from 0 to 1 containing the beginning of the texture</param> /// <param name="begin">A Value ranging from 0 to 1 containing the beginning of the texture</param>
/// <param name="end">A Value ranging from 0 to 1 containing the ending of the texture</param> /// <param name="end">A Value ranging from 0 to 1 containing the ending of the texture</param>
public void DrawTexture(CTextureRef texture, SRectF rect, SColorF color, float begin, float end) public void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, float begin, float end)
{ {
if (!_TextureExists(texture)) CD3DTexture texture;
return; if (!_GetTexture(textureRef, out texture))
if (_Textures[texture.ID] == null)
return; return;
float x1 = 0f + begin * texture.WidthRatio; float x1 = 0f + begin * texture.WidthRatio;
@@ -807,23 +819,22 @@ namespace Vocaluxe.Lib.Draw
vert[1] = new STexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y2), c.ToArgb()); vert[1] = new STexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y2), c.ToArgb());
vert[2] = new STexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb()); vert[2] = new STexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb());
vert[3] = new STexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), c.ToArgb()); vert[3] = new STexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), c.ToArgb());
_AddToVertexBuffer(vert, _Textures[texture.ID], _CalculateRotationMatrix(rect.Rotation, rx1, rx2, ry1, ry2)); _AddToVertexBuffer(vert, texture.D3DTexture, _CalculateRotationMatrix(rect.Rotation, rx1, rx2, ry1, ry2));
} }
/// <summary> /// <summary>
/// Draws a reflection of a texture /// Draws a reflection of a texture
/// </summary> /// </summary>
/// <param name="texture">The texture of which a reflection should be drawn</param> /// <param name="textureRef">The texture of which a reflection should be drawn</param>
/// <param name="rect">A SRectF struct containing the destination coordinates</param> /// <param name="rect">A SRectF struct containing the destination coordinates</param>
/// <param name="color">A SColorF struct containing a color which the texture will be colored in</param> /// <param name="color">A SColorF struct containing a color which the texture will be colored in</param>
/// <param name="bounds">A SRectF struct containing which part of the texture should be drawn</param> /// <param name="bounds">A SRectF struct containing which part of the texture should be drawn</param>
/// <param name="space">The space between the texture and the reflection</param> /// <param name="space">The space between the texture and the reflection</param>
/// <param name="height">The height of the reflection</param> /// <param name="height">The height of the reflection</param>
public void DrawTextureReflection(CTextureRef texture, SRectF rect, SColorF color, SRectF bounds, float space, float height) public void DrawTextureReflection(CTextureRef textureRef, SRectF rect, SColorF color, SRectF bounds, float space, float height)
{ {
if (!_TextureExists(texture)) CD3DTexture texture;
return; if (!_GetTexture(textureRef, out texture))
if (_Textures[texture.ID] == null)
return; return;
if (Math.Abs(rect.W) < float.Epsilon || Math.Abs(rect.H) < float.Epsilon || Math.Abs(bounds.H) < float.Epsilon || Math.Abs(bounds.W) < float.Epsilon || if (Math.Abs(rect.W) < float.Epsilon || Math.Abs(rect.H) < float.Epsilon || Math.Abs(bounds.H) < float.Epsilon || Math.Abs(bounds.W) < float.Epsilon ||
@@ -890,7 +901,7 @@ namespace Vocaluxe.Lib.Draw
vert[1] = new STexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y1), transparent.ToArgb()); vert[1] = new STexturedColoredVertex(new Vector3(rx1, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x1, y1), transparent.ToArgb());
vert[2] = new STexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), transparent.ToArgb()); vert[2] = new STexturedColoredVertex(new Vector3(rx2, -ry2, rect.Z + CGraphics.ZOffset), new Vector2(x2, y1), transparent.ToArgb());
vert[3] = new STexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb()); vert[3] = new STexturedColoredVertex(new Vector3(rx2, -ry1, rect.Z + CGraphics.ZOffset), new Vector2(x2, y2), c.ToArgb());
_AddToVertexBuffer(vert, _Textures[texture.ID], _CalculateRotationMatrix(rect.Rotation, rx1, rx2, ry1, ry2)); _AddToVertexBuffer(vert, texture.D3DTexture, _CalculateRotationMatrix(rect.Rotation, rx1, rx2, ry1, ry2));
} }
#endregion drawing #endregion drawing
@@ -898,7 +909,6 @@ namespace Vocaluxe.Lib.Draw
#endregion implementation #endregion implementation
#region utility
private static Matrix _CalculateRotationMatrix(float rot, float rx1, float rx2, float ry1, float ry2) private static Matrix _CalculateRotationMatrix(float rot, float rx1, float rx2, float ry1, float ry2)
{ {
Matrix originTranslation = Matrix.Translation(new Vector3(-(float)CSettings.RenderW / 2, (float)CSettings.RenderH / 2, 0)); Matrix originTranslation = Matrix.Translation(new Vector3(-(float)CSettings.RenderW / 2, (float)CSettings.RenderH / 2, 0));
@@ -921,7 +931,6 @@ namespace Vocaluxe.Lib.Draw
} }
return originTranslation; return originTranslation;
} }
#endregion utility
private struct STexturedColoredVertex private struct STexturedColoredVertex
{ {
+132 -93
View File
@@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Drawing.Imaging; using System.Drawing.Imaging;
@@ -10,23 +9,34 @@ using System.Threading;
using Vocaluxe.Base; using Vocaluxe.Base;
using VocaluxeLib; using VocaluxeLib;
using VocaluxeLib.Draw; using VocaluxeLib.Draw;
using System.Diagnostics;
namespace Vocaluxe.Lib.Draw namespace Vocaluxe.Lib.Draw
{ {
struct STextureQueue abstract class CDrawBase<TTextureType> where TTextureType : CTextureBase, IDisposable
{ {
public readonly CTextureRef Texture; struct STextureQueue
public readonly byte[] Data;
public STextureQueue(CTextureRef texture, byte[] data)
{ {
Texture = texture; public readonly int TextureID;
Data = data; public readonly Size DataSize;
public readonly byte[] Data;
public STextureQueue(int textureID, Size dataSize, byte[] data)
{
TextureID = textureID;
DataSize = dataSize;
Data = data;
}
}
struct STextureCacheEntry
{
public TTextureType Texture;
/// <summary>
/// Size of original image (e.g. of bmp)
/// </summary>
public Size OrigSize;
} }
}
abstract class CDrawBase<TTextureType> where TTextureType : class, IDisposable
{
protected bool _NonPowerOf2TextureSupported; protected bool _NonPowerOf2TextureSupported;
protected bool _Fullscreen; protected bool _Fullscreen;
@@ -38,9 +48,12 @@ namespace Vocaluxe.Lib.Draw
private readonly Object _MutexID = new object(); private readonly Object _MutexID = new object();
private int _NextID; private int _NextID;
protected readonly Dictionary<int, TTextureType> _Textures = new Dictionary<int, TTextureType>(); // Maps texture IDs to textures. Multiple IDs can be mapped to one texture (-->texture.RefCount>1)
protected readonly Dictionary<string, WeakReference> _TextureCache = new Dictionary<string, WeakReference>(); private readonly Dictionary<int, TTextureType> _Textures = new Dictionary<int, TTextureType>();
// Maps texturePaths to textures. Texture may have already been disposed (RefCount<=0) or even null
private readonly Dictionary<string, STextureCacheEntry> _TextureCache = new Dictionary<string, STextureCacheEntry>();
private readonly Queue<STextureQueue> _TexturesToLoad = new Queue<STextureQueue>(); private readonly Queue<STextureQueue> _TexturesToLoad = new Queue<STextureQueue>();
private int _TextureCount;
protected int _H; protected int _H;
protected int _W; protected int _W;
@@ -72,8 +85,9 @@ namespace Vocaluxe.Lib.Draw
//Dispose all textures //Dispose all textures
foreach (TTextureType texture in _Textures.Values) foreach (TTextureType texture in _Textures.Values)
{ {
if (texture != null) if (texture != null && --texture.RefCount<=0)
texture.Dispose(); texture.Dispose();
} }
_Textures.Clear(); _Textures.Clear();
_TextureCache.Clear(); _TextureCache.Clear();
@@ -127,7 +141,7 @@ namespace Vocaluxe.Lib.Draw
/// </summary> /// </summary>
/// <param name="n">The value of which the next power of two will be calculated</param> /// <param name="n">The value of which the next power of two will be calculated</param>
/// <returns>The next power of two</returns> /// <returns>The next power of two</returns>
private int _CheckForNextPowerOf2(int n) protected int _CheckForNextPowerOf2(int n)
{ {
if (_NonPowerOf2TextureSupported) if (_NonPowerOf2TextureSupported)
return n; return n;
@@ -140,37 +154,54 @@ namespace Vocaluxe.Lib.Draw
/// <summary> /// <summary>
/// Creates the texture specified by the reference and fills it with the given data /// Creates the texture specified by the reference and fills it with the given data
/// </summary> /// </summary>
/// <param name="texture"></param> /// <param name="dataSize"></param>
/// <param name="data"></param> /// <param name="data"></param>
/// <returns></returns> /// <returns></returns>
protected abstract TTextureType _CreateTexture(CTextureRef texture, byte[] data); protected abstract TTextureType _CreateTexture(Size dataSize, byte[] data);
/// <summary> /// <summary>
/// Creates the texture specified by the reference and fills it with the given data<br /> /// Creates the texture specified by the reference and fills it with the given data<br />
/// Data has to be a pointer to an array of size W*H with 4 Byte values for each pixel (e.g. bmpData.Scan0) /// Data has to be a pointer to an array of size W*H with 4 Byte values for each pixel (e.g. bmpData.Scan0)
/// </summary> /// </summary>
/// <param name="texture"></param> /// <param name="dataSize"></param>
/// <param name="data"></param> /// <param name="data"></param>
/// <returns></returns> /// <returns></returns>
protected virtual TTextureType _CreateTexture(CTextureRef texture, IntPtr data) protected virtual TTextureType _CreateTexture(Size dataSize, IntPtr data)
{ {
byte[] dataArray = new byte[4 * texture.DataSize.Width * texture.DataSize.Height]; byte[] dataArray = new byte[4 * dataSize.Width * dataSize.Height];
Marshal.Copy(data, dataArray, 0, dataArray.Length); Marshal.Copy(data, dataArray, 0, dataArray.Length);
return _CreateTexture(texture, dataArray); return _CreateTexture(dataSize, dataArray);
} }
protected CTextureRef _GetNewTextureRef(int origWidth, int origHeight, int dataWidth = 0, int dataHeight = 0) private void _AddToCache(TTextureType texture, Size origSize, string texturePath)
{ {
Debug.Assert(origWidth > 0 && origHeight > 0); if (texture != null && !String.IsNullOrEmpty(texturePath))
Debug.Assert(dataWidth > 0 || dataHeight <= 0);
int id;
lock (_MutexID)
{ {
id = _NextID++; texture.TexturePath = texturePath;
STextureCacheEntry cacheEntry=new STextureCacheEntry {OrigSize = origSize, Texture = texture};
lock (_Textures)
{
_TextureCache[texturePath] = cacheEntry;
}
} }
Size origSize = new Size(origWidth, origHeight); }
Size dataSize = (dataHeight > 0) ? new Size(dataWidth, dataHeight) : origSize;
return new CTextureRef(id, origSize, dataSize, _CheckForNextPowerOf2(dataSize.Width), _CheckForNextPowerOf2(dataSize.Height)); private CTextureRef _GetFromCache(string texturePath)
{
lock (_Textures)
{
STextureCacheEntry cacheEntry;
if (_TextureCache.TryGetValue(texturePath, out cacheEntry))
{
if (cacheEntry.Texture.RefCount <= 0)
_TextureCache.Remove(texturePath);
else
{
return _GetTextureReference(cacheEntry.OrigSize, cacheEntry.Texture);
}
}
}
return null;
} }
/// <summary> /// <summary>
@@ -180,22 +211,9 @@ namespace Vocaluxe.Lib.Draw
/// <returns>A STexture object containing the added texture</returns> /// <returns>A STexture object containing the added texture</returns>
public CTextureRef AddTexture(string texturePath) public CTextureRef AddTexture(string texturePath)
{ {
CTextureRef textureRef; CTextureRef textureRef = _GetFromCache(texturePath);
lock (_Textures) if (textureRef != null)
{ return textureRef;
WeakReference reference;
if (_TextureCache.TryGetValue(texturePath, out reference))
{
if (!reference.IsAlive)
_TextureCache.Remove(texturePath);
else
{
textureRef = (CTextureRef)reference.Target;
textureRef.RefCount++;
return textureRef;
}
}
}
if (!File.Exists(texturePath)) if (!File.Exists(texturePath))
{ {
@@ -220,16 +238,12 @@ namespace Vocaluxe.Lib.Draw
{ {
bmp.Dispose(); bmp.Dispose();
} }
lock (_Textures)
{
_TextureCache[texturePath] = new WeakReference(textureRef);
}
return textureRef; return textureRef;
} }
public CTextureRef AddTexture(Bitmap bmp) public CTextureRef AddTexture(Bitmap bmp)
{ {
return AddTexture(bmp, ""); return AddTexture(bmp, null);
} }
/// <summary> /// <summary>
@@ -266,18 +280,17 @@ namespace Vocaluxe.Lib.Draw
break; break;
} }
int w = Math.Min(bmp.Width, maxSize); Size origSize=new Size(bmp.Width, bmp.Height);
int h = Math.Min(bmp.Height, maxSize);
CTextureRef texture = _GetNewTextureRef(bmp.Width, bmp.Height, w, h);
texture.TexturePath = texturePath;
Bitmap bmp2 = null; Bitmap bmp2 = null;
TTextureType texture;
try try
{ {
// Make sure maxSize is a power of 2 if required
maxSize = _CheckForNextPowerOf2(maxSize);
// Do not stretch the texture, only make it smaller // Do not stretch the texture, only make it smaller
w = Math.Min(bmp.Width, texture.W2); int w = Math.Min(bmp.Width, maxSize);
h = Math.Min(bmp.Height, texture.H2); int h = Math.Min(bmp.Height, maxSize);
if (bmp.Width != w || bmp.Height != h) if (bmp.Width != w || bmp.Height != h)
{ {
//Create a new Bitmap with the new sizes //Create a new Bitmap with the new sizes
@@ -290,12 +303,11 @@ namespace Vocaluxe.Lib.Draw
g.DrawImage(bmp, new Rectangle(0, 0, bmp2.Width, bmp2.Height)); g.DrawImage(bmp, new Rectangle(0, 0, bmp2.Width, bmp2.Height));
} }
bmp = bmp2; bmp = bmp2;
texture.DataSize = new Size(w, h);
} }
//Fill the new Bitmap with the texture data //Fill the new Bitmap with the texture data
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
_AddTexture(texture, bmpData.Scan0); texture=_CreateTexture(new Size(w,h), bmpData.Scan0);
bmp.UnlockBits(bmpData); bmp.UnlockBits(bmpData);
} }
finally finally
@@ -304,44 +316,64 @@ namespace Vocaluxe.Lib.Draw
bmp2.Dispose(); bmp2.Dispose();
} }
return texture; _AddToCache(texture, origSize, texturePath);
return _GetTextureReference(origSize,texture);
} }
public CTextureRef AddTexture(int w, int h, byte[] data) public CTextureRef AddTexture(int w, int h, byte[] data)
{ {
CTextureRef texture = _GetNewTextureRef(w, h); TTextureType texture = _CreateTexture(new Size(w, h), data);
_AddTexture(texture, data); return _GetTextureReference(w,h, texture);
return texture;
} }
private void _AddTexture(CTextureRef texture, byte[] data) /// <summary>
/// Method used to add a texture to the texture list and return a TextureRef to it<br/>
/// Important: This should be the only function to ever write to _Textures
/// </summary>
/// <param name="origSize"></param>
/// <param name="texture"></param>
/// <returns></returns>
protected CTextureRef _GetTextureReference(Size origSize, TTextureType texture)
{ {
TTextureType t = _CreateTexture(texture, data); Debug.Assert(origSize.Width>0 && origSize.Height>0);
int id;
lock (_MutexID)
{
id = _NextID++;
}
CTextureRef textureRef = new CTextureRef(id, origSize);
if (texture != null)
texture.RefCount++;
if(texture==null || texture.RefCount==1)
_TextureCount++;
lock (_Textures) lock (_Textures)
{ {
_Textures.Add(texture.ID, t); _Textures.Add(id, texture);
} }
return textureRef;
} }
private void _AddTexture(CTextureRef texture, IntPtr data) /// <summary>
/// Method used to add a texture to the texture list and return a TextureRef to it<br/>
/// Convenience method
/// </summary>
/// <param name="origWidth"></param>
/// <param name="origHeight"></param>
/// <param name="texture"></param>
/// <returns></returns>
protected CTextureRef _GetTextureReference(int origWidth, int origHeight, TTextureType texture)
{ {
TTextureType t = _CreateTexture(texture, data); return _GetTextureReference(new Size(origWidth, origHeight), texture);
lock (_Textures)
{
_Textures.Add(texture.ID, t);
}
} }
public CTextureRef EnqueueTexture(int w, int h, byte[] data) public CTextureRef EnqueueTexture(int w, int h, byte[] data)
{ {
CTextureRef texture = _GetNewTextureRef(w, h);
lock (_Textures) lock (_Textures)
{ {
_Textures.Add(texture.ID, null); CTextureRef textureRef = _GetTextureReference(w, h, null);
_TexturesToLoad.Enqueue(new STextureQueue(texture, data)); _TexturesToLoad.Enqueue(new STextureQueue(textureRef.ID, new Size(w,h),data));
return textureRef;
} }
return texture;
} }
/// <summary> /// <summary>
@@ -371,11 +403,8 @@ namespace Vocaluxe.Lib.Draw
/// <returns>True if the texture exists</returns> /// <returns>True if the texture exists</returns>
protected bool _TextureExists(CTextureRef texture) protected bool _TextureExists(CTextureRef texture)
{ {
lock (_Textures)
{
TTextureType t; TTextureType t;
return texture != null && _Textures.TryGetValue(texture.ID, out t) && t != null; return _GetTexture(texture, out t);
}
} }
protected TTextureType _GetTexture(CTextureRef textureRef) protected TTextureType _GetTexture(CTextureRef textureRef)
@@ -383,10 +412,19 @@ namespace Vocaluxe.Lib.Draw
if (textureRef == null) if (textureRef == null)
return null; return null;
TTextureType texture; TTextureType texture;
_Textures.TryGetValue(textureRef.ID, out texture); lock (_Textures)
{
_Textures.TryGetValue(textureRef.ID, out texture);
}
return texture; return texture;
} }
protected bool _GetTexture(CTextureRef textureRef, out TTextureType texture)
{
texture = _GetTexture(textureRef);
return texture != null;
}
/// <summary> /// <summary>
/// Removes a texture from the Vram /// Removes a texture from the Vram
/// </summary> /// </summary>
@@ -397,41 +435,42 @@ namespace Vocaluxe.Lib.Draw
return; return;
lock (_Textures) lock (_Textures)
{ {
if (--texture.RefCount <= 0)
{
TTextureType t; TTextureType t;
if (_Textures.TryGetValue(texture.ID, out t)) if (_Textures.TryGetValue(texture.ID, out t))
{ {
if (t != null) if (t != null && --t.RefCount <= 0)
{
if(t.TexturePath!=null) _TextureCache.Remove(t.TexturePath);
t.Dispose(); t.Dispose();
_TextureCount--;
}
_Textures.Remove(texture.ID); _Textures.Remove(texture.ID);
} }
_TextureCache.Remove(texture.TexturePath);
}
texture.ID = -1; texture.ID = -1;
} }
texture = null; texture = null;
} }
protected void _CheckQueue() private void _CheckQueue()
{ {
lock (_Textures) lock (_Textures)
{ {
while (_TexturesToLoad.Count > 0) while (_TexturesToLoad.Count > 0)
{ {
STextureQueue q = _TexturesToLoad.Dequeue(); STextureQueue q = _TexturesToLoad.Dequeue();
if (!_Textures.ContainsKey(q.Texture.ID)) if (!_Textures.ContainsKey(q.TextureID))
continue; continue;
TTextureType t = _CreateTexture(q.Texture, q.Data); TTextureType texture = _CreateTexture(q.DataSize, q.Data);
_Textures[q.Texture.ID] = t; texture.RefCount = 1; //Currently no more than 1 reference to queued textures is possible
_Textures[q.TextureID] = texture;
} }
} }
} }
public int GetTextureCount() public int GetTextureCount()
{ {
return _Textures.Count; return _TextureCount;
} }
#endregion #endregion
+1 -1
View File
@@ -15,7 +15,7 @@ namespace Vocaluxe.Lib.Draw
MessageEventHandler OnMessage { set; } MessageEventHandler OnMessage { set; }
} }
abstract class CDrawBaseWindows<TTextureType> : CDrawBase<TTextureType> where TTextureType : class, IDisposable abstract class CDrawBaseWindows<TTextureType> : CDrawBase<TTextureType> where TTextureType : CTextureBase, IDisposable
{ {
private struct SClientRect private struct SClientRect
{ {
+9 -12
View File
@@ -45,6 +45,7 @@ namespace Vocaluxe.Lib.Draw
private Rectangle _Bounds; private Rectangle _Bounds;
private readonly Dictionary<int, CTextureRef> _Textures = new Dictionary<int, CTextureRef>(); private readonly Dictionary<int, CTextureRef> _Textures = new Dictionary<int, CTextureRef>();
private readonly Dictionary<string, CTextureRef> _TextureCache = new Dictionary<string, CTextureRef>();
private readonly List<Bitmap> _Bitmaps = new List<Bitmap>(); private readonly List<Bitmap> _Bitmaps = new List<Bitmap>();
private readonly Color _ClearColor = Color.DarkBlue; private readonly Color _ClearColor = Color.DarkBlue;
@@ -64,7 +65,6 @@ namespace Vocaluxe.Lib.Draw
_G = Graphics.FromImage(_Backbuffer); _G = Graphics.FromImage(_Backbuffer);
_G.Clear(Color.DarkBlue); _G.Clear(Color.DarkBlue);
Paint += _OnPaintEvent;
Closing += _OnClosingEvent; Closing += _OnClosingEvent;
KeyDown += _OnKeyDownEvent; KeyDown += _OnKeyDownEvent;
KeyPress += _OnKeyPressEvent; KeyPress += _OnKeyPressEvent;
@@ -107,8 +107,6 @@ namespace Vocaluxe.Lib.Draw
frontBuffer.DrawImage(_Backbuffer, new Rectangle(x, y, w, h), new Rectangle(0, 0, _Backbuffer.Width, _Backbuffer.Height), GraphicsUnit.Pixel); frontBuffer.DrawImage(_Backbuffer, new Rectangle(x, y, w, h), new Rectangle(0, 0, _Backbuffer.Width, _Backbuffer.Height), GraphicsUnit.Pixel);
} }
private void _OnPaintEvent(object sender, PaintEventArgs e) {}
#region FullScreenStuff #region FullScreenStuff
private void _ToggleFullScreen() private void _ToggleFullScreen()
{ {
@@ -293,7 +291,7 @@ namespace Vocaluxe.Lib.Draw
public void CopyScreen(ref CTextureRef texture) public void CopyScreen(ref CTextureRef texture)
{ {
if (!_TextureExists(texture) || texture.W2 != GetScreenWidth() || texture.H2 != GetScreenHeight()) if (!_TextureExists(texture) || texture.OrigSize.Width != GetScreenWidth() || texture.OrigSize.Height != GetScreenHeight())
{ {
RemoveTexture(ref texture); RemoveTexture(ref texture);
texture = CopyScreen(); texture = CopyScreen();
@@ -332,23 +330,22 @@ namespace Vocaluxe.Lib.Draw
private CTextureRef _GetNewTexture(int w, int h) private CTextureRef _GetNewTexture(int w, int h)
{ {
return new CTextureRef(_Bitmaps.Count - 1, new Size(w, h), new Size(w, h)); return new CTextureRef(_Bitmaps.Count - 1, new Size(w, h));
} }
public CTextureRef AddTexture(string texturePath) public CTextureRef AddTexture(string texturePath)
{ {
if (File.Exists(texturePath)) if (File.Exists(texturePath))
{ {
foreach (CTextureRef tex in _Textures.Values) CTextureRef texture;
{ if (_TextureCache.TryGetValue(texturePath, out texture))
if (tex.TexturePath == texturePath) return texture;
return tex;
}
using (var bmp = new Bitmap(texturePath)) using (var bmp = new Bitmap(texturePath))
{ {
CTextureRef texture = AddTexture(bmp); texture = AddTexture(bmp);
texture.TexturePath = texturePath; _TextureCache.Add(texturePath,texture);
} }
return texture;
} }
return null; return null;
+56 -55
View File
@@ -60,19 +60,19 @@ namespace Vocaluxe.Lib.Draw
} }
} }
class CGLTexture : IDisposable class COGLTexture : CTextureBase,IDisposable
{ {
public int PBO; public int PBO;
//The texture "name" according to the specs //The texture "name" according to the specs
public readonly int Name; public readonly int Name;
public CGLTexture(int name) private bool _IsDisposed;
public COGLTexture( int name, Size dataSize, int texWidth = 0, int texHeight = 0) : base(dataSize, texWidth, texHeight)
{ {
Name = name; Name = name;
} }
private bool _IsDisposed;
public void Dispose() public void Dispose()
{ {
if (_IsDisposed) if (_IsDisposed)
@@ -81,10 +81,11 @@ namespace Vocaluxe.Lib.Draw
GL.DeleteTexture(Name); GL.DeleteTexture(Name);
if (PBO > 0) if (PBO > 0)
GL.DeleteBuffers(1, ref PBO); GL.DeleteBuffers(1, ref PBO);
RefCount = 0;
} }
} }
class COpenGL : CDrawBaseWindows<CGLTexture>, IDraw class COpenGL : CDrawBaseWindows<COGLTexture>, IDraw
{ {
#region private vars #region private vars
private readonly GLControl _Control; private readonly GLControl _Control;
@@ -277,39 +278,34 @@ namespace Vocaluxe.Lib.Draw
public CTextureRef CopyScreen() public CTextureRef CopyScreen()
{ {
CTextureRef texture = _GetNewTextureRef(_W, _H); //TODO: Check if _W,_H needs to be used or not
Size size = new Size(GetScreenWidth(), GetScreenHeight());
CGLTexture t = new CGLTexture(GL.GenTexture()); COGLTexture texture = _CreateTexture(size);
GL.BindTexture(TextureTarget.Texture2D, t.Name); GL.BindTexture(TextureTarget.Texture2D, texture.Name);
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, 0, 0, size.Width,size.Height);//TODO: Use _X,_Y and _W,_H?
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.BindTexture(TextureTarget.Texture2D, 0); GL.BindTexture(TextureTarget.Texture2D, 0);
// Add to Texture List return _GetTextureReference(size, texture);
lock (_Textures)
{
_Textures[texture.ID] = t;
}
return texture;
} }
public void CopyScreen(ref CTextureRef texture) public void CopyScreen(ref CTextureRef textureRef)
{ {
COGLTexture texture;
//Check for actual texture sizes as it may be downsized compared to OrigSize //Check for actual texture sizes as it may be downsized compared to OrigSize
if (!_TextureExists(texture) || texture.DataSize.Width != GetScreenWidth() || texture.DataSize.Height != GetScreenHeight()) if (!_GetTexture(textureRef, out texture) || texture.DataSize.Width != GetScreenWidth() || texture.DataSize.Height != GetScreenHeight())
{ {
RemoveTexture(ref texture); RemoveTexture(ref textureRef);
texture = CopyScreen(); textureRef = CopyScreen();
} }
else else
{ {
GL.BindTexture(TextureTarget.Texture2D, _Textures[texture.ID].Name); GL.BindTexture(TextureTarget.Texture2D, texture.Name);
GL.CopyTexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, 0, 0, GetScreenWidth(), GetScreenHeight()); GL.CopyTexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, 0, 0, GetScreenWidth(), GetScreenHeight());
@@ -419,11 +415,16 @@ namespace Vocaluxe.Lib.Draw
#region Textures #region Textures
#region adding #region adding
protected override CGLTexture _CreateTexture(CTextureRef texture, IntPtr data) private COGLTexture _CreateTexture(Size dataSize)
{ {
CGLTexture t = new CGLTexture(GL.GenTexture()); return new COGLTexture(GL.GenTexture(), dataSize, _CheckForNextPowerOf2(dataSize.Width), _CheckForNextPowerOf2(dataSize.Height));
}
GL.BindTexture(TextureTarget.Texture2D, t.Name); protected override COGLTexture _CreateTexture(Size dataSize, IntPtr data)
{
COGLTexture texture = _CreateTexture(dataSize);
GL.BindTexture(TextureTarget.Texture2D, texture.Name);
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1); GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
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);
@@ -436,19 +437,19 @@ namespace Vocaluxe.Lib.Draw
GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D); GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, 0); GL.BindTexture(TextureTarget.Texture2D, 0);
return t; return texture;
} }
protected override CGLTexture _CreateTexture(CTextureRef texture, byte[] data) protected override COGLTexture _CreateTexture(Size dataSize, byte[] data)
{ {
CGLTexture t = new CGLTexture(GL.GenTexture()); COGLTexture texture = _CreateTexture(dataSize);
if (_UsePBO) if (_UsePBO)
{ {
try try
{ {
GL.GenBuffers(1, out t.PBO); GL.GenBuffers(1, out texture.PBO);
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, t.PBO); GL.BindBuffer(BufferTarget.PixelUnpackBuffer, texture.PBO);
GL.BufferData(BufferTarget.PixelUnpackBuffer, (IntPtr)data.Length, IntPtr.Zero, BufferUsageHint.StreamDraw); GL.BufferData(BufferTarget.PixelUnpackBuffer, (IntPtr)data.Length, IntPtr.Zero, BufferUsageHint.StreamDraw);
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0); GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
} }
@@ -459,10 +460,10 @@ namespace Vocaluxe.Lib.Draw
} }
} }
GL.BindTexture(TextureTarget.Texture2D, t.Name); GL.BindTexture(TextureTarget.Texture2D, texture.Name);
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, texture.OrigSize.Width, texture.OrigSize.Height, PixelFormat.Bgra, PixelType.UnsignedByte, data); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texture.DataSize.Width, texture.DataSize.Height, PixelFormat.Bgra, PixelType.UnsignedByte, data);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
@@ -471,15 +472,15 @@ namespace Vocaluxe.Lib.Draw
GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D); GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, 0); GL.BindTexture(TextureTarget.Texture2D, 0);
return t; return texture;
} }
#endregion adding #endregion adding
#region updating #region updating
public override bool UpdateTexture(CTextureRef texture, int w, int h, byte[] data) public override bool UpdateTexture(CTextureRef textureRef, int w, int h, byte[] data)
{ {
CGLTexture t = _GetTexture(texture); COGLTexture texture;
if (t == null) if (_GetTexture(textureRef,out texture))
return false; return false;
if (texture.DataSize.Width != w || texture.DataSize.Height != h) if (texture.DataSize.Width != w || texture.DataSize.Height != h)
{ {
@@ -492,14 +493,14 @@ namespace Vocaluxe.Lib.Draw
if (_UsePBO) if (_UsePBO)
{ {
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, t.PBO); GL.BindBuffer(BufferTarget.PixelUnpackBuffer, texture.PBO);
IntPtr buffer = GL.MapBuffer(BufferTarget.PixelUnpackBuffer, BufferAccess.WriteOnly); IntPtr buffer = GL.MapBuffer(BufferTarget.PixelUnpackBuffer, BufferAccess.WriteOnly);
Marshal.Copy(data, 0, buffer, data.Length); Marshal.Copy(data, 0, buffer, data.Length);
GL.UnmapBuffer(BufferTarget.PixelUnpackBuffer); GL.UnmapBuffer(BufferTarget.PixelUnpackBuffer);
GL.BindTexture(TextureTarget.Texture2D, t.Name); GL.BindTexture(TextureTarget.Texture2D, texture.Name);
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, w, h, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, w, h, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
@@ -511,7 +512,7 @@ namespace Vocaluxe.Lib.Draw
return true; return true;
} }
GL.BindTexture(TextureTarget.Texture2D, t.Name); GL.BindTexture(TextureTarget.Texture2D, texture.Name);
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);
@@ -538,16 +539,16 @@ namespace Vocaluxe.Lib.Draw
DrawTexture(texture, rect, texture.Color); DrawTexture(texture, rect, texture.Color);
} }
public void DrawTexture(CTextureRef texture, SRectF rect, SColorF color, bool mirrored = false) public void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, bool mirrored = false)
{ {
CGLTexture t = _GetTexture(texture); COGLTexture texture;
if (t == null) if (!_GetTexture(textureRef, out texture))
return; return;
if (Math.Abs(rect.W) < 1 || Math.Abs(rect.H) < 1 || Math.Abs(color.A) < 0.01) if (Math.Abs(rect.W) < 1 || Math.Abs(rect.H) < 1 || Math.Abs(color.A) < 0.01)
return; return;
GL.BindTexture(TextureTarget.Texture2D, t.Name); GL.BindTexture(TextureTarget.Texture2D, texture.Name);
float x1 = 0; float x1 = 0;
float x2 = texture.WidthRatio; float x2 = texture.WidthRatio;
@@ -601,10 +602,10 @@ namespace Vocaluxe.Lib.Draw
GL.BindTexture(TextureTarget.Texture2D, 0); GL.BindTexture(TextureTarget.Texture2D, 0);
} }
public void DrawTexture(CTextureRef texture, SRectF rect, SColorF color, SRectF bounds, bool mirrored = false) public void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, SRectF bounds, bool mirrored = false)
{ {
CGLTexture t = _GetTexture(texture); COGLTexture texture;
if (t == null) if (!_GetTexture(textureRef, out texture))
return; return;
if (Math.Abs(rect.W) < 1 || Math.Abs(rect.H) < 1 || Math.Abs(bounds.H) < 1 || Math.Abs(bounds.W) < 1 || if (Math.Abs(rect.W) < 1 || Math.Abs(rect.H) < 1 || Math.Abs(bounds.H) < 1 || Math.Abs(bounds.W) < 1 ||
@@ -617,7 +618,7 @@ namespace Vocaluxe.Lib.Draw
if (bounds.Y > rect.Y + rect.H || bounds.Y + bounds.H < rect.Y) if (bounds.Y > rect.Y + rect.H || bounds.Y + bounds.H < rect.Y)
return; return;
GL.BindTexture(TextureTarget.Texture2D, t.Name); GL.BindTexture(TextureTarget.Texture2D, texture.Name);
float x1 = (bounds.X - rect.X) / rect.W * texture.WidthRatio; float x1 = (bounds.X - rect.X) / rect.W * texture.WidthRatio;
float x2 = (bounds.X + bounds.W - rect.X) / rect.W * texture.WidthRatio; float x2 = (bounds.X + bounds.W - rect.X) / rect.W * texture.WidthRatio;
@@ -696,13 +697,13 @@ namespace Vocaluxe.Lib.Draw
GL.BindTexture(TextureTarget.Texture2D, 0); GL.BindTexture(TextureTarget.Texture2D, 0);
} }
public void DrawTexture(CTextureRef texture, SRectF rect, SColorF color, float begin, float end) public void DrawTexture(CTextureRef textureRef, SRectF rect, SColorF color, float begin, float end)
{ {
CGLTexture t = _GetTexture(texture); COGLTexture texture;
if (t == null) if (!_GetTexture(textureRef, out texture))
return; return;
GL.BindTexture(TextureTarget.Texture2D, t.Name); GL.BindTexture(TextureTarget.Texture2D, texture.Name);
GL.Enable(EnableCap.Blend); GL.Enable(EnableCap.Blend);
GL.Color4(color.R, color.G, color.B, color.A * CGraphics.GlobalAlpha); GL.Color4(color.R, color.G, color.B, color.A * CGraphics.GlobalAlpha);
@@ -729,10 +730,10 @@ namespace Vocaluxe.Lib.Draw
GL.BindTexture(TextureTarget.Texture2D, 0); GL.BindTexture(TextureTarget.Texture2D, 0);
} }
public void DrawTextureReflection(CTextureRef texture, SRectF rect, SColorF color, SRectF bounds, float space, float height) public void DrawTextureReflection(CTextureRef textureRef, SRectF rect, SColorF color, SRectF bounds, float space, float height)
{ {
CGLTexture t = _GetTexture(texture); COGLTexture texture;
if (t == null) if (!_GetTexture(textureRef, out texture))
return; return;
if (Math.Abs(rect.W) < float.Epsilon || Math.Abs(rect.H) < float.Epsilon || Math.Abs(bounds.H) < float.Epsilon || Math.Abs(bounds.W) < float.Epsilon || if (Math.Abs(rect.W) < float.Epsilon || Math.Abs(rect.H) < float.Epsilon || Math.Abs(bounds.H) < float.Epsilon || Math.Abs(bounds.W) < float.Epsilon ||
@@ -748,7 +749,7 @@ namespace Vocaluxe.Lib.Draw
if (height > bounds.H) if (height > bounds.H)
height = bounds.H; height = bounds.H;
GL.BindTexture(TextureTarget.Texture2D, t.Name); GL.BindTexture(TextureTarget.Texture2D, texture.Name);
float x1 = (bounds.X - rect.X) / rect.W * texture.WidthRatio; float x1 = (bounds.X - rect.X) / rect.W * texture.WidthRatio;
float x2 = (bounds.X + bounds.W - rect.X) / rect.W * texture.WidthRatio; float x2 = (bounds.X + bounds.W - rect.X) / rect.W * texture.WidthRatio;
+102
View File
@@ -0,0 +1,102 @@
using System.Drawing;
namespace Vocaluxe.Lib.Draw
{
abstract class CTextureBase
{
public int RefCount;
public string TexturePath;
private Size _DataSize;
/// <summary>
/// Size of data used (mostly equal to OrigSize but due to resizing this might change)
/// </summary>
public Size DataSize
{
get { return _DataSize; }
set
{
_DataSize = value;
_CalcRatios();
}
}
private int _W2, _H2;
/// <summary>
/// Internal texture width (on device), a power of 2 if necessary
/// </summary>
public int W2
{
get { return _W2; }
set
{
_W2 = value;
_CalcRatios();
}
}
/// <summary>
/// Internal texture height (on device), a power of 2 if necessary
/// </summary>
public int H2
{
get { return _H2; }
set
{
_H2 = value;
_CalcRatios();
}
}
/// <summary>
/// Internal use. Specifies which part of texture memory is actually used
/// </summary>
public float WidthRatio { get; private set; }
/// <summary>
/// Internal use. Specifies which part of texture memory is actually used
/// </summary>
public float HeightRatio { get; private set; }
/// <summary>
/// Creates a new texture reference
/// </summary>
/// <param name="dataSize">Size of the data used</param>
/// <param name="texWidth">Width in video memory</param>
/// <param name="texHeight">Height in video memory</param>
public CTextureBase(Size dataSize, int texWidth = 0, int texHeight = 0)
{
_DataSize = dataSize;
_W2 = (texWidth > 0) ? texWidth : dataSize.Width;
_H2 = (texHeight > 0) ? texHeight : dataSize.Height;
_CalcRatios();
}
public bool IsValid()
{
return RefCount > 0;
}
private void _CalcRatios()
{
// OpenGL has a problem with partial (NPOT) textures, so we remove 1 pixel
if (_DataSize.Width == _W2)
WidthRatio = 1f;
else
{
int mod = 0;
if (_DataSize.Width > 1)
mod = -1;
WidthRatio = (float)(_DataSize.Width + mod) / _W2;
}
if (_DataSize.Height == _H2)
HeightRatio = 1f;
else
{
int mod = 0;
if (_DataSize.Height > 1)
mod = -1;
HeightRatio = (float)(_DataSize.Height + mod) / _H2;
}
}
}
}
+1
View File
@@ -226,6 +226,7 @@
<Compile Include="Lib\Database\CResourceDB.cs" /> <Compile Include="Lib\Database\CResourceDB.cs" />
<Compile Include="Lib\Draw\CDrawBase.cs" /> <Compile Include="Lib\Draw\CDrawBase.cs" />
<Compile Include="Lib\Draw\CDrawBaseWindows.cs" /> <Compile Include="Lib\Draw\CDrawBaseWindows.cs" />
<Compile Include="Lib\Draw\CTextureBase.cs" />
<Compile Include="Lib\Sound\Playback\CAudioStreamBase.cs" /> <Compile Include="Lib\Sound\Playback\CAudioStreamBase.cs" />
<Compile Include="Lib\Sound\Playback\CPlaybackBase.cs" /> <Compile Include="Lib\Sound\Playback\CPlaybackBase.cs" />
<Compile Include="Lib\Sound\Playback\CRingBuffer.cs" /> <Compile Include="Lib\Sound\Playback\CRingBuffer.cs" />
+1 -84
View File
@@ -25,9 +25,6 @@ namespace VocaluxeLib.Draw
public class CTextureRef public class CTextureRef
{ {
public int ID; public int ID;
public int RefCount = 1;
public string TexturePath = "";
/// <summary> /// <summary>
/// Size of original image (e.g. of bmp) /// Size of original image (e.g. of bmp)
@@ -39,20 +36,6 @@ namespace VocaluxeLib.Draw
get { return (float)OrigSize.Width / OrigSize.Height; } get { return (float)OrigSize.Width / OrigSize.Height; }
} }
private Size _DataSize;
/// <summary>
/// Size of data used (mostly equal to OrigSize but due to resizing this might change)
/// </summary>
public Size DataSize
{
get { return _DataSize; }
set
{
_DataSize = value;
_CalcRatios();
}
}
/// <summary> /// <summary>
/// Current size when drawn /// Current size when drawn
/// </summary> /// </summary>
@@ -60,60 +43,17 @@ namespace VocaluxeLib.Draw
public SColorF Color = new SColorF(1f, 1f, 1f, 1f); public SColorF Color = new SColorF(1f, 1f, 1f, 1f);
private int _W2, _H2;
/// <summary>
/// Internal texture width (on device), a power of 2 if necessary
/// </summary>
public int W2
{
get { return _W2; }
set
{
_W2 = value;
_CalcRatios();
}
}
/// <summary>
/// Internal texture height (on device), a power of 2 if necessary
/// </summary>
public int H2
{
get { return _H2; }
set
{
_H2 = value;
_CalcRatios();
}
}
/// <summary>
/// Internal use. Specifies which part of texture memory is actually used
/// </summary>
public float WidthRatio { get; private set; }
/// <summary>
/// Internal use. Specifies which part of texture memory is actually used
/// </summary>
public float HeightRatio { get; private set; }
/// <summary> /// <summary>
/// Creates a new texture reference /// Creates a new texture reference
/// </summary> /// </summary>
/// <param name="id">ID of the texture</param> /// <param name="id">ID of the texture</param>
/// <param name="origSize">Original size (Bitmap size)</param> /// <param name="origSize">Original size (Bitmap size)</param>
/// <param name="dataSize">Size of the data used</param> public CTextureRef(int id, Size origSize)
/// <param name="texWidth">Width in video memory</param>
/// <param name="texHeight">Height in video memory</param>
public CTextureRef(int id, Size origSize, Size dataSize, int texWidth = 0, int texHeight = 0)
{ {
ID = id; ID = id;
_DataSize = dataSize;
OrigSize = origSize; OrigSize = origSize;
_W2 = (texWidth > 0) ? texWidth : dataSize.Width;
_H2 = (texHeight > 0) ? texHeight : dataSize.Height;
Rect = new SRectF(0f, 0f, origSize.Width, origSize.Height, 0f); Rect = new SRectF(0f, 0f, origSize.Width, origSize.Height, 0f);
_CalcRatios();
} }
~CTextureRef() ~CTextureRef()
@@ -125,28 +65,5 @@ namespace VocaluxeLib.Draw
CBase.Drawing.RemoveTexture(ref tmp); CBase.Drawing.RemoveTexture(ref tmp);
} }
} }
private void _CalcRatios()
{
// OpenGL has a problem with partial (NPOT) textures, so we remove 1 pixel
if (_DataSize.Width == _W2)
WidthRatio = 1f;
else
{
int mod = 0;
if (_DataSize.Width > 1)
mod = -1;
WidthRatio = (float)(_DataSize.Width + mod) / _W2;
}
if (_DataSize.Height == _H2)
HeightRatio = 1f;
else
{
int mod = 0;
if (_DataSize.Height > 1)
mod = -1;
HeightRatio = (float)(_DataSize.Height + mod) / _H2;
}
}
} }
} }