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

Commit

Permalink
Moving renderer over to new shader.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed May 23, 2020
1 parent 9ae5a08 commit 87b06e1
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 98 deletions.
18 changes: 8 additions & 10 deletions Effects/crt-lottes-mg.fx
Expand Up @@ -40,6 +40,9 @@ struct VertexShaderOutput
#define bloomAmount 0.15
#define warp float2(0.008,0.01)

sampler2D screen: register(s0);
sampler colorPallete: register(s1);

float brightboost = 1.0;
float2 textureSize;
float2 videoSize;
Expand All @@ -52,13 +55,6 @@ float2 outputSize;

// ------------- //

Texture2D decal;
sampler2D DecalSampler = sampler_state
{
Texture = <decal>;
};

float4x4 modelViewProj;

//------------------------------------------------------------------------

Expand All @@ -76,7 +72,9 @@ float3 ToSrgb(float3 c)
// Also zero's off screen.
float3 Fetch(float2 pos, float2 off, float2 texture_size){
pos=(floor(pos*texture_size.xy+off)+float2(0.5,0.5))/texture_size.xy;
return brightboost * pow(tex2D(DecalSampler,pos.xy).rgb, 2);

float4 color = tex2D(screen, pos);
return brightboost * pow(tex2D(colorPallete, float2(color.r, 0.5f)).rgb, 2);
}

// Distance in emulated pixels to nearest texel.
Expand Down Expand Up @@ -195,7 +193,7 @@ float3 Mask(float2 pos){
return mask;
}

float4 crt_lottes(float2 texture_size, float2 video_size, float2 tex, sampler2D s0)
float4 crt_lottes(float2 texture_size, float2 video_size, float2 tex)
{

float2 pos=Warp(tex.xy*(texture_size.xy/video_size.xy))*(video_size.xy/texture_size.xy);
Expand All @@ -212,7 +210,7 @@ float3 outColor = Tri(pos, texture_size);

float4 main_fragment(VertexShaderOutput VOUT) : COLOR0
{
return crt_lottes(textureSize, videoSize, VOUT.texCoord, DecalSampler);
return crt_lottes(textureSize, videoSize, VOUT.texCoord);
}

technique
Expand Down
Binary file modified Effects/crt-lottes-mg.ogl.mgfxo
Binary file not shown.
Binary file added Runners/DevRunnerCore/Content/screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions Runners/PixelVision8Core/Runner/Tests/ShaderTest.cs
Expand Up @@ -99,8 +99,8 @@ protected override void Draw(GameTime gameTime)

_screen.SetData(_data);

// _spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp);
// _quickDraw.CurrentTechnique.Passes[0].Apply();
_spriteBatch.Begin(SpriteSortMode.Immediate, SamplerState.PointClamp);
_quickDraw.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.Textures[1] = _colorPallete;
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
_spriteBatch.Draw(_screen, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
Expand Down
2 changes: 1 addition & 1 deletion SDK/Editor/Services/ScreenshotService.cs
Expand Up @@ -121,7 +121,7 @@ public bool TakeScreenshot(IEngine engine)
var col = i % width;
if (col < visibleWidth && index < newTotalPixels)
{
newPixels[index] = pixels[i];
// newPixels[index] = pixels[i];
index++;
}
}
Expand Down
6 changes: 4 additions & 2 deletions SDK/Engine/Chips/Game/GameChip.cs
Expand Up @@ -603,8 +603,10 @@ public Point Display(bool visible = true)
tmpSpriteDataID = id;
}

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

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

CurrentSprites++;
}
Expand Down
174 changes: 137 additions & 37 deletions SDK/Engine/Chips/Graphics/DisplayChip.cs
Expand Up @@ -20,33 +20,41 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
// using System.Linq;
using Microsoft.Xna.Framework;

namespace PixelVision8.Engine.Chips
{
public class DisplayChip : AbstractChip, IDraw
{
private Color[] cachedColors;
// private Color[] cachedColors;

private DrawRequest draw;
protected List<DrawRequest>[] DrawRequestLayers = new List<DrawRequest>[0];
private DrawRequestPixelData draw;
protected List<DrawRequestPixelData>[] DrawRequestPixelDataLayers = new List<DrawRequestPixelData>[0];
protected Stack<int[]> DrawRequestPixelDataPool = new Stack<int[]>();
public Color[] Pixels = new Color[0];
public int[] Pixels = new int[0];
public int TotalPixels;
public int OverscanX { get; set; }
public int OverscanY { get; set; }

// This should be part of the chip's data
public int maxDrawRequests = 512;


SpriteChip SpriteChip => engine.SpriteChip;

public int layers
{
get => DrawRequestLayers.Length;
get => DrawRequestPixelDataLayers.Length;
set
{
Array.Resize(ref DrawRequestLayers, value);
Array.Resize(ref DrawRequestPixelDataLayers, value);
for (var i = value - 1; i > -1; i--)
{
var requests = DrawRequestLayers[i];
var requests = DrawRequestPixelDataLayers[i];
if (requests == null)
DrawRequestLayers[i] = new List<DrawRequest>();
DrawRequestPixelDataLayers[i] = new List<DrawRequestPixelData>();
else
requests.Clear();
}
Expand Down Expand Up @@ -77,34 +85,60 @@ public int layers
public int Height { get; protected set; } = 240;


private List<DrawRequest> _drawRequests;
private List<DrawRequestPixelData> _drawRequests;
private int _totalDR;
private int _layer;
private int _i;
private DrawRequest _drawRequest;
private DrawRequestPixelData _drawRequest;

/// <summary>
/// </summary>
public void Draw()
{
cachedColors = engine.ColorChip.colors;

// cachedColors = engine.ColorChip.colors;

// var bgColor = engine.ColorChip.backgroundColor;
//
// var col = 0;
// var row = 0;
// var w = Width;
//
// for (int i = 0; i < TotalPixels; i++)
// {
//
// if (col >= w)
// {
// row++;
// col = 0;
// }
//
//
// }

// Loop through all draw requests
for (_layer = 0; _layer < DrawRequestLayers.Length; _layer++)
for (_layer = 0; _layer < DrawRequestPixelDataLayers.Length; _layer++)
{
// TODO need to add back in support for turning layers on and off

_drawRequests = DrawRequestLayers[_layer];
_drawRequests = DrawRequestPixelDataLayers[_layer];
_totalDR = _drawRequests.Count;
for (_i = 0; _i < _totalDR; _i++)
{
_drawRequest = _drawRequests[_i];

CopyDrawRequest(_drawRequest.isRectangle ? null : _drawRequest.pixelData, _drawRequest.x, _drawRequest.y, _drawRequest.width, _drawRequest.height,
CopyDrawRequestPixelData(_drawRequest.isRectangle ? null : _drawRequest.pixelData, _drawRequest.x, _drawRequest.y, _drawRequest.width, _drawRequest.height,
_drawRequest.flipH, _drawRequest.flipV, _drawRequest.colorOffset);
}
}

// Sort sprite draw calls
SpriteDrawRequests.Sort((x, y) => x.priority.CompareTo(y.priority));

// for (int i = 0; i < SpriteDrawRequests.Count; i++)
// {
//
// }

// Reset Draw Requests after they have been processed
ResetDrawCalls();
}
Expand Down Expand Up @@ -134,16 +168,19 @@ public void Draw()
public void NewDrawCall(int[] pixelData, int x, int y, int width, int height, int layer = 0, bool flipH = false,
bool flipV = false, int colorOffset = 0)
{

// Exit if we are drawing to a layer that doesn't exist
if (layer >= layers)
{
// This can happen as the old system wasn't very strict.
// TODO: Handle "out of bounds" layer accesses properly!
_oldSize = layers;
Array.Resize(ref DrawRequestLayers, layer + 1);
for (_i = layers - 1; _i >= _oldSize; _i--) DrawRequestLayers[_i] = new List<DrawRequest>();
}

draw = NextDrawRequest();
return;
// {
// // This can happen as the old system wasn't very strict.
// // TODO: Handle "out of bounds" layer accesses properly!
// _oldSize = layers;
// Array.Resize(ref DrawRequestPixelDataLayers, layer + 1);
// for (_i = layers - 1; _i >= _oldSize; _i--) DrawRequestPixelDataLayers[_i] = new List<DrawRequestPixelData>();
// }

draw = NextDrawRequestPixelData();
draw.x = x;
draw.y = y;
draw.width = width;
Expand All @@ -152,7 +189,30 @@ public void Draw()
draw.flipH = flipH;
draw.flipV = flipV;
draw.colorOffset = colorOffset;
DrawRequestLayers[layer].Add(draw);
DrawRequestPixelDataLayers[layer].Add(draw);
}

public List<DrawRequest> SpriteDrawRequests = new List<DrawRequest>();

public void DrawSprite(int id, int x, int y, bool flipH, bool flipV, byte priority, int colorOffset)
{

var request = NextSpriteDrawRequest();

if (request.HasValue)
{
var drawCall = request.Value;
drawCall.id = id;
drawCall.x = x;
drawCall.y = y;
drawCall.flipH = flipH;
drawCall.flipV = flipV;
drawCall.priority = priority;
drawCall.colorOffset = colorOffset;

SpriteDrawRequests.Add(drawCall);
}

}

/// <summary>
Expand Down Expand Up @@ -187,6 +247,19 @@ public override void Configure()

// By default set the total layers to the DrawModes minus Tilemap Cache which isn't used for rendering
layers = Enum.GetNames(typeof(DrawMode)).Length - 1;

// 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 All @@ -198,9 +271,9 @@ public override void Deactivate()
public void ResetDrawCalls()
{
// Reset all draw requests
for (var layer = DrawRequestLayers.Length - 1; layer > -1; layer--)
for (var layer = DrawRequestPixelDataLayers.Length - 1; layer > -1; layer--)
{
var drawRequests = DrawRequestLayers[layer];
var drawRequests = DrawRequestPixelDataLayers[layer];

for (var i = drawRequests.Count - 1; i > -1; i--)
{
Expand All @@ -212,9 +285,9 @@ public void ResetDrawCalls()
}
}

public DrawRequest NextDrawRequest()
public DrawRequestPixelData NextDrawRequestPixelData()
{
var request = new DrawRequest();
var request = new DrawRequestPixelData();

if (DrawRequestPixelDataPool.Count > 0)
request.pixelData = DrawRequestPixelDataPool.Pop();
Expand All @@ -224,14 +297,32 @@ public DrawRequest NextDrawRequest()
return request;
}

protected Stack<DrawRequest> SpriteDrawRequestPool;

public DrawRequest? NextSpriteDrawRequest()
{

if (SpriteDrawRequestPool.Count > 0)
return SpriteDrawRequestPool.Pop();

// var request = new DrawRequest();
//
// if (SpriteDrawRequestPool.Count > 0)
// request.pixelData = DrawRequestPixelDataPool.Pop();
// else
// request.pixelData = new int[0];

return null;
}

int _total;
int _srcX;
int _srcY;
int _colorID;
int i1;
int _index;

public void CopyDrawRequest(int[] pixelData, int x, int y, int width, int height, bool flipH = false,
public void CopyDrawRequestPixelData(int[] pixelData, int x, int y, int width, int height, bool flipH = false,
bool flipV = false, int colorOffset = 0)
{
// int total;
Expand All @@ -240,11 +331,12 @@ public DrawRequest NextDrawRequest()
// int colorID;
// // int i;
// int index;
var tmpWidth = Width;
var tmpHeight = Height;

var tmpWidth = this.Width;
var tmpHeight = this.Height;

_total = width * height;

for (i1 = 0; i1 < _total; i1++)
{
_colorID = pixelData?[i1] ?? 0;
Expand Down Expand Up @@ -273,11 +365,18 @@ public DrawRequest NextDrawRequest()
_index = _srcX + tmpWidth * _srcY;

// Set the pixel
Pixels[_index] = cachedColors[_colorID];
Pixels[_index] = _colorID;//cachedColors[_colorID];
}
}
}

private bool clearFlag = false;

public void Clear()
{
clearFlag = true;
}

public Color[] VisiblePixels()
{
var pixels = engine.DisplayChip.Pixels;
Expand All @@ -301,13 +400,14 @@ public Color[] VisiblePixels()
var col = i % width;
if (col < visibleWidth && index < newTotalPixels)
{
newPixels[index] = pixels[i];
// newPixels[index] = pixels[i];
index++;
}
}

return newPixels;
}


}
}

0 comments on commit 87b06e1

Please sign in to comment.