trying to do tiled draws

tiled_draw_refactor
cosmonaut 2020-12-10 16:02:13 -08:00
parent 0ebad486c5
commit 40b5d71bfe
17 changed files with 134 additions and 153 deletions

35
Data/MeshPartDrawData.cs Normal file
View File

@ -0,0 +1,35 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Kav.Data
{
public struct MeshPartDrawData : IGBufferDrawable, IUVDrawable, ICullable, IIndexDrawable
{
public MeshPart MeshPart { get; }
public Matrix TransformMatrix { get; }
public UVData UVData { get; }
public BoundingBox BoundingBox => MeshPart.BoundingBox;
public IndexBuffer IndexBuffer => MeshPart.IndexBuffer;
public VertexBuffer VertexBuffer => MeshPart.VertexBuffer;
public Vector3 Albedo => MeshPart.Albedo;
public float Metallic => MeshPart.Metallic;
public float Roughness => MeshPart.Roughness;
public Texture2D AlbedoTexture => MeshPart.AlbedoTexture;
public Texture2D NormalTexture => MeshPart.NormalTexture;
public Texture2D MetallicRoughnessTexture => MeshPart.MetallicRoughnessTexture;
public MeshPartDrawData(
MeshPart meshPart,
Matrix transformMatrix,
UVData uvData
) {
MeshPart = meshPart;
TransformMatrix = transformMatrix;
UVData = uvData;
}
}
}

View File

@ -2,23 +2,25 @@ using Microsoft.Xna.Framework;
namespace Kav.Data
{
public struct MeshSpriteDrawData
public struct MeshSpriteDrawData : ICullable
{
public MeshSprite MeshSprite { get; }
public SpriteBillboardConstraint BillboardConstraint { get; }
public Matrix TransformMatrix { get; }
public UVData UVOffset { get; }
public UVData UVData { get; }
public BoundingBox BoundingBox => MeshSprite.BoundingBox;
public MeshSpriteDrawData(
MeshSprite meshSprite,
SpriteBillboardConstraint billboardConstraint,
Matrix transformMatrix,
UVData offset
UVData uvData
) {
MeshSprite = meshSprite;
BillboardConstraint = billboardConstraint;
TransformMatrix = transformMatrix;
UVOffset = offset;
UVData = uvData;
}
}
}

View File

@ -15,5 +15,10 @@ namespace Kav.Data
Percentage = subTextureDimensions / atlasDimensions;
Offset = positionInAtlas / atlasDimensions;
}
public Vector4 ToVector4()
{
return new Vector4(Offset.X, Offset.Y, Percentage.X, Percentage.Y);
}
}
}

View File

@ -12,14 +12,12 @@ namespace Kav
EffectParameter normalTextureParam;
EffectParameter metallicRoughnessTextureParam;
EffectParameter uvOffsetAndDimensionsParam;
EffectParameter albedoParam;
EffectParameter metallicParam;
EffectParameter roughnessParam;
EffectParameter numTextureRowsParam;
EffectParameter numTextureColumnsParam;
EffectParameter vertexShaderIndexParam;
EffectParameter shaderIndexParam;
Matrix world = Matrix.Identity;
@ -30,8 +28,7 @@ namespace Kav
float metallic;
float roughness;
int numTextureRows = 1;
int numTextureColumns = 1;
Vector4 uvData;
bool albedoTextureEnabled = false;
bool metallicRoughnessMapEnabled = false;
@ -133,24 +130,15 @@ namespace Kav
}
}
public int NumTextureRows
public Vector4 UVData
{
get { return numTextureRows; }
get { return uvData; }
set
{
numTextureRows = value;
numTextureRowsParam.SetValue(numTextureRows);
}
uvData = value;
uvOffsetAndDimensionsParam.SetValue(uvData);
}
public int NumTextureColumns
{
get { return numTextureColumns; }
set
{
numTextureColumns = value;
numTextureColumnsParam.SetValue(numTextureColumns);
}
}
public bool HardwareInstancingEnabled
@ -210,18 +198,6 @@ namespace Kav
dirtyFlags &= ~EffectDirtyFlags.ViewProj;
}
if ((dirtyFlags & EffectDirtyFlags.VertexShaderIndex) != 0)
{
int vertexShaderIndex = 0;
if (hardwareInstancingEnabled)
{
vertexShaderIndex = 1;
}
vertexShaderIndexParam.SetValue(vertexShaderIndex);
}
if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0)
{
int shaderIndex = 0;
@ -274,11 +250,9 @@ namespace Kav
metallicParam = Parameters["MetallicValue"];
roughnessParam = Parameters["RoughnessValue"];
numTextureRowsParam = Parameters["NumTextureRows"];
numTextureColumnsParam = Parameters["NumTextureColumns"];
uvOffsetAndDimensionsParam = Parameters["UVOffsetAndDimensions"];
shaderIndexParam = Parameters["PixelShaderIndex"];
vertexShaderIndexParam = Parameters["VertexShaderIndex"];
}
}
}

View File

@ -33,8 +33,7 @@ namespace Kav
Matrix view = Matrix.Identity;
Matrix projection = Matrix.Identity;
Vector2 uvOffset;
Vector2 subTextureDimensions;
Vector4 uvData;
EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;
@ -115,23 +114,13 @@ namespace Kav
}
}
public Vector2 UVOffset
public Vector4 UVData
{
get { return uvOffset; }
get { return uvData; }
set
{
uvOffset = value;
dirtyFlags |= EffectDirtyFlags.UVOrDimensions;
}
}
public Vector2 SubTextureDimensions
{
get { return subTextureDimensions; }
set
{
subTextureDimensions = value;
dirtyFlags |= EffectDirtyFlags.UVOrDimensions;
uvData = value;
dirtyFlags |= EffectDirtyFlags.UVData;
}
}
@ -168,14 +157,11 @@ namespace Kav
dirtyFlags &= ~EffectDirtyFlags.WorldViewProj;
}
if ((dirtyFlags & EffectDirtyFlags.UVOrDimensions) != 0)
if ((dirtyFlags & EffectDirtyFlags.UVData) != 0)
{
uvOffsetAndDimensionsParam.SetValue(new Vector4(
UVOffset.X, UVOffset.Y,
SubTextureDimensions.X, SubTextureDimensions.Y
));
uvOffsetAndDimensionsParam.SetValue(uvData);
dirtyFlags &= ~EffectDirtyFlags.UVOrDimensions;
dirtyFlags &= ~EffectDirtyFlags.UVData;
}
if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0)

View File

@ -11,7 +11,7 @@ namespace Kav
VertexShaderIndex = 8,
PixelShaderIndex = 16,
ViewProj = 32,
UVOrDimensions = 64,
UVData = 64,
All = -1
}
}

BIN
Effects/FXB/DeferredPBR_GBufferEffect.fxb (Stored with Git LFS)

Binary file not shown.

View File

@ -4,29 +4,31 @@ DECLARE_TEXTURE(AlbedoTexture, 0);
DECLARE_TEXTURE(NormalTexture, 1);
DECLARE_TEXTURE(MetallicRoughnessTexture, 2);
BEGIN_CONSTANTS
float3 AlbedoValue;
float MetallicValue;
float RoughnessValue;
float3 AlbedoValue _ps(c0) _cb(c0);
float MetallicValue _ps(c1) _cb(c1);
float RoughnessValue _ps(c2) _cb(c2);
float4 UVOffsetAndDimensions;
int NumTextureRows _vs(c9) _cb(c3);
int NumTextureColumns _vs(c10) _cb(c4);
MATRIX_CONSTANTS
float4x4 World _vs(c0) _cb(c7);
float4x4 ViewProjection _vs(c4) _cb(c11);
END_CONSTANTS
float4x4 World;
float4x4 ViewProjection;
struct VertexInput
{
float4 Position : POSITION;
float3 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoord : TEXCOORD;
};
struct VertexInstancedInput
{
float3 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoord : TEXCOORD0;
float3 Translation : TEXCOORD2;
float2 UVData : TANGENT;
};
struct PixelInput
{
float4 Position : SV_POSITION;
@ -51,7 +53,11 @@ PixelInput main_vs(VertexInput input)
output.PositionWorld = mul(input.Position, World).xyz;
output.NormalWorld = normalize(mul(input.Normal, World));
output.TexCoord = input.TexCoord;
float2 texCoord;
texCoord.x = (input.TexCoord.x / UVOffsetAndDimensions.z) + UVOffsetAndDimensions.x;
texCoord.y = (input.TexCoord.y / UVOffsetAndDimensions.w) + UVOffsetAndDimensions.y;
output.TexCoord = texCoord;
float4x4 worldViewProjection = mul(World, ViewProjection);
output.Position = mul(input.Position, worldViewProjection);
@ -60,9 +66,7 @@ PixelInput main_vs(VertexInput input)
}
PixelInput instanced_vs(
VertexInput input,
float3 Translation : TEXCOORD2,
float2 UVOffset : TEXCOORD5
VertexInstancedInput input
) {
PixelInput output;
@ -70,7 +74,7 @@ PixelInput instanced_vs(
float4(1, 0, 0, 0),
float4(0, 1, 0, 0),
float4(0, 0, 1, 0),
float4(Translation.x, Translation.y, Translation.z, 1)
float4(input.Translation.x, input.Translation.y, input.Translation.z, 1)
);
float4x4 worldViewProjection = mul(world, ViewProjection);
@ -79,9 +83,9 @@ PixelInput instanced_vs(
output.NormalWorld = mul(input.Normal, world);
float2 texCoord;
texCoord.x = (input.TexCoord.x / NumTextureColumns + UVOffset.x);
texCoord.y = (input.TexCoord.y / NumTextureRows + UVOffset.y);
output.TexCoord = texCoord;
texCoord.x = (input.TexCoord.x / 8) + input.UVData.x;
texCoord.y = (input.TexCoord.y / 1) + input.UVData.y;
output.TexCoord = input.TexCoord;
output.Position = mul(input.Position, worldViewProjection);
@ -204,19 +208,6 @@ PixelOutput AlbedoMetallicRoughnessNormalMapPS(PixelInput input)
return output;
}
VertexShader VSArray[2] =
{
compile vs_3_0 main_vs(),
compile vs_3_0 instanced_vs()
};
int VSIndices[2] =
{
0, 1
};
int VertexShaderIndex = 0;
PixelShader PSArray[8] =
{
compile ps_3_0 NonePS(),
@ -243,7 +234,7 @@ Technique GBuffer
{
Pass
{
VertexShader = (VSArray[VSIndices[VertexShaderIndex]]);
VertexShader = compile vs_3_0 instanced_vs();
PixelShader = (PSArray[PSIndices[PixelShaderIndex]]);
}
}

View File

@ -5,5 +5,6 @@ namespace Kav
public interface ICullable
{
BoundingBox BoundingBox { get; }
Matrix TransformMatrix { get; }
}
}

View File

@ -1,3 +1,4 @@
using Kav.Data;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@ -12,8 +13,5 @@ namespace Kav
Texture2D AlbedoTexture { get; }
Texture2D NormalTexture { get; }
Texture2D MetallicRoughnessTexture { get; }
int NumTextureRows { get; }
int NumTextureColumns { get; }
}
}

View File

@ -0,0 +1,9 @@
using Kav.Data;
namespace Kav
{
public interface IUVDrawable
{
UVData UVData { get; }
}
}

View File

@ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
public class MeshPart : IIndexDrawable, IGBufferDrawable, ICullable, IHasVertexPositions
public class MeshPart : IIndexDrawable, IGBufferDrawable, IHasVertexPositions
{
public IndexBuffer IndexBuffer { get; }
public VertexBuffer VertexBuffer { get; }
@ -38,9 +38,6 @@ namespace Kav
public float Metallic { get; set; } = 0.5f;
public float Roughness { get; set; } = 0.5f;
public int NumTextureRows { get; set; } = 1;
public int NumTextureColumns { get; set; } = 1;
public bool DisableAlbedoMap { get; set; } = false;
public bool DisableNormalMap { get; set; } = false;
public bool DisableMetallicRoughnessMap { get; set; } = false;

View File

@ -4,7 +4,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
public class MeshSprite : ICullable, IIndexDrawable
public class MeshSprite : IIndexDrawable
{
public enum FlipOptions
{

View File

@ -2,7 +2,7 @@ using Microsoft.Xna.Framework;
namespace Kav
{
public class Model : ICullable
public class Model
{
public Mesh[] Meshes { get; }
public BoundingBox BoundingBox { get; }

View File

@ -159,20 +159,14 @@ namespace Kav
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
foreach (var data in meshSpriteDrawDatas)
foreach (var data in FrustumCull(boundingFrustum, meshSpriteDrawDatas))
{
var matrix = BillboardTransforms(camera, data.TransformMatrix, data.BillboardConstraint);
if (FrustumCull(boundingFrustum, data.MeshSprite, matrix))
{
continue;
}
DiffuseLitSpriteEffect.NormalMapEnabled = data.MeshSprite.Normal != null;
DiffuseLitSpriteEffect.World = matrix;
DiffuseLitSpriteEffect.UVOffset = data.UVOffset.Offset;
DiffuseLitSpriteEffect.SubTextureDimensions = data.UVOffset.Percentage;
DiffuseLitSpriteEffect.UVData = data.UVData.ToVector4();
GraphicsDevice.Textures[0] = data.MeshSprite.Texture;
GraphicsDevice.Textures[1] = data.MeshSprite.Normal;
@ -232,13 +226,13 @@ namespace Kav
public static void CullAndRenderIndexed<T, U>(
GraphicsDevice graphicsDevice,
BoundingFrustum boundingFrustum,
IEnumerable<(T, Matrix)> drawableTransformPairs,
IEnumerable<T> drawables,
U effect
) where T : IIndexDrawable, ICullable where U : Effect, IHasWorldMatrix
{
foreach (var (drawable, transform) in FrustumCull(boundingFrustum, drawableTransformPairs))
foreach (var drawable in FrustumCull(boundingFrustum, drawables))
{
effect.World = transform;
effect.World = drawable.TransformMatrix;
RenderIndexed(
graphicsDevice,
@ -316,7 +310,7 @@ namespace Kav
public void RenderDepthIndexed<T>(
RenderTarget2D renderTarget,
PerspectiveCamera camera,
IEnumerable<(T, Matrix)> drawableTransforms
IEnumerable<T> drawables
) where T : ICullable, IIndexDrawable
{
GraphicsDevice.SetRenderTarget(renderTarget);
@ -328,7 +322,7 @@ namespace Kav
CullAndRenderIndexed(
GraphicsDevice,
new BoundingFrustum(camera.View * camera.Projection),
drawableTransforms,
drawables,
SimpleDepthEffect
);
}
@ -381,9 +375,6 @@ namespace Kav
Deferred_GBufferEffect.Metallic = drawable.Metallic;
Deferred_GBufferEffect.Roughness = drawable.Roughness;
Deferred_GBufferEffect.NumTextureRows = drawable.NumTextureRows;
Deferred_GBufferEffect.NumTextureColumns = drawable.NumTextureColumns;
Deferred_GBufferEffect.AlbedoTexture = drawable.AlbedoTexture;
Deferred_GBufferEffect.NormalTexture = drawable.NormalTexture;
Deferred_GBufferEffect.MetallicRoughnessTexture = drawable.MetallicRoughnessTexture;
@ -417,8 +408,8 @@ namespace Kav
public void RenderGBufferIndexed<T>(
RenderTargetBinding[] gBuffer,
PerspectiveCamera camera,
IEnumerable<(T, Matrix)> drawableTransforms
) where T : ICullable, IIndexDrawable, IGBufferDrawable {
IEnumerable<T> drawables
) where T : ICullable, IIndexDrawable, IUVDrawable, IGBufferDrawable {
GraphicsDevice.SetRenderTargets(gBuffer);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.Opaque;
@ -429,9 +420,9 @@ namespace Kav
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
foreach (var (drawable, transform) in FrustumCull(boundingFrustum, drawableTransforms))
foreach (var drawable in FrustumCull(boundingFrustum, drawables))
{
Deferred_GBufferEffect.World = transform;
Deferred_GBufferEffect.World = drawable.TransformMatrix;
Deferred_GBufferEffect.HardwareInstancingEnabled = false;
@ -439,6 +430,8 @@ namespace Kav
Deferred_GBufferEffect.Metallic = drawable.Metallic;
Deferred_GBufferEffect.Roughness = drawable.Roughness;
Deferred_GBufferEffect.UVData = drawable.UVData.ToVector4();
Deferred_GBufferEffect.AlbedoTexture = drawable.AlbedoTexture;
Deferred_GBufferEffect.NormalTexture = drawable.NormalTexture;
Deferred_GBufferEffect.MetallicRoughnessTexture = drawable.MetallicRoughnessTexture;
@ -684,7 +677,7 @@ namespace Kav
public void RenderDirectionalShadowsIndexed<T>(
DirectionalShadowMapData shadowMapData,
IEnumerable<(T, Matrix)> drawableTransforms
IEnumerable<T> drawableTransforms
) where T : ICullable, IIndexDrawable {
// render the individual shadow cascades
for (var i = 0; i < shadowMapData.NumShadowCascades; i++)
@ -700,7 +693,7 @@ namespace Kav
private void RenderDirectionalShadowMapIndexed<T>(
DirectionalShadowMapData shadowMapData,
int shadowCascadeIndex,
IEnumerable<(T, Matrix)> drawableTransforms
IEnumerable<T> drawables
) where T : ICullable, IIndexDrawable {
GraphicsDevice.SetRenderTarget(shadowMapData.ShadowMaps[shadowCascadeIndex]);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
@ -712,7 +705,7 @@ namespace Kav
CullAndRenderIndexed(
GraphicsDevice,
new BoundingFrustum(SimpleDepthEffect.View * SimpleDepthEffect.Projection),
drawableTransforms,
drawables,
SimpleDepthEffect
);
}
@ -763,7 +756,7 @@ namespace Kav
public void RenderPointShadowMapIndexed<T>(
RenderTargetCube pointShadowCubeMap,
IEnumerable<(T, Matrix)> modelTransforms,
IEnumerable<T> drawables,
PointLight pointLight
) where T : ICullable, IIndexDrawable {
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
@ -831,7 +824,7 @@ namespace Kav
CullAndRenderIndexed(
GraphicsDevice,
new BoundingFrustum(LinearDepthEffect.View * LinearDepthEffect.Projection),
modelTransforms,
drawables,
LinearDepthEffect
);
}
@ -843,7 +836,7 @@ namespace Kav
VertexBuffer instanceVertexBuffer,
int numInstances,
PointLight pointLight
) where T : ICullable, IIndexDrawable
) where T : IIndexDrawable
{
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.Opaque;
@ -917,31 +910,21 @@ namespace Kav
}
}
private static IEnumerable<(T, Matrix)> FrustumCull<T>(
private static IEnumerable<T> FrustumCull<T>(
BoundingFrustum boundingFrustum,
IEnumerable<(T, Matrix)> cullableTransforms
IEnumerable<T> cullables
) where T : ICullable {
foreach (var (cullable, transform) in cullableTransforms)
foreach (var cullable in cullables)
{
var boundingBox = TransformedBoundingBox(cullable.BoundingBox, transform);
var boundingBox = TransformedBoundingBox(cullable.BoundingBox, cullable.TransformMatrix);
var containment = boundingFrustum.Contains(boundingBox);
if (containment != ContainmentType.Disjoint)
{
yield return (cullable, transform);
yield return cullable;
}
}
}
private static bool FrustumCull<T>(
BoundingFrustum boundingFrustum,
T cullable,
Matrix transform
) where T : ICullable {
var boundingBox = TransformedBoundingBox(cullable.BoundingBox, transform);
var containment = boundingFrustum.Contains(boundingBox);
return (containment == ContainmentType.Disjoint);
}
private static BoundingBox TransformedBoundingBox(BoundingBox boundingBox, Matrix matrix)
{
var center = (boundingBox.Min + boundingBox.Max) / 2f;

View File

@ -12,7 +12,7 @@ namespace Kav
public static VertexDeclaration PositionTextureOffsetInstanceDeclaration = new VertexDeclaration
(
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.TextureCoordinate, 2),
new VertexElement(12, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 5)
new VertexElement(12, VertexElementFormat.Vector4, VertexElementUsage.Tangent, 0)
);
}
}

View File

@ -16,16 +16,16 @@ namespace Kav
}
public Vector3 Translation { get; set; }
public Vector2 UVOffset { get; set; }
public Vector4 UVData { get; set; }
public static readonly VertexDeclaration VertexDeclaration;
public PositionTextureOffsetInstanceVertex(
Vector3 translation,
Vector2 uvOffset
Vector4 uvData
) {
Translation = translation;
UVOffset = uvOffset;
UVData = uvData;
}
}
}