Skip to content

Commit

Permalink
Extending effect state with rasterizer.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZaneDubya committed Jun 8, 2016
1 parent 8d91290 commit 714db39
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
23 changes: 18 additions & 5 deletions Source/Libraries/YpsilonFramework/Core/Graphics/EffectState.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ypsilon.Core.Graphics
{
public class EffectState
{
public readonly Effect Effect;
public readonly SamplerState Sampler;
public readonly RasterizerState Raster;
public readonly bool TextureOverride;
public readonly DepthStencilState stencil;

public EffectState(Effect effect, SamplerState sampler)
public EffectState(Effect effect, SamplerState sampler, RasterizerState raster, DepthStencilState stencil, bool texture)
{
Effect = effect;
Sampler = sampler;
Raster = raster;
TextureOverride = texture;
}

public EffectState(Effect effect, SamplerState sampler, RasterizerState raster)
: this(effect, sampler, raster, DepthStencilState.Default, false)
{

}

public EffectState(Effect effect, SamplerState sample)
: this(effect, sample, RasterizerState.CullNone, DepthStencilState.Default, false)
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ public PreTransformedQuad(Vector3 position, Vector2 area, Vector4 uv, Color hue,
}

private void Resize(Vector3 position, Vector2 area, Vector4 uv, Vector4 data, Color hue) {
Vector2 texelPixelOffset = new Vector2(); // new Vector2(0.5f / device.Viewport.Width, 0.5f / device.Viewport.Height);
Vertices = new[] {
new VertexPositionTextureDataColor(position, new Vector2(uv.X, uv.Y) + texelPixelOffset, data, hue), // top left
new VertexPositionTextureDataColor(position + new Vector3(area.X, 0, 0), new Vector2(uv.Z, uv.Y) + texelPixelOffset, data, hue), // top right
new VertexPositionTextureDataColor(position + new Vector3(0, area.Y, 0), new Vector2(uv.X, uv.W) + texelPixelOffset, data, hue), // bottom left
new VertexPositionTextureDataColor(position + new Vector3(area, 0), new Vector2(uv.Z, uv.W) + texelPixelOffset, data, hue) // bottom right
new VertexPositionTextureDataColor(position, new Vector2(uv.X, uv.Y), data, hue), // top left
new VertexPositionTextureDataColor(position + new Vector3(area.X, 0, 0), new Vector2(uv.Z, uv.Y), data, hue), // top right
new VertexPositionTextureDataColor(position + new Vector3(0, area.Y, 0), new Vector2(uv.X, uv.W), data, hue), // bottom left
new VertexPositionTextureDataColor(position + new Vector3(area, 0), new Vector2(uv.Z, uv.W), data, hue) // bottom right
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class SpriteBatchExtended {
private short[] m_IndexBuffer;
private Dictionary<EffectState, Dictionary<Texture2D, List<VertexPositionTextureDataColor>>> m_DrawCommands;
private Queue<List<VertexPositionTextureDataColor>> m_QueuedVertexLists;
private Texture2D m_Pixel;

public SpriteBatchExtended(Game game) {
m_Game = game;
Expand All @@ -21,6 +22,8 @@ public void Initialize()
m_DrawCommands = new Dictionary<EffectState, Dictionary<Texture2D, List<VertexPositionTextureDataColor>>>();
m_IndexBuffer = CreateIndexBuffer(0x2000);
m_QueuedVertexLists = new Queue<List<VertexPositionTextureDataColor>>(256);
m_Pixel = new Texture2D(Graphics, 1, 1);
m_Pixel.SetData(new Color[1] { Color.White });
}

public void Dispose()
Expand All @@ -47,7 +50,7 @@ public void Begin(Color? clear = null) {
public void End(Matrix projection, Matrix view, Matrix world) {
Graphics.DepthStencilState = DepthStencilState.Default;
EndUnderlying(projection, view, world, false);
Graphics.DepthStencilState = DepthStencilState.DepthRead;
Graphics.DepthStencilState = DepthStencilState.Default;
EndUnderlying(projection, view, world, true);
EndClearAllVertexLists();
Graphics.Textures[0] = null;
Expand All @@ -60,11 +63,7 @@ private void EndUnderlying(Matrix projection, Matrix view, Matrix world, bool dr
Dictionary<Texture2D, List<VertexPositionTextureDataColor>> vls = m_DrawCommands[effect];
Graphics.BlendState = BlendState.AlphaBlend;
Graphics.SamplerStates[0] = effect.Sampler;
Graphics.RasterizerState = new RasterizerState
{
ScissorTestEnable = true,
CullMode = CullMode.None
};
Graphics.RasterizerState = effect.Raster;
IEnumerator<KeyValuePair<Texture2D, List<VertexPositionTextureDataColor>>> vlKeyIsTexture = vls.GetEnumerator();
effect.Effect.Parameters["ProjectionMatrix"].SetValue(projection);
effect.Effect.Parameters["ViewMatrix"].SetValue(view);
Expand All @@ -75,7 +74,7 @@ private void EndUnderlying(Matrix projection, Matrix view, Matrix world, bool dr
while (vlKeyIsTexture.MoveNext())
{
List<VertexPositionTextureDataColor> iVertexList = vlKeyIsTexture.Current.Value;
Graphics.Textures[0] = vlKeyIsTexture.Current.Key;
Graphics.Textures[0] = effect.TextureOverride ? m_Pixel : vlKeyIsTexture.Current.Key;
Graphics.DrawUserIndexedPrimitives(
PrimitiveType.TriangleList, iVertexList.ToArray(), 0, iVertexList.Count, m_IndexBuffer, 0, iVertexList.Count / 2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Ypsilon.Core.Graphics {
/// </summary>
public class VectorRenderer {
private const int c_MaxPrimitives = 0x1000;
private readonly EffectState m_Effect;
private readonly GraphicsDevice m_Graphics;
private readonly short[] m_TriIndices;
private readonly VertexList m_WorldTris;
Expand Down

0 comments on commit 714db39

Please sign in to comment.