Skip to content

Commit

Permalink
fix gdi+ lua rendering and prescale option
Browse files Browse the repository at this point in the history
  • Loading branch information
zeromus committed Jul 25, 2015
1 parent e73c781 commit f8b840e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 19 deletions.
2 changes: 0 additions & 2 deletions BizHawk.Client.MultiHawk/DisplayManager/DisplayManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ FilterProgram BuildDefaultChain(Size chain_insize, Size chain_outsize, bool incl
if (Global.Config.DispFinalFilter == 1) finalFilter = BizHawk.Client.EmuHawk.Filters.FinalPresentation.eFilterOption.Bilinear;
if (Global.Config.DispFinalFilter == 2) finalFilter = BizHawk.Client.EmuHawk.Filters.FinalPresentation.eFilterOption.Bicubic;

finalFilter = BizHawk.Client.EmuHawk.Filters.FinalPresentation.eFilterOption.None;

fPresent.FilterOption = finalFilter;

//add final presentation
Expand Down
68 changes: 55 additions & 13 deletions Bizware/BizHawk.Bizware.BizwareGL.GdiPlus/GdiPlusGuiRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,34 @@ public void DrawSubrect(Texture2d tex, float x, float y, float w, float h, float
float y0 = v0 * tex.Height;
float x1 = u1 * tex.Width;
float y1 = v1 * tex.Height;

//==========HACKY COPYPASTE=============

//first we need to make a transform that will change us from the default GDI+ transformation (a top left identity transformation) to an opengl-styled one
//(this is necessary because a 'GuiProjectionMatrix' call doesnt have any sense of the size of the destination viewport it's meant for)
var vcb = g.VisibleClipBounds;
float vw = vcb.Width;
float vh = vcb.Height;
Matrix4 fixmat = Matrix4.CreateTranslation(vw / 2, -vh / 2, 0);
fixmat *= Matrix4.CreateScale(vw / 2, -vh / 2, 1);

//------------------
//( reminder: this is just an experiment: we need to turn this into a transform on the GraphicsDevice )
//------------------
Matrix4 mat = Projection.Top * Modelview.Top * fixmat;
var tl = new Vector3(x, y, 0);
var tr = new Vector3(x + w, y, 0);
var bl = new Vector3(x, y + h, 0);
tl = Vector3.Transform(tl, mat);
tr = Vector3.Transform(tr, mat);
bl = Vector3.Transform(bl, mat);

//=======================================

sd.PointF[] destPoints = new sd.PointF[] {
new sd.PointF(x,y),
new sd.PointF(x+w,y),
new sd.PointF(x,y+h),
tl.ToSDPointf(),
tr.ToSDPointf(),
bl.ToSDPointf(),
};

g.DrawImage(tw.SDBitmap, destPoints, new sd.RectangleF(x0, y0, x1 - x0, y1 - y0), sd.GraphicsUnit.Pixel, CurrentImageAttributes);
Expand Down Expand Up @@ -234,19 +258,37 @@ unsafe void DrawInternal(Texture2d tex, float x, float y, float w, float h)
var g = Gdi.GetCurrentGraphics();
PrepDraw(g, tw);

//first we need to make a transform that will change us from the default GDI+ transformation (a top left identity transformation) to an opengl-styled one
//(this is necessary because a 'GuiProjectionMatrix' call doesnt have any sense of the size of the destination viewport it's meant for)
var vcb = g.VisibleClipBounds;
float vw = vcb.Width;
float vh = vcb.Height;
Matrix4 fixmat = Matrix4.CreateTranslation(vw / 2, -vh / 2, 0);
fixmat *= Matrix4.CreateScale(vw / 2, -vh / 2, 1);

//------------------
//( reminder: this is just an experiment: we need to turn this into a transform on the GraphicsDevice )
//------------------
Matrix4 mat = Projection.Top * Modelview.Top * fixmat;
var tl = new Vector3(x, y, 0);
var tr = new Vector3(x + w, y, 0);
var bl = new Vector3(x, y + h, 0);
tl = Vector3.Transform(tl, mat);
tr = Vector3.Transform(tr, mat);
bl = Vector3.Transform(bl, mat);

//a little bit of a fastpath.. I think it's safe
//SO WHY DIDNT IT WORK?
//anyway, it would interfere with the transforming
//if (w == tex.Width && h == tex.Height && x == (int)x && y == (int)y)
// g.DrawImageUnscaled(tw.SDBitmap, (int)x, (int)y);
//else
{
sd.PointF[] destPoints = new sd.PointF[] {
new sd.PointF(x,y),
new sd.PointF(x+w,y),
new sd.PointF(x,y+h),
};
//g.DrawImage(tw.SDBitmap, x, y, w, h); //original
g.DrawImage(tw.SDBitmap, destPoints, new sd.RectangleF(0, 0, tex.Width, tex.Height), sd.GraphicsUnit.Pixel, CurrentImageAttributes);
}
sd.PointF[] destPoints = new sd.PointF[] {
tl.ToSDPointf(),
tr.ToSDPointf(),
bl.ToSDPointf(),
};

g.DrawImage(tw.SDBitmap, destPoints, new sd.RectangleF(0, 0, tex.Width, tex.Height), sd.GraphicsUnit.Pixel, CurrentImageAttributes);
}

unsafe void DrawInternal(Art art, float x, float y, float w, float h, bool fx, bool fy)
Expand Down
31 changes: 27 additions & 4 deletions Bizware/BizHawk.Bizware.BizwareGL.GdiPlus/IGL_GdiPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ void IDisposable.Dispose()

public void Clear(OpenTK.Graphics.OpenGL.ClearBufferMask mask)
{
var g = GetCurrentGraphics();
if((mask & ClearBufferMask.ColorBufferBit) != 0)
{
g.Clear(_currentClearColor);
}
}

public string API { get { return "GDIPLUS"; } }
Expand All @@ -94,9 +99,10 @@ public void Clear(OpenTK.Graphics.OpenGL.ClearBufferMask mask)
return null;
}

private sd.Color _currentClearColor = Color.Transparent;
public void SetClearColor(sd.Color color)
{

_currentClearColor = color;
}

public unsafe void BindArrayData(void* pData)
Expand Down Expand Up @@ -349,9 +355,10 @@ public RenderTargetWrapper(IGL_GdiPlus gdi)

public BufferedGraphics MyBufferedGraphics;

public Graphics refGraphics; //?? hacky?

public void CreateGraphics()
{
Graphics refGraphics;
Rectangle r;
if (Control != null)
{
Expand Down Expand Up @@ -392,13 +399,17 @@ public void FreeRenderTarget(RenderTarget rt)
{
int id = rt.Id.ToInt32();
var rtw = ResourceIDs.Lookup[id] as RenderTargetWrapper;
rtw.Target.Dispose();
ResourceIDs.Free(rt.Id);
}

public unsafe RenderTarget CreateRenderTarget(int w, int h)
{
Texture2d tex = null;
TextureWrapper tw = new TextureWrapper();
tw.SDBitmap = new Bitmap(w,h, sdi.PixelFormat.Format32bppArgb);
IntPtr texid = GenTexture();
ResourceIDs.Lookup[texid.ToInt32()] = tw;
var tex = new Texture2d(this, texid, null, w, h);

var rt = new RenderTarget(this, ResourceIDs.Alloc(ResourceIdManager.EResourceType.RenderTarget), tex);
int id = rt.Id.ToInt32();
RenderTargetWrapper rtw = new RenderTargetWrapper(this);
Expand All @@ -409,6 +420,16 @@ public unsafe RenderTarget CreateRenderTarget(int w, int h)

public void BindRenderTarget(RenderTarget rt)
{
if (CurrentRenderTargetWrapper != null)
{
if (CurrentRenderTargetWrapper == CurrentControl.RenderTargetWrapper)
{
//dont do anything til swapbuffers
}
else
CurrentRenderTargetWrapper.MyBufferedGraphics.Render();
}

if (rt == null)
{
//null means to use the default RT for the current control
Expand All @@ -417,6 +438,8 @@ public void BindRenderTarget(RenderTarget rt)
else
{
CurrentRenderTargetWrapper = RenderTargetWrapperForRt(rt);
if (CurrentRenderTargetWrapper.MyBufferedGraphics == null)
CurrentRenderTargetWrapper.CreateGraphics();
}
}

Expand Down
13 changes: 13 additions & 0 deletions Bizware/BizHawk.Bizware.BizwareGL/BitmapBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ int nexthigher(int k)
/// </summary>
public unsafe Bitmap ToSysdrawingBitmap()
{
if (WrappedBitmap != null)
return (Bitmap)WrappedBitmap.Clone();
var pf = PixelFormat.Format32bppArgb;
if (!HasAlpha)
pf = PixelFormat.Format24bppRgb;
Expand All @@ -491,6 +493,17 @@ public unsafe Bitmap ToSysdrawingBitmap()
/// </summary>
public unsafe void ToSysdrawingBitmap(Bitmap bmp)
{
if (WrappedBitmap != null)
{
using (var g = Graphics.FromImage(bmp))
{
g.CompositingMode = sd.Drawing2D.CompositingMode.SourceCopy;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
g.DrawImageUnscaled(WrappedBitmap, 0, 0);
return;
}
}

//note: we lock it as 32bpp even if the bitmap is 24bpp so we can write to it more conveniently.
var bmpdata = bmp.LockBits(new sd.Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

Expand Down
4 changes: 4 additions & 0 deletions Bizware/BizHawk.Bizware.BizwareGL/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ public static Vector2 ToVector2(this Size size)
{
return new Vector2(size.Width, size.Height);
}
public static PointF ToSDPointf(this Vector3 v)
{
return new PointF(v.X, v.Y);
}
}
}

0 comments on commit f8b840e

Please sign in to comment.