Deferred shading

This commit is contained in:
dexy
2019-12-04 00:52:22 +11:00
Unverified
parent 755477590d
commit 9a53c2147b
60 changed files with 2724 additions and 509 deletions
+129
View File
@@ -421,4 +421,133 @@ namespace CodeWalker.Rendering
}
public class GpuMultiTexture //multiple texture and render targets (depth).
{
public Texture2D[] Textures;
public Texture2D Depth;
public RenderTargetView[] RTVs;
public DepthStencilView DSV;
public ShaderResourceView[] SRVs;
public ShaderResourceView DepthSRV;
public int VramUsage;
public bool UseDepth;
public int Count;
public void Init(Device device, int w, int h, int count, Format f, bool depth, Format df)
{
Count = count;
VramUsage = 0;
UseDepth = depth;
ResourceUsage u = ResourceUsage.Default;
BindFlags b = BindFlags.RenderTarget | BindFlags.ShaderResource;
RenderTargetViewDimension rtvd = RenderTargetViewDimension.Texture2D;
ShaderResourceViewDimension srvd = ShaderResourceViewDimension.Texture2D;// D3D11_SRV_DIMENSION_TEXTURE2D;
int fs = DXUtility.ElementSize(f);
int wh = w * h;
BindFlags db = BindFlags.DepthStencil | BindFlags.ShaderResource;// D3D11_BIND_DEPTH_STENCIL;
DepthStencilViewDimension dsvd = DepthStencilViewDimension.Texture2D;
Textures = new Texture2D[count];
RTVs = new RenderTargetView[count];
SRVs = new ShaderResourceView[count];
for (int i = 0; i < count; i++)
{
Textures[i] = DXUtility.CreateTexture2D(device, w, h, 1, 1, f, 1, 0, u, b, 0, 0);
RTVs[i] = DXUtility.CreateRenderTargetView(device, Textures[i], f, rtvd, 0, 0, 0);
SRVs[i] = DXUtility.CreateShaderResourceView(device, Textures[i], f, srvd, 1, 0, 0, 0);
VramUsage += (wh * fs);
}
if (depth)
{
Format dtexf = Format.R32_Typeless;
Format dsrvf = Format.R32_Float;
switch (df)
{
case Format.D16_UNorm:
dtexf = Format.R16_Typeless;
dsrvf = Format.R16_UNorm;
break;
case Format.D24_UNorm_S8_UInt:
dtexf = Format.R24G8_Typeless;
dsrvf = Format.R24_UNorm_X8_Typeless;
break;
case Format.D32_Float:
dtexf = Format.R32_Typeless;
dsrvf = Format.R32_Float;
break;
case Format.D32_Float_S8X24_UInt:
dtexf = Format.R32G8X24_Typeless;//is this right? who uses this anyway??
dsrvf = Format.R32_Float_X8X24_Typeless;
break;
}
Depth = DXUtility.CreateTexture2D(device, w, h, 1, 1, dtexf, 1, 0, u, db, 0, 0);
DSV = DXUtility.CreateDepthStencilView(device, Depth, df, dsvd);
DepthSRV = DXUtility.CreateShaderResourceView(device, Depth, dsrvf, srvd, 1, 0, 0, 0);
VramUsage += (wh * DXUtility.ElementSize(df));
}
}
public void Dispose()
{
for (int i = 0; i < Count; i++)
{
SRVs[i].Dispose();
RTVs[i].Dispose();
Textures[i].Dispose();
}
SRVs = null;
RTVs = null;
Textures = null;
if (DSV != null)
{
DSV.Dispose();
DSV = null;
}
if (DepthSRV != null)
{
DepthSRV.Dispose();
DepthSRV = null;
}
if (Depth != null)
{
Depth.Dispose();
Depth = null;
}
}
public GpuMultiTexture(Device device, int w, int h, int count, Format f, bool depth, Format df)
{
Init(device, w, h, count, f, depth, df);
}
public GpuMultiTexture(Device device, int w, int h, int count, Format f)
{
Init(device, w, h, count, f, false, Format.Unknown);
}
public void Clear(DeviceContext context, Color4 colour)
{
for (int i = 0; i < Count; i++)
{
context.ClearRenderTargetView(RTVs[i], colour);
}
if (UseDepth)
{
context.ClearDepthStencilView(DSV, DepthStencilClearFlags.Depth, 0.0f, 0);
}
}
public void ClearDepth(DeviceContext context)
{
if (!UseDepth) return;
context.ClearDepthStencilView(DSV, DepthStencilClearFlags.Depth, 0.0f, 0);
}
public void SetRenderTargets(DeviceContext context)
{
context.OutputMerger.SetRenderTargets(UseDepth ? DSV : null, RTVs);
}
}
}
+3 -3
View File
@@ -33,7 +33,7 @@ namespace CodeWalker.Rendering
}
}
public UnitCapsule(Device device, byte[] vsbytes, int detail)
public UnitCapsule(Device device, byte[] vsbytes, int detail, bool invert = false)
{
InputLayout = new InputLayout(device, vsbytes, new[]
@@ -208,8 +208,8 @@ namespace CodeWalker.Rendering
foreach (var tri in curtris)
{
idata.Add((uint)tri.v1);
idata.Add((uint)tri.v2);
idata.Add((uint)tri.v3);
idata.Add((uint)(invert ? tri.v3 : tri.v2));
idata.Add((uint)(invert ? tri.v2 : tri.v3));
}
+152
View File
@@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpDX;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using Device = SharpDX.Direct3D11.Device;
using Buffer = SharpDX.Direct3D11.Buffer;
using SharpDX.DXGI;
namespace CodeWalker.Rendering
{
public class UnitCone
{
private Buffer VertexBuffer { get; set; }
private Buffer IndexBuffer { get; set; }
private InputLayout InputLayout { get; set; }
private VertexBufferBinding vbbinding;
private int indexcount;
private struct SphTri
{
public int v1;
public int v2;
public int v3;
public SphTri(int i1, int i2, int i3)
{
v1 = i1;
v2 = i2;
v3 = i3;
}
}
public UnitCone(Device device, byte[] vsbytes, int detail, bool invert = false)
{
InputLayout = new InputLayout(device, vsbytes, new[]
{
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0),
});
List<Vector4> verts = new List<Vector4>();
Dictionary<Vector4, int> vdict = new Dictionary<Vector4, int>();
List<SphTri> curtris = new List<SphTri>();
verts.Add(new Vector4(0.0f, 0.0f, 0.0f, 0.0f));//top end (translated by VS!)
verts.Add(new Vector4(0.0f, -1.0f, 0.0f, 0.0f));//top normal
verts.Add(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));//bottom end
verts.Add(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));//bottom normal
int nlons = detail * 4;
int lastlon = nlons - 1;
float latrng = 1.0f / (detail);
float lonrng = 1.0f / (nlons);
float twopi = (float)(2.0 * Math.PI);
for (int lon = 0; lon < nlons; lon++)
{
float tlon = lon * lonrng;
float rlon = tlon * twopi;
float lonx = (float)Math.Sin(rlon);
float lonz = (float)Math.Cos(rlon);
verts.Add(new Vector4(lonx, 0.0f, lonz, 1.0f));//2
verts.Add(new Vector4(lonx, 0.0f, lonz, 0.0f));//side normal
verts.Add(new Vector4(lonx, 0.0f, lonz, 1.0f));//3
verts.Add(new Vector4(0.0f, 1.0f, 0.0f, 0.0f));//bottom normal
}
for (int lon = 0; lon < nlons; lon++)
{
int i0 = 2 + lon * 2;
int i1 = i0 + 2;
if (lon == lastlon)
{
i1 = 2;
}
curtris.Add(new SphTri(0, i0, i1)); //fill the cone
curtris.Add(new SphTri(1, i1+1, i0+1)); //bottom cap triangles
}
List<uint> idata = new List<uint>();
foreach (var tri in curtris)
{
idata.Add((uint)tri.v1);
idata.Add((uint)(invert ? tri.v2 : tri.v3));
idata.Add((uint)(invert ? tri.v3 : tri.v2));
}
VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, verts.ToArray());
vbbinding = new VertexBufferBinding(VertexBuffer, 32, 0);
IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, idata.ToArray());
indexcount = idata.Count;
}
public void Draw(DeviceContext context)
{
context.InputAssembler.InputLayout = InputLayout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
context.InputAssembler.SetVertexBuffers(0, vbbinding);
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
context.DrawIndexed(indexcount, 0, 0);
}
public void DrawInstanced(DeviceContext context, int count)
{
context.InputAssembler.InputLayout = InputLayout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
context.InputAssembler.SetVertexBuffers(0, vbbinding);
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
context.DrawIndexedInstanced(indexcount, count, 0, 0, 0);
}
public void Dispose()
{
if (VertexBuffer != null)
{
VertexBuffer.Dispose();
VertexBuffer = null;
}
if (IndexBuffer != null)
{
IndexBuffer.Dispose();
IndexBuffer = null;
}
if (InputLayout != null)
{
InputLayout.Dispose();
InputLayout = null;
}
}
}
}
+3 -3
View File
@@ -33,7 +33,7 @@ namespace CodeWalker.Rendering
}
}
public UnitSphere(Device device, byte[] vsbytes, int detail)
public UnitSphere(Device device, byte[] vsbytes, int detail, bool invert = false)
{
InputLayout = new InputLayout(device, vsbytes, new[]
@@ -121,8 +121,8 @@ namespace CodeWalker.Rendering
foreach (var tri in curtris)
{
idata.Add((uint)tri.v1);
idata.Add((uint)tri.v2);
idata.Add((uint)tri.v3);
idata.Add((uint)(invert ? tri.v3 : tri.v2));
idata.Add((uint)(invert ? tri.v2 : tri.v3));
}