Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Trying to remove additional copy step when rendering by passing in a …
Browse files Browse the repository at this point in the history
…reference to the src pixel data container.
  • Loading branch information
jessefreeman committed Oct 23, 2020
1 parent 16f4271 commit 53d50da
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 137 deletions.
76 changes: 36 additions & 40 deletions SDK/Engine/Chips/Game/GameChipLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class GameChipLite : AbstractChip, IUpdate, IDraw
protected int[] tmpFontData = new int[0];

protected int[] tmpSpriteData = new int[0];
protected int tmpSpriteDataID = -1;
// protected int tmpSpriteDataID = -1;

protected int w;
protected int h;
Expand Down Expand Up @@ -430,7 +430,7 @@ public void Clear(int x = 0, int y = 0, int? width = null, int? height = null)
// h = height ?? DisplayChip.Height - y;
//
// DrawRect(x, y, w, h, ColorChip.backgroundColor);
DisplayChip.Clear();
DisplayChip.Clear(ColorChip.backgroundColor);

}

Expand Down Expand Up @@ -510,8 +510,11 @@ public Point Display(bool visible = true)
break;

default:

var data = new DrawPixelDataRequest(pixelData, width, height);
DisplayChip.NewDrawCall(data, x, y, width, height, (byte) drawMode, flipH, flipV, colorOffset);

DisplayChip.NewDrawCall(pixelData, x, y, width, height, (byte) drawMode, flipH, flipV, colorOffset);
// DisplayChip.NewDrawCall(pixelData, x, y, width, height, (byte) drawMode, flipH, flipV, colorOffset);

break;
}
Expand Down Expand Up @@ -559,21 +562,23 @@ public Point Display(bool visible = true)
/// </param>
public virtual void DrawSprite(int id, int x, int y, bool flipH = false, bool flipV = false,
DrawMode drawMode = DrawMode.Sprite, int colorOffset = 0, bool onScreen = true, bool useScrollPos = true,
Rectangle? bounds = null)
Rectangle? bounds = null, SpriteChip srcChip = null)
{
// Only apply the max sprite count to sprite draw modes

srcChip ??= SpriteChip;

if (drawMode == DrawMode.Tile)
{
Tile(x, y, id, colorOffset);
}
else if (drawMode == DrawMode.TilemapCache || drawMode == DrawMode.UI)
else if (drawMode == DrawMode.TilemapCache)
{
if (id != tmpSpriteDataID)
{
SpriteChip.ReadSpriteAt(id, ref tmpSpriteData);
tmpSpriteDataID = id;
}
// if (id != tmpSpriteDataID)
// {
srcChip.ReadSpriteAt(id, ref tmpSpriteData);
// tmpSpriteDataID = id;
// }

DrawPixels(tmpSpriteData, x, y, SpriteChip.width, SpriteChip.height, flipH, flipV, drawMode,
colorOffset);
Expand All @@ -587,9 +592,6 @@ public Point Display(bool visible = true)
{
_tmpBounds = bounds ?? DisplayChip.VisibleBounds;

// if (bounds == null)
// bounds = displayChip.visibleBounds;

// This can set the render flag to true or false based on it's location
//TODO need to take into account the current bounds of the screen
render = x >= _tmpBounds.X && x <= _tmpBounds.Width && y >= _tmpBounds.Y && y <= _tmpBounds.Height;
Expand All @@ -603,18 +605,13 @@ public Point Display(bool visible = true)
// If the sprite should be rendered, call DrawSprite()
if (render)
{
//TODO flipping H, V and colorOffset should all be passed into reading a sprite
if (id != tmpSpriteDataID)
{
SpriteChip.ReadSpriteAt(id, ref tmpSpriteData);
tmpSpriteDataID = id;
}

DrawPixels(tmpSpriteData, x, y, SpriteChip.width, SpriteChip.height, flipH, flipV, drawMode,
colorOffset);

// DisplayChip.DrawSprite(id, x, y, flipH, flipV, (byte)drawMode, colorOffset);


var pos = MathUtil.CalculatePosition(id, srcChip.Columns);
pos.X *= SpriteChip.width;
pos.Y *= SpriteChip.height;

DisplayChip.NewDrawCall(srcChip, x, y, SpriteChip.width, SpriteChip.height, (byte)drawMode, flipH, flipV, colorOffset, pos.X, pos.Y);

CurrentSprites++;
}
}
Expand Down Expand Up @@ -679,18 +676,12 @@ public Point Display(bool visible = true)

height = MathUtil.CeilToInt(total / width);

// TODO this needs to be moved into the Draw Sprite
startX = x - (useScrollPos ? _scrollPos.X : 0);
startY = y - (useScrollPos ? _scrollPos.Y : 0);

var paddingW = SpriteChip.width;
var paddingH = SpriteChip.height;

if (drawMode == DrawMode.Tile)
{
paddingW = 1;
paddingH = 1;
}
// startY = displayChip.height - height - startY;
_paddingW = drawMode == DrawMode.Tile ? 1 : _spriteSize.X;
_paddingH = drawMode == DrawMode.Tile ? 1 : _spriteSize.Y;

if (flipH || flipV) SpriteChipUtil.FlipSpriteData(ref tmpIDs, width, height, flipH, flipV);

Expand All @@ -709,8 +700,8 @@ public Point Display(bool visible = true)
// Test to see if the sprite is within range
if (id > -1)
{
x = MathUtil.FloorToInt(i % width) * paddingW + startX;
y = MathUtil.FloorToInt(i / width) * paddingH + startY;
x = MathUtil.FloorToInt(i % width) * _paddingW + startX;
y = MathUtil.FloorToInt(i / width) * _paddingH + startY;
//
// var render = true;

Expand Down Expand Up @@ -834,7 +825,8 @@ public Point Display(bool visible = true)
/// </param>
/// <returns></returns>
public void DrawText(string text, int x, int y, DrawMode drawMode = DrawMode.Sprite, string font = "Default",
int colorOffset = 0, int spacing = 0, Rectangle? bounds = null)
int colorOffset = 0, int spacing = 0, bool onScreen = true, bool useScrollPos = true,
Rectangle? bounds = null)
{
// TODO this should use DrawSprites() API
spriteSize = SpriteSize();
Expand Down Expand Up @@ -889,10 +881,12 @@ public Point Display(bool visible = true)
CurrentSprites++;
}

FontChip.ReadSpriteAt(spriteIDs[j], ref tmpFontData);
// FontChip.ReadSpriteAt(spriteIDs[j], ref tmpFontData);

DrawSprite(spriteIDs[j], nextX, nextY, false, false, drawMode, colorOffset, onScreen, useScrollPos, null, FontChip);

DrawPixels(tmpFontData, nextX, nextY, SpriteChip.width, SpriteChip.height, false, false, drawMode,
colorOffset);
// DrawPixels(tmpFontData, nextX, nextY, SpriteChip.width, SpriteChip.height, false, false, drawMode,
// colorOffset);
}

nextX += offset;
Expand Down Expand Up @@ -1607,6 +1601,8 @@ public void RebuildTilemap()


protected Point _tilemapSize = Point.Zero;
private int _paddingW;
private int _paddingH;

/// <summary>
/// This will return a vector representing the size of the tilemap in columns (x) and rows (y).
Expand Down
97 changes: 50 additions & 47 deletions SDK/Engine/Chips/Graphics/DisplayChip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@

namespace PixelVision8.Engine.Chips
{

public class DisplayChip : AbstractChip, IDraw
{
// private Color[] cachedColors;

private DrawRequestPixelData draw;
private PixelData Display = new PixelData();
// protected List<DrawRequestPixelData>[] DrawRequestPixelDataLayers = new List<DrawRequestPixelData>[0];
// protected Stack<int[]> DrawRequestPixelDataPool = new Stack<int[]>();
public int[] Pixels = new int[0];
public int TotalPixels;
public int[] Pixels => Display.Pixels;

public int TotalPixels => Display.TotalPixels;
public int OverscanX { get; set; }
public int OverscanY { get; set; }

Expand Down Expand Up @@ -84,12 +87,12 @@ public int MaxDrawRequests
/// <summary>
/// Returns the display's <see cref="Width" />
/// </summary>
public int Width { get; protected set; } = 256;
public int Width => Display.Width;

/// <summary>
/// Returns the display's <see cref="Height" />
/// </summary>
public int Height { get; protected set; } = 240;
public int Height => Display.Height;


private List<DrawRequestPixelData> _drawRequests;
Expand All @@ -99,7 +102,8 @@ public int MaxDrawRequests
private DrawRequestPixelData _drawRequest;
private int drawCallCounter = -1;
private bool _clearFlag = true;

private int clearColor = -1;

/// <summary>
/// </summary>
public void Draw()
Expand All @@ -113,7 +117,7 @@ public void Draw()
for (_i = 0; _i < TotalPixels; _i++)
{
// We always set the clear color to -1 since the display target will automatically convert this into the background color
Pixels[_i] = -1;
Pixels[_i] = clearColor;
}

// Reset the clear flag for the next frame
Expand All @@ -129,17 +133,24 @@ public void Draw()
for (int i = 0; i < drawCallCounter; i++)
{
_drawRequest = DrawCalls[i];
CopyDrawRequestPixelData(_drawRequest.isRectangle ? null : _drawRequest.PixelData.Pixels, _drawRequest.x, _drawRequest.y, _drawRequest.width, _drawRequest.height,
_drawRequest.flipH, _drawRequest.flipV, _drawRequest.colorOffset);

PixelDataUtil.CopyPixels(_drawRequest.PixelData, _drawRequest.SampleRect.X, _drawRequest.SampleRect.Y, _drawRequest.SampleRect.Width, _drawRequest.SampleRect.Height, Display, _drawRequest.x, _drawRequest.y, _drawRequest.FlipH, _drawRequest.FlipV, _drawRequest.ColorOffset);


// PixelDataUtil.MergePixels(_drawRequest.PixelData, _drawRequest.x, _drawRequest.y, _drawRequest.width, _drawRequest.height, _drawRequest.PixelData.Pixels, _drawRequest.FlipH, _drawRequest.FlipV, _drawRequest.ColorOffset);
// CopyDrawRequestPixelData(_drawRequest.PixelData.Pixels, _drawRequest.x, _drawRequest.y, _drawRequest.width, _drawRequest.height,
// _drawRequest.FlipH, _drawRequest.FlipV, _drawRequest.ColorOffset);
}

// Reset Draw Requests after they have been processed
ResetDrawCalls();

}

public void Clear()
public void Clear(int color = -1)
{
clearColor = color;

_clearFlag = true;
}

Expand All @@ -148,10 +159,10 @@ public void Clear()
/// to the Display's TextureData.
/// </summary>
/// <param name="pixelData"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="destX"></param>
/// <param name="destY"></param>
/// <param name="blockWidth"></param>
/// <param name="blockHeight"></param>
/// <param name="layer"></param>
/// <param name="flipH">
/// This is an optional argument which accepts a bool. The default value is set to false but passing in true flips
Expand All @@ -163,37 +174,40 @@ public void Clear()
/// </param>
/// <param name="colorOffset"></param>
/// <param name="layerOrder"></param>
public void NewDrawCall(int[] pixelData, int x, int y, int width, int height, byte layer = 0, bool flipH = false,
bool flipV = false, int colorOffset = 0)
public void NewDrawCall(IDisplay src, int destX, int destY, int blockWidth, int blockHeight, byte layer = 0, bool flipH = false,
bool flipV = false, int colorOffset = 0, int srcX = 0, int srcY = 0)
{

// Exit if we are drawing to a layer that doesn't exist
// if (layer >= layers)
// return;

if (pixelData == null)
{
var total = width * height;

if(emptyPixels.Length != total)
Array.Resize(ref emptyPixels, total);

pixelData = emptyPixels;

}
// if (pixelData == null)
// {
// var total = width * height;
//
// if(emptyPixels.Length != total)
// Array.Resize(ref emptyPixels, total);
//
// pixelData = emptyPixels;
//
// }

var request = NextDrawRequest();

if (request.HasValue)
{
draw = request.Value;
draw.x = x;
draw.y = y;
draw.SetPixels(pixelData, width, height);
draw.x = destX;
draw.y = destY;


draw.SampleFrom(src, srcX, srcY, blockWidth, blockHeight);

draw.Priority = layer;
draw.flipH = flipH;
draw.flipV = flipV;
draw.colorOffset = colorOffset;
draw.FlipH = flipH;
draw.FlipV = flipV;
draw.ColorOffset = colorOffset;

DrawCalls.Add(draw);
// DrawRequestPixelDataLayers[layer].Add(draw);
Expand All @@ -208,12 +222,13 @@ public void Clear()
/// <param name="height"></param>
public void ResetResolution(int width, int height)
{
Width = width;
Height = height;
Display.Resize(width, height);
// Width = width;
// Height = height;

TotalPixels = Width * Height;
// TotalPixels = Width * Height;

Array.Resize(ref Pixels, TotalPixels);
// Array.Resize(ref Pixels, TotalPixels);
}

/// <summary>
Expand All @@ -236,18 +251,6 @@ public override void Configure()

MaxDrawRequests = 1024;

// TODO should the display have the sprite limit and the game chip looks there first

// SpriteDrawRequestPool = new Stack<DrawRequest>();
//
// var maxCalls = SpriteChip.maxSpriteCount > 0 ? SpriteChip.maxSpriteCount : maxDrawRequests;
//
// for (int i = 0; i < maxCalls; i++)
// {
// SpriteDrawRequestPool.Push(new DrawRequest());
// }


}

public override void Deactivate()
Expand Down

0 comments on commit 53d50da

Please sign in to comment.