Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internalise Texture/TextureGL DrawX() methods #2475

Merged
merged 4 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/Audio/WaveformGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public override void Draw(Action<TexturedVertex2D> vertexAction)
}

quadToDraw *= DrawInfo.Matrix;
texture.DrawQuad(quadToDraw, colour, null, vertexBatch.AddAction, Vector2.Divide(localInflationAmount, quadToDraw.Size));
DrawQuad(texture, quadToDraw, colour, null, vertexBatch.AddAction, Vector2.Divide(localInflationAmount, quadToDraw.Size));
}

shader.Unbind();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void drawFrameBufferToBackBuffer(FrameBuffer frameBuffer, RectangleF dra
RectangleF textureRect = new RectangleF(0, frameBuffer.Texture.Height, frameBuffer.Texture.Width, -frameBuffer.Texture.Height);
if (frameBuffer.Texture.Bind())
// Color was already applied by base.Draw(); no need to re-apply. Thus we use White here.
frameBuffer.Texture.DrawQuad(drawRectangle, textureRect, colourInfo);
DrawQuad(frameBuffer.Texture, drawRectangle, colourInfo, textureRect);
}

private void drawChildren(Action<TexturedVertex2D> vertexAction, Vector2 frameBufferSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ private void drawEdgeEffect()
colour.TopRight.MultiplyAlpha(DrawColourInfo.Colour.TopRight.Linear.A);
colour.BottomRight.MultiplyAlpha(DrawColourInfo.Colour.BottomRight.Linear.A);

Texture.WhitePixel.DrawQuad(
DrawQuad(
Texture.WhitePixel,
screenSpaceMaskingQuad.Value,
colour, null, null, null,
// HACK HACK HACK. We re-use the unused vertex blend range to store the original
Expand Down
65 changes: 65 additions & 0 deletions osu.Framework/Graphics/DrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@

using osu.Framework.Graphics.OpenGL;
using System;
using System.Runtime.CompilerServices;
using osu.Framework.Graphics.Batches;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.OpenGL.Textures;
using osu.Framework.Graphics.OpenGL.Vertices;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Textures;
using osu.Framework.Threading;
using osuTK;

namespace osu.Framework.Graphics
{
Expand Down Expand Up @@ -71,6 +78,64 @@ public virtual void Draw(Action<TexturedVertex2D> vertexAction)
GLWrapper.SetBlend(DrawColourInfo.Blending);
}

/// <summary>
/// Draws a triangle to the screen.
/// </summary>
/// <param name="texture">The texture to fill the triangle with.</param>
/// <param name="vertexTriangle">The triangle to draw.</param>
/// <param name="textureRect">The texture rectangle.</param>
/// <param name="drawColour">The vertex colour.</param>
/// <param name="vertexAction">An action that adds vertices to a <see cref="VertexBatch{T}"/>.</param>
/// <param name="inflationPercentage">The percentage amount that <see cref="textureRect"/> should be inflated.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void DrawTriangle(Texture texture, Triangle vertexTriangle, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null,
Vector2? inflationPercentage = null)
=> texture.DrawTriangle(vertexTriangle, drawColour, textureRect, vertexAction, inflationPercentage);

/// <summary>
/// Draws a triangle to the screen.
/// </summary>
/// <param name="texture">The texture to fill the triangle with.</param>
/// <param name="vertexTriangle">The triangle to draw.</param>
/// <param name="drawColour">The vertex colour.</param>
/// <param name="textureRect">The texture rectangle.</param>
/// <param name="vertexAction">An action that adds vertices to a <see cref="VertexBatch{T}"/>.</param>
/// <param name="inflationPercentage">The percentage amount that <see cref="textureRect"/> should be inflated.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void DrawTriangle(TextureGL texture, Triangle vertexTriangle, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null,
Vector2? inflationPercentage = null)
=> texture.DrawTriangle(vertexTriangle, drawColour, textureRect, vertexAction, inflationPercentage);

/// <summary>
/// Draws a quad to the screen.
/// </summary>
/// <param name="texture">The texture to fill the triangle with.</param>
/// <param name="vertexQuad">The quad to draw.</param>
/// <param name="textureRect">The texture rectangle.</param>
/// <param name="drawColour">The vertex colour.</param>
/// <param name="vertexAction">An action that adds vertices to a <see cref="VertexBatch{T}"/>.</param>
/// <param name="inflationPercentage">The percentage amount that <see cref="textureRect"/> should be inflated.</param>
/// <param name="blendRangeOverride">The range over which the edges of the <see cref="textureRect"/> should be blended.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void DrawQuad(Texture texture, Quad vertexQuad, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null,
Vector2? inflationPercentage = null, Vector2? blendRangeOverride = null)
=> texture.DrawQuad(vertexQuad, drawColour, textureRect, vertexAction, inflationPercentage, blendRangeOverride);

/// <summary>
/// Draws a quad to the screen.
/// </summary>
/// <param name="texture">The texture to fill the triangle with.</param>
/// <param name="vertexQuad">The quad to draw.</param>
/// <param name="drawColour">The vertex colour.</param>
/// <param name="textureRect">The texture rectangle.</param>
/// <param name="vertexAction">An action that adds vertices to a <see cref="VertexBatch{T}"/>.</param>
/// <param name="inflationPercentage">The percentage amount that <see cref="textureRect"/> should be inflated.</param>
/// <param name="blendRangeOverride">The range over which the edges of the <see cref="textureRect"/> should be blended.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void DrawQuad(TextureGL texture, Quad vertexQuad, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null,
Vector2? inflationPercentage = null, Vector2? blendRangeOverride = null)
=> texture.DrawQuad(vertexQuad, drawColour, textureRect, vertexAction, inflationPercentage, blendRangeOverride);

/// <summary>
/// Increments the reference count of this <see cref="DrawNode"/>, blocking <see cref="Dispose"/> until the count reaches 0.
/// Invoke <see cref="Dispose"/> to remove the reference.
Expand Down
24 changes: 19 additions & 5 deletions osu.Framework/Graphics/OpenGL/Textures/TextureGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Threading;
using osu.Framework.Graphics.Batches;
using osu.Framework.Graphics.Primitives;
using osuTK.Graphics.ES30;
using osuTK;
Expand Down Expand Up @@ -71,20 +72,33 @@ public void Dispose()
public abstract RectangleF GetTextureRect(RectangleF? textureRect);

/// <summary>
/// Blit a triangle to OpenGL display with specified parameters.
/// Draws a triangle to the screen.
/// </summary>
public abstract void DrawTriangle(Triangle vertexTriangle, RectangleF? textureRect, ColourInfo drawColour, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null);
/// <param name="vertexTriangle">The triangle to draw.</param>
/// <param name="drawColour">The vertex colour.</param>
/// <param name="textureRect">The texture rectangle.</param>
/// <param name="vertexAction">An action that adds vertices to a <see cref="VertexBatch{T}"/>.</param>
/// <param name="inflationPercentage">The percentage amount that <see cref="textureRect"/> should be inflated.</param>
internal abstract void DrawTriangle(Triangle vertexTriangle, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null,
Vector2? inflationPercentage = null);

/// <summary>
/// Blit a quad to OpenGL display with specified parameters.
/// Draws a quad to the screen.
/// </summary>
public abstract void DrawQuad(Quad vertexQuad, RectangleF? textureRect, ColourInfo drawColour, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null, Vector2? blendRangeOverride = null);
/// <param name="vertexQuad">The quad to draw.</param>
/// <param name="drawColour">The vertex colour.</param>
/// <param name="textureRect">The texture rectangle.</param>
/// <param name="vertexAction">An action that adds vertices to a <see cref="VertexBatch{T}"/>.</param>
/// <param name="inflationPercentage">The percentage amount that <see cref="textureRect"/> should be inflated.</param>
/// <param name="blendRangeOverride">The range over which the edges of the <see cref="textureRect"/> should be blended.</param>
internal abstract void DrawQuad(Quad vertexQuad, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null,
Vector2? blendRangeOverride = null);

/// <summary>
/// Bind as active texture.
/// </summary>
/// <returns>True if bind was successful.</returns>
public abstract bool Bind();
internal abstract bool Bind();

/// <summary>
/// Uploads pending texture data to the GPU if it exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public TextureGLAtlasWhite(TextureGLSingle parent)
{
}

public override bool Bind()
internal override bool Bind()
{
//we can use the special white space from any atlas texture.
if (GLWrapper.AtlasTextureIsBound)
Expand Down
8 changes: 4 additions & 4 deletions osu.Framework/Graphics/OpenGL/Textures/TextureGLSingle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public override RectangleF GetTextureRect(RectangleF? textureRect)
return texRect;
}

public override void DrawTriangle(Triangle vertexTriangle, RectangleF? textureRect, ColourInfo drawColour, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null)
internal override void DrawTriangle(Triangle vertexTriangle, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null)
{
if (!Available)
throw new ObjectDisposedException(ToString(), "Can not draw a triangle with a disposed texture.");
Expand Down Expand Up @@ -215,8 +215,8 @@ public override void DrawTriangle(Triangle vertexTriangle, RectangleF? textureRe
FrameStatistics.Add(StatisticsCounterType.Pixels, (long)vertexTriangle.ConservativeArea);
}

public override void DrawQuad(Quad vertexQuad, RectangleF? textureRect, ColourInfo drawColour, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null,
Vector2? blendRangeOverride = null)
internal override void DrawQuad(Quad vertexQuad, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null,
Vector2? blendRangeOverride = null)
{
if (!Available)
throw new ObjectDisposedException(ToString(), "Can not draw a quad with a disposed texture.");
Expand Down Expand Up @@ -298,7 +298,7 @@ public override void SetData(ITextureUpload upload)
}
}

public override bool Bind()
internal override bool Bind()
{
if (!Available)
throw new ObjectDisposedException(ToString(), "Can not bind a disposed texture.");
Expand Down
12 changes: 7 additions & 5 deletions osu.Framework/Graphics/OpenGL/Textures/TextureGLSub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ private RectangleF boundsInParent(RectangleF? textureRect)

public override RectangleF GetTextureRect(RectangleF? textureRect) => parent.GetTextureRect(boundsInParent(textureRect));

public override void DrawTriangle(Triangle vertexTriangle, RectangleF? textureRect, ColourInfo drawColour, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null)
internal override void DrawTriangle(Triangle vertexTriangle, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null,
Vector2? inflationPercentage = null)
{
parent.DrawTriangle(vertexTriangle, boundsInParent(textureRect), drawColour, vertexAction, inflationPercentage);
parent.DrawTriangle(vertexTriangle, drawColour, boundsInParent(textureRect), vertexAction, inflationPercentage);
}

public override void DrawQuad(Quad vertexQuad, RectangleF? textureRect, ColourInfo drawColour, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null, Vector2? blendRangeOverride = null)
internal override void DrawQuad(Quad vertexQuad, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null,
Vector2? blendRangeOverride = null)
{
parent.DrawQuad(vertexQuad, boundsInParent(textureRect), drawColour, vertexAction, inflationPercentage, blendRangeOverride);
parent.DrawQuad(vertexQuad, drawColour, boundsInParent(textureRect), vertexAction, inflationPercentage, blendRangeOverride);
}

internal override bool Upload() => false;
Expand All @@ -77,7 +79,7 @@ internal override void FlushUploads()
{
}

public override bool Bind()
internal override bool Bind()
{
if (!Available)
throw new ObjectDisposedException(ToString(), "Can not bind disposed sub textures.");
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/Shapes/Triangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public TriangleDrawNode(Triangle source)

protected override void Blit(Action<TexturedVertex2D> vertexAction)
{
Texture.DrawTriangle(toTriangle(ScreenSpaceDrawQuad), DrawColourInfo.Colour, null, null,
DrawTriangle(Texture, toTriangle(ScreenSpaceDrawQuad), DrawColourInfo.Colour, null, null,
new Vector2(InflationAmount.X / DrawRectangle.Width, InflationAmount.Y / DrawRectangle.Height));
}
}
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/Sprites/SpriteDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override void ApplyState()

protected virtual void Blit(Action<TexturedVertex2D> vertexAction)
{
Texture.DrawQuad(ScreenSpaceDrawQuad, DrawColourInfo.Colour, null, vertexAction,
DrawQuad(Texture, ScreenSpaceDrawQuad, DrawColourInfo.Colour, null, vertexAction,
new Vector2(InflationAmount.X / DrawRectangle.Width, InflationAmount.Y / DrawRectangle.Height));
}

Expand Down
4 changes: 2 additions & 2 deletions osu.Framework/Graphics/Sprites/SpriteText_DrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public override void Draw(Action<TexturedVertex2D> vertexAction)
shadowQuad.BottomLeft += shadowOffset;
shadowQuad.BottomRight += shadowOffset;

parts[i].Texture.DrawQuad(shadowQuad, finalShadowColour, vertexAction: vertexAction);
DrawQuad(parts[i].Texture, shadowQuad, finalShadowColour, vertexAction: vertexAction);
}

parts[i].Texture.DrawQuad(parts[i].DrawQuad, DrawColourInfo.Colour, vertexAction: vertexAction);
DrawQuad(parts[i].Texture, parts[i].DrawQuad, DrawColourInfo.Colour, vertexAction: vertexAction);
}

Shader.Unbind();
Expand Down
27 changes: 23 additions & 4 deletions osu.Framework/Graphics/Textures/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.IO;
using osu.Framework.Graphics.Batches;
using osu.Framework.Graphics.OpenGL.Textures;
using osu.Framework.Graphics.Primitives;
using osuTK;
Expand Down Expand Up @@ -112,18 +113,36 @@ protected virtual RectangleF TextureBounds(RectangleF? textureRect = null)

public RectangleF GetTextureRect(RectangleF? textureRect = null) => TextureGL.GetTextureRect(TextureBounds(textureRect));

public void DrawTriangle(Triangle vertexTriangle, ColourInfo colour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null)
/// <summary>
/// Draws a triangle to the screen.
/// </summary>
/// <param name="vertexTriangle">The triangle to draw.</param>
/// <param name="textureRect">The texture rectangle.</param>
/// <param name="drawColour">The vertex colour.</param>
/// <param name="vertexAction">An action that adds vertices to a <see cref="VertexBatch{T}"/>.</param>
/// <param name="inflationPercentage">The percentage amount that <see cref="textureRect"/> should be inflated.</param>
internal void DrawTriangle(Triangle vertexTriangle, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null)
{
if (TextureGL == null || !TextureGL.Bind()) return;

TextureGL.DrawTriangle(vertexTriangle, TextureBounds(textureRect), colour, vertexAction, inflationPercentage);
TextureGL.DrawTriangle(vertexTriangle, drawColour, TextureBounds(textureRect), vertexAction, inflationPercentage);
}

public void DrawQuad(Quad vertexQuad, ColourInfo colour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null, Vector2? blendRangeOverride = null)
/// <summary>
/// Draws a quad to the screen.
/// </summary>
/// <param name="vertexQuad">The quad to draw.</param>
/// <param name="textureRect">The texture rectangle.</param>
/// <param name="drawColour">The vertex colour.</param>
/// <param name="vertexAction">An action that adds vertices to a <see cref="VertexBatch{T}"/>.</param>
/// <param name="inflationPercentage">The percentage amount that <see cref="textureRect"/> should be inflated.</param>
/// <param name="blendRangeOverride">The range over which the edges of the <see cref="textureRect"/> should be blended.</param>
internal void DrawQuad(Quad vertexQuad, ColourInfo drawColour, RectangleF? textureRect = null, Action<TexturedVertex2D> vertexAction = null, Vector2? inflationPercentage = null,
Vector2? blendRangeOverride = null)
{
if (TextureGL == null || !TextureGL.Bind()) return;

TextureGL.DrawQuad(vertexQuad, TextureBounds(textureRect), colour, vertexAction, inflationPercentage, blendRangeOverride);
TextureGL.DrawQuad(vertexQuad, drawColour, TextureBounds(textureRect), vertexAction, inflationPercentage, blendRangeOverride);
}

public override string ToString() => $@"{AssetName} ({Width}, {Height})";
Expand Down