#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.GameContent;
namespace CalamityMod.Graphics.Primitives;
public static class SanePrimitiveRenderer
{
private static GraphicsDevice _graphicsDevice => Main.graphics.GraphicsDevice;
private static BasicEffect? _effect;
public readonly struct ShaderScope : IDisposable
{
private readonly GraphicsDevice _device;
private readonly Effect _effect;
private readonly bool _useBasicEffect;
private readonly Matrix _world;
private readonly Matrix _view;
private readonly Matrix _projection;
private readonly string? _transformMatrixParam;
private readonly RasterizerState _previousRasterizer;
private readonly DepthStencilState _previousDepth;
private readonly BlendState _previousBlend;
private readonly SamplerState? _previousSampler;
internal ShaderScope(
GraphicsDevice device,
Effect effect,
bool useBasicEffect,
in Matrix world,
in Matrix view,
in Matrix projection,
string? transformMatrixParam,
RasterizerState? rasterizerState,
DepthStencilState? depthState,
BlendState? blendState,
SamplerState? samplerState)
{
_device = device;
_effect = effect;
_useBasicEffect = useBasicEffect;
_world = world;
_view = view;
_projection = projection;
_transformMatrixParam = transformMatrixParam;
_previousRasterizer = device.RasterizerState;
_previousDepth = device.DepthStencilState;
_previousBlend = device.BlendState;
_previousSampler = device.SamplerStates[0];
device.RasterizerState = rasterizerState ?? RasterizerState.CullNone;
device.DepthStencilState = depthState ?? DepthStencilState.Default;
device.BlendState = blendState ?? BlendState.AlphaBlend;
device.SamplerStates[0] = samplerState ?? SamplerState.PointWrap;
if (_useBasicEffect)
{
var basic = (BasicEffect)_effect;
basic.World = world;
basic.View = view;
basic.Projection = projection;
}
else if (transformMatrixParam != null)
{
var param = _effect.Parameters[transformMatrixParam];
if (param != null)
param.SetValue(world * view * projection);
}
}
public void Draw(in PrimitiveMesh mesh)
{
if (!mesh.IsValid)
return;
if (_useBasicEffect)
{
var basic = (BasicEffect)_effect;
bool textured = mesh.UsesTexture;
basic.TextureEnabled = textured;
basic.Texture = textured ? TextureAssets.MagicPixel.Value : null;
}
int primitiveCount = GetPrimitiveCount(mesh.PrimitiveType, mesh.Indices.Length);
bool usesTexture = mesh.UsesTexture;
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
if (usesTexture)
{
_device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.TexturedVertices,
0,
mesh.VertexCount,
mesh.Indices,
0,
primitiveCount);
}
else
{
_device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.ColorVertices,
0,
mesh.VertexCount,
mesh.Indices,
0,
primitiveCount);
}
}
}
public void Draw(in PrimitiveMeshView mesh)
{
if (!mesh.IsValid)
return;
if (_useBasicEffect)
{
var basic = (BasicEffect)_effect;
bool textured = mesh.UsesTexture;
basic.TextureEnabled = textured;
basic.Texture = textured ? TextureAssets.MagicPixel.Value : null;
}
int primitiveCount = GetPrimitiveCount(mesh.PrimitiveType, mesh.IndexCount);
bool usesTexture = mesh.UsesTexture;
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
if (usesTexture)
{
_device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.TexturedVertices,
mesh.VertexOffset,
mesh.VertexCount,
mesh.Indices,
mesh.IndexOffset,
primitiveCount);
}
else
{
_device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.ColorVertices,
mesh.VertexOffset,
mesh.VertexCount,
mesh.Indices,
mesh.IndexOffset,
primitiveCount);
}
}
}
public void Dispose()
{
_device.RasterizerState = _previousRasterizer;
_device.DepthStencilState = _previousDepth;
_device.BlendState = _previousBlend;
if (_previousSampler != null)
_device.SamplerStates[0] = _previousSampler;
}
}
public static bool IsReady =>
_graphicsDevice != null &&
!_graphicsDevice.IsDisposed &&
_effect != null &&
!_effect.IsDisposed;
public static void Initialize()
{
Main.RunOnMainThread(() =>
{
if (_graphicsDevice == null)
throw new ArgumentNullException(nameof(_graphicsDevice));
_effect?.Dispose();
_effect = new BasicEffect(_graphicsDevice)
{
VertexColorEnabled = true,
TextureEnabled = true,
LightingEnabled = false,
FogEnabled = false,
Texture = TextureAssets.Logo.Value
};
});
}
public static void Dispose()
{
Main.RunOnMainThread(() =>
_effect?.Dispose()
);
}
///
/// Draws the specified mesh using the provided world, view, and projection matrices.
///
/// World matrix.
/// View matrix.
/// Projection matrix.
/// The mesh to draw.
/// The rasterizer state to use. If null, defaults to .
/// The depth stencil state to use. If null, defaults to .
/// The blend state to use. If null, defaults to .
/// The renderer is not initialized.
/// The primitive type is not supported. Supported types are: TriangleList, TriangleStrip, LineList, LineStrip, and PointListEXT.
public static void DrawMesh(
in Matrix world,
in Matrix view,
in Matrix projection,
in PrimitiveMesh mesh,
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null)
{
if (!IsReady)
throw new InvalidOperationException("SanePrimitiveRenderer is not initialized.");
if (!mesh.IsValid)
return;
Debug.Assert(_effect is not null);
bool textured = mesh.UsesTexture;
_effect.TextureEnabled = textured;
_effect.Texture = textured ? TextureAssets.MagicPixel.Value : null;
_graphicsDevice.SamplerStates[0] = SamplerState.PointWrap;
_effect.World = world;
_effect.View = view;
_effect.Projection = projection;
var device = _graphicsDevice;
var previousRasterizer = device.RasterizerState;
var previousDepth = device.DepthStencilState;
var previousBlend = device.BlendState;
device.RasterizerState = rasterizerState ?? RasterizerState.CullNone;
device.DepthStencilState = depthState ?? DepthStencilState.Default;
device.BlendState = blendState ?? BlendState.AlphaBlend;
int primitiveCount = GetPrimitiveCount(mesh.PrimitiveType, mesh.Indices.Length);
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
if (textured)
{
device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.TexturedVertices,
0,
mesh.VertexCount,
mesh.Indices,
0,
primitiveCount);
}
else
{
device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.ColorVertices,
0,
mesh.VertexCount,
mesh.Indices,
0,
primitiveCount);
}
}
device.RasterizerState = previousRasterizer;
device.DepthStencilState = previousDepth;
device.BlendState = previousBlend;
}
///
/// Draws the specified mesh using an arbitrary instead of the built-in .
/// The caller is responsible for setting all shader parameters (including any transform matrices) before calling this method.
/// A convenience parameter, , can be used to automatically set a uTransformMatrix
/// parameter on the effect to world * view * projection.
///
/// World matrix.
/// View matrix.
/// Projection matrix.
/// The mesh to draw.
/// The custom effect to use for rendering.
///
/// If non-null, the name of an on that will be set to world * view * projection.
/// Defaults to "uTransformMatrix". Pass null to skip automatic matrix assignment.
///
/// The rasterizer state to use. If null, defaults to .
/// The depth stencil state to use. If null, defaults to .
/// The blend state to use. If null, defaults to .
/// is null.
/// The primitive type is not supported.
public static void DrawMesh(
in Matrix world,
in Matrix view,
in Matrix projection,
in PrimitiveMesh mesh,
Effect effect,
string? transformMatrixParam = "uTransformMatrix",
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null)
{
if (effect == null)
throw new ArgumentNullException(nameof(effect));
if (!mesh.IsValid)
return;
if (transformMatrixParam != null)
{
var param = effect.Parameters[transformMatrixParam];
if (param != null)
param.SetValue(world * view * projection);
}
var device = _graphicsDevice;
var previousRasterizer = device.RasterizerState;
var previousDepth = device.DepthStencilState;
var previousBlend = device.BlendState;
device.RasterizerState = rasterizerState ?? RasterizerState.CullNone;
device.DepthStencilState = depthState ?? DepthStencilState.Default;
device.BlendState = blendState ?? BlendState.AlphaBlend;
int primitiveCount = GetPrimitiveCount(mesh.PrimitiveType, mesh.Indices.Length);
bool textured = mesh.UsesTexture;
foreach (var pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
if (textured)
{
device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.TexturedVertices,
0,
mesh.VertexCount,
mesh.Indices,
0,
primitiveCount);
}
else
{
device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.ColorVertices,
0,
mesh.VertexCount,
mesh.Indices,
0,
primitiveCount);
}
}
device.RasterizerState = previousRasterizer;
device.DepthStencilState = previousDepth;
device.BlendState = previousBlend;
}
/**
Draws a triangle strip using the specified parameters.
Alias for . Assumes you want a 'triangle strip' primitive type.
The world matrix.
The view matrix.
The projection matrix.
The vertex positions and colors.
**/
public static void DrawTriangleStrip(
in Matrix world,
in Matrix view,
in Matrix projection,
VertexPositionColorTexture[] vertices,
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null)
{
if (vertices == null)
throw new ArgumentNullException(nameof(vertices));
if (vertices.Length < 3)
return;
if (vertices.Length > short.MaxValue)
throw new ArgumentOutOfRangeException(nameof(vertices), "Vertex count exceeds index buffer range.");
DrawMesh(
world,
view,
projection,
PrimitiveMesh.FromSequential(vertices, PrimitiveType.TriangleStrip),
rasterizerState,
depthState,
blendState);
}
/**
Draws a triangle strip using the specified parameters.
Alias for . Assumes you want a 'triangle strip' primitive type.
The world matrix.
The view matrix.
The projection matrix.
The vertex positions and colors.
**/
public static void DrawTriangleStrip(
in Matrix world,
in Matrix view,
in Matrix projection,
VertexPositionColor[] vertices,
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null)
{
if (vertices == null)
throw new ArgumentNullException(nameof(vertices));
if (vertices.Length < 3)
return;
if (vertices.Length > short.MaxValue)
throw new ArgumentOutOfRangeException(nameof(vertices), "Vertex count exceeds index buffer range.");
DrawMesh(
world,
view,
projection,
PrimitiveMesh.FromSequential(vertices, PrimitiveType.TriangleStrip),
rasterizerState,
depthState,
blendState);
}
/**
Draws a triangle list using the specified parameters.
Alias for . Assumes you want a 'triangle list' primitive type.
The world matrix.
The view matrix.
The projection matrix.
The vertex positions and colors.
**/
public static void DrawTriangleList(
in Matrix world,
in Matrix view,
in Matrix projection,
VertexPositionColorTexture[] vertices,
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null)
{
if (vertices == null)
throw new ArgumentNullException(nameof(vertices));
if (vertices.Length < 3 || vertices.Length % 3 != 0)
return;
if (vertices.Length > short.MaxValue)
throw new ArgumentOutOfRangeException(nameof(vertices), "Vertex count exceeds index buffer range.");
DrawMesh(
world,
view,
projection,
PrimitiveMesh.FromSequential(vertices, PrimitiveType.TriangleList),
rasterizerState,
depthState,
blendState);
}
/**
Draws a triangle list using the specified parameters.
Alias for . Assumes you want a 'triangle list' primitive type.
The world matrix.
The view matrix.
The projection matrix.
The vertex positions and colors.
**/
public static void DrawTriangleList(
in Matrix world,
in Matrix view,
in Matrix projection,
VertexPositionColor[] vertices,
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null)
{
if (vertices == null)
throw new ArgumentNullException(nameof(vertices));
if (vertices.Length < 3 || vertices.Length % 3 != 0)
return;
if (vertices.Length > short.MaxValue)
throw new ArgumentOutOfRangeException(nameof(vertices), "Vertex count exceeds index buffer range.");
DrawMesh(
world,
view,
projection,
PrimitiveMesh.FromSequential(vertices, PrimitiveType.TriangleList),
rasterizerState,
depthState,
blendState);
}
public static void DrawMesh(
in Matrix world,
in Matrix view,
in Matrix projection,
in PrimitiveMeshView mesh,
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null)
{
if (!IsReady)
throw new InvalidOperationException("SanePrimitiveRenderer is not initialized.");
if (!mesh.IsValid)
return;
Debug.Assert(_effect is not null);
bool textured = mesh.UsesTexture;
_effect.TextureEnabled = textured;
_effect.Texture = textured ? TextureAssets.MagicPixel.Value : null;
_graphicsDevice.SamplerStates[0] = SamplerState.PointWrap;
_effect.World = world;
_effect.View = view;
_effect.Projection = projection;
var device = _graphicsDevice;
var previousRasterizer = device.RasterizerState;
var previousDepth = device.DepthStencilState;
var previousBlend = device.BlendState;
device.RasterizerState = rasterizerState ?? RasterizerState.CullNone;
device.DepthStencilState = depthState ?? DepthStencilState.Default;
device.BlendState = blendState ?? BlendState.AlphaBlend;
int primitiveCount = GetPrimitiveCount(mesh.PrimitiveType, mesh.IndexCount);
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
if (textured)
{
device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.TexturedVertices,
mesh.VertexOffset,
mesh.VertexCount,
mesh.Indices,
mesh.IndexOffset,
primitiveCount);
}
else
{
device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.ColorVertices,
mesh.VertexOffset,
mesh.VertexCount,
mesh.Indices,
mesh.IndexOffset,
primitiveCount);
}
}
device.RasterizerState = previousRasterizer;
device.DepthStencilState = previousDepth;
device.BlendState = previousBlend;
}
public static void DrawMesh(
in Matrix world,
in Matrix view,
in Matrix projection,
in PrimitiveMeshView mesh,
Effect effect,
string? transformMatrixParam = "uTransformMatrix",
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null)
{
if (effect == null)
throw new ArgumentNullException(nameof(effect));
if (!mesh.IsValid)
return;
if (transformMatrixParam != null)
{
var param = effect.Parameters[transformMatrixParam];
if (param != null)
param.SetValue(world * view * projection);
}
var device = _graphicsDevice;
var previousRasterizer = device.RasterizerState;
var previousDepth = device.DepthStencilState;
var previousBlend = device.BlendState;
device.RasterizerState = rasterizerState ?? RasterizerState.CullNone;
device.DepthStencilState = depthState ?? DepthStencilState.Default;
device.BlendState = blendState ?? BlendState.AlphaBlend;
int primitiveCount = GetPrimitiveCount(mesh.PrimitiveType, mesh.IndexCount);
bool textured = mesh.UsesTexture;
foreach (var pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
if (textured)
{
device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.TexturedVertices,
mesh.VertexOffset,
mesh.VertexCount,
mesh.Indices,
mesh.IndexOffset,
primitiveCount);
}
else
{
device.DrawUserIndexedPrimitives(
mesh.PrimitiveType,
mesh.ColorVertices,
mesh.VertexOffset,
mesh.VertexCount,
mesh.Indices,
mesh.IndexOffset,
primitiveCount);
}
}
device.RasterizerState = previousRasterizer;
device.DepthStencilState = previousDepth;
device.BlendState = previousBlend;
}
public static ShaderScope BeginShaderScope(
in Matrix world,
in Matrix view,
in Matrix projection,
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null,
SamplerState? samplerState = null)
{
if (!IsReady)
throw new InvalidOperationException("SanePrimitiveRenderer is not initialized.");
Debug.Assert(_effect is not null);
return new ShaderScope(
_graphicsDevice,
_effect,
true,
world,
view,
projection,
null,
rasterizerState,
depthState,
blendState,
samplerState);
}
public static ShaderScope BeginShaderScope(
Effect effect,
in Matrix world,
in Matrix view,
in Matrix projection,
string? transformMatrixParam = "uTransformMatrix",
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null,
SamplerState? samplerState = null)
{
if (effect == null)
throw new ArgumentNullException(nameof(effect));
return new ShaderScope(
_graphicsDevice,
effect,
false,
world,
view,
projection,
transformMatrixParam,
rasterizerState,
depthState,
blendState,
samplerState);
}
public static void DrawMeshes(
in Matrix world,
in Matrix view,
in Matrix projection,
IReadOnlyList meshes,
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null,
SamplerState? samplerState = null)
{
if (meshes == null)
throw new ArgumentNullException(nameof(meshes));
using var scope = BeginShaderScope(world, view, projection, rasterizerState, depthState, blendState, samplerState);
for (int i = 0; i < meshes.Count; i++)
scope.Draw(meshes[i]);
}
public static void DrawMeshes(
in Matrix world,
in Matrix view,
in Matrix projection,
IReadOnlyList meshes,
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null,
SamplerState? samplerState = null)
{
if (meshes == null)
throw new ArgumentNullException(nameof(meshes));
using var scope = BeginShaderScope(world, view, projection, rasterizerState, depthState, blendState, samplerState);
for (int i = 0; i < meshes.Count; i++)
scope.Draw(meshes[i]);
}
public static void DrawMeshes(
Effect effect,
in Matrix world,
in Matrix view,
in Matrix projection,
IReadOnlyList meshes,
string? transformMatrixParam = "uTransformMatrix",
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null,
SamplerState? samplerState = null)
{
if (effect == null)
throw new ArgumentNullException(nameof(effect));
if (meshes == null)
throw new ArgumentNullException(nameof(meshes));
using var scope = BeginShaderScope(effect, world, view, projection, transformMatrixParam, rasterizerState, depthState, blendState, samplerState);
for (int i = 0; i < meshes.Count; i++)
scope.Draw(meshes[i]);
}
public static void DrawMeshes(
Effect effect,
in Matrix world,
in Matrix view,
in Matrix projection,
IReadOnlyList meshes,
string? transformMatrixParam = "uTransformMatrix",
RasterizerState? rasterizerState = null,
DepthStencilState? depthState = null,
BlendState? blendState = null,
SamplerState? samplerState = null)
{
if (effect == null)
throw new ArgumentNullException(nameof(effect));
if (meshes == null)
throw new ArgumentNullException(nameof(meshes));
using var scope = BeginShaderScope(effect, world, view, projection, transformMatrixParam, rasterizerState, depthState, blendState, samplerState);
for (int i = 0; i < meshes.Count; i++)
scope.Draw(meshes[i]);
}
private static int GetPrimitiveCount(PrimitiveType primitiveType, int indexCount)
{
return primitiveType switch
{
PrimitiveType.TriangleList => indexCount / 3,
PrimitiveType.TriangleStrip => Math.Max(indexCount - 2, 0),
PrimitiveType.LineList => indexCount / 2,
PrimitiveType.LineStrip => Math.Max(indexCount - 1, 0),
PrimitiveType.PointListEXT => indexCount,
_ => throw new NotSupportedException(primitiveType.ToString())
};
}
}