Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
-MSAA and MRT support :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Cassunshine committed Dec 11, 2023
1 parent a58e47e commit fcccf5c
Show file tree
Hide file tree
Showing 18 changed files with 395 additions and 96 deletions.
25 changes: 7 additions & 18 deletions Client/Content/shaders/blit.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,16 @@ layout(location = 0) in vec3 Position;
layout(location = 0) out vec2 fsin_texCoords;

layout (set = 1, binding = 0) uniform TextureDrawParams {
vec2 SrcMin;
vec2 SrcMax;
vec2 DstMin;
vec2 DstMax;
vec2 SrcSize;
vec2 DstSize;
bool flip;
};

void main() {
vec2 scaledDstMin = DstMin / DstSize;
vec2 scaledDstMax = DstMax / DstSize;

//Move vertex to correct place on texture.
vec2 uv = (scaledDstMin + (scaledDstMax - scaledDstMin) * Position.xy);
uv.y = 1-uv.y;

gl_Position = vec4((uv * 2) - 1, 0, 1);

vec2 scaledSrcMin = SrcMin / SrcSize;
vec2 scaledSrcMax = SrcMax / SrcSize;
gl_Position = vec4((Position.xy * 2) - 1, 0, 1);

//Sample from source texture.
fsin_texCoords = scaledSrcMin + (scaledSrcMax - scaledSrcMin) * Position.xy;

vec2 uv = Position.xy;
if (flip)
uv = vec2(uv.x, 1 - uv.y);
fsin_texCoords = uv;
}
3 changes: 3 additions & 0 deletions Client/Content/shaders/simple.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ layout(location = 3) in vec2 fsin_UvMin;
layout(location = 4) in vec2 fsin_UvMax;

layout(location = 0) out vec4 fsout_Color;
layout(location = 1) out vec4 gbuffer_Color;

layout (set = 1, binding = 0) uniform sampler TextureSampler;
layout (set = 1, binding = 1) uniform texture2D Texture;
Expand Down Expand Up @@ -40,6 +41,8 @@ void interpolatePixels() {
texture(sampler2D(Texture, TextureSampler), vec2(newUvMax.x, newUvMin.y))
) / 4;
fsout_Color = sampledColor * fsin_Color;

gbuffer_Color = vec4(1,1,0,1);
}

void main() {
Expand Down
11 changes: 11 additions & 0 deletions Client/Content/shaders/stitcher.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 450

layout(location = 0) in vec2 fsin_texCoords;
layout(location = 0) out vec4 fsout_Color;

layout (set = 0, binding = 0) uniform sampler TextureSampler;
layout (set = 0, binding = 1) uniform texture2D Texture;

void main() {
fsout_Color = texture(sampler2D(Texture, TextureSampler), fsin_texCoords);
}
30 changes: 30 additions & 0 deletions Client/Content/shaders/stitcher.vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#version 450

layout(location = 0) in vec3 Position;
layout(location = 0) out vec2 fsin_texCoords;

layout (set = 1, binding = 0) uniform TextureDrawParams {
vec2 SrcMin;
vec2 SrcMax;
vec2 DstMin;
vec2 DstMax;
vec2 SrcSize;
vec2 DstSize;
};

void main() {
vec2 scaledDstMin = DstMin / DstSize;
vec2 scaledDstMax = DstMax / DstSize;

//Move vertex to correct place on texture.
vec2 uv = (scaledDstMin + (scaledDstMax - scaledDstMin) * Position.xy);
uv.y = 1-uv.y;

gl_Position = vec4((uv * 2) - 1, 0, 1);

vec2 scaledSrcMin = SrcMin / SrcSize;
vec2 scaledSrcMax = SrcMax / SrcSize;

//Sample from source texture.
fsin_texCoords = scaledSrcMin + (scaledSrcMax - scaledSrcMin) * Position.xy;
}
30 changes: 15 additions & 15 deletions Client/Rendering/Debug/DebugRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DebugRenderer : Renderer {
private const int BatchSize = 8192 * 4;
private static DebugRenderer Instance;

public readonly Pipeline DebugPipeline;
public Pipeline DebugPipeline;
private readonly DeviceBuffer vertexBuffer;

private readonly DebugVertex[] DebugVertices = new DebugVertex[BatchSize];
Expand All @@ -23,15 +23,21 @@ public class DebugRenderer : Renderer {
public DebugRenderer(VoxelClient client) : base(client) {
Instance = this;

if (!client.RenderSystem.ShaderManager.GetShaders("shaders/debug", out var shaders))

vertexBuffer = ResourceFactory.CreateBuffer(new BufferDescription {
Usage = BufferUsage.Dynamic | BufferUsage.VertexBuffer, SizeInBytes = (uint)Marshal.SizeOf<DebugVertex>() * BatchSize
});
}
public override void CreatePipeline(MainFramebuffer framebuffer) {
if (!Client.RenderSystem.ShaderManager.GetShaders("shaders/debug", out var shaders))
throw new("Shaders not present.");

DebugPipeline = ResourceFactory.CreateGraphicsPipeline(new() {
BlendState = BlendStateDescription.SingleOverrideBlend,
DepthStencilState = new() {
DepthComparison = ComparisonKind.LessEqual, DepthTestEnabled = true, DepthWriteEnabled = true,
},
Outputs = RenderSystem.GraphicsDevice.SwapchainFramebuffer.OutputDescription,
Outputs = framebuffer.Framebuffer.OutputDescription,
PrimitiveTopology = PrimitiveTopology.LineList,
RasterizerState = new() {
CullMode = FaceCullMode.None, DepthClipEnabled = false, FillMode = PolygonFillMode.Wireframe, ScissorTestEnabled = false,
Expand All @@ -46,35 +52,29 @@ public DebugRenderer(VoxelClient client) : base(client) {
Shaders = shaders
}
});


vertexBuffer = ResourceFactory.CreateBuffer(new BufferDescription {
Usage = BufferUsage.Dynamic | BufferUsage.VertexBuffer, SizeInBytes = (uint)Marshal.SizeOf<DebugVertex>() * BatchSize
});
}

public override void Render(double delta) {
Flush();
//Flush();
}

private void Flush() {
return;
if (vertexIndex == 0)
return;

CommandList.UpdateBuffer(vertexBuffer, 0, DebugVertices.AsSpan(0, vertexIndex));
SetPipeline(DebugPipeline);

CommandList.SetGraphicsResourceSet(0, Client.GameRenderer.CameraStateManager.CameraResourceSet);

CommandList.SetVertexBuffer(0, vertexBuffer);
CommandList.Draw((uint)vertexIndex);

vertexIndex = 0;

//RestoreLastPipeline();
}

public override void Dispose() {
DebugPipeline.Dispose();

}

private void AddPoint(dvec3 pos) {
Expand Down Expand Up @@ -129,12 +129,12 @@ public static void DrawCube(dvec3 min, dvec3 max, float expansion = 0) {
DrawLine(new dvec3(realMax.x, realMin.y, realMax.z), new dvec3(realMax.x, realMin.y, realMin.z));
DrawLine(new dvec3(realMax.x, realMin.y, realMin.z), new dvec3(realMin.x, realMin.y, realMin.z));


DrawLine(new dvec3(realMin.x, realMax.y, realMin.z), new dvec3(realMin.x, realMax.y, realMax.z));
DrawLine(new dvec3(realMin.x, realMax.y, realMax.z), new dvec3(realMax.x, realMax.y, realMax.z));
DrawLine(new dvec3(realMax.x, realMax.y, realMax.z), new dvec3(realMax.x, realMax.y, realMin.z));
DrawLine(new dvec3(realMax.x, realMax.y, realMin.z), new dvec3(realMin.x, realMax.y, realMin.z));


DrawLine(new dvec3(realMin.x, realMin.y, realMin.z), new dvec3(realMin.x, realMax.y, realMin.z));
DrawLine(new dvec3(realMin.x, realMin.y, realMax.z), new dvec3(realMin.x, realMax.y, realMax.z));
Expand Down
59 changes: 52 additions & 7 deletions Client/Rendering/GameRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
using System;
using GlmSharp;
using Voxel.Client.Keybinding;
using Veldrid;
using Voxel.Client.Rendering.Debug;
using Voxel.Client.Rendering.Gui;
using Voxel.Client.Rendering.World;
using Voxel.Common.Collision;
using Voxel.Common.World;

namespace Voxel.Client.Rendering;

public class GameRenderer : Renderer {

public MainFramebuffer Framebuffer { get; private set; }
private uint msaaLevel = 1;
private bool needMainBufferRefresh = true;

/// <summary>
/// Main camera, used to render main game window.
/// Cannot be destroyed, it's essential for basic game rendering.
/// </summary>
public readonly Camera MainCamera;
public readonly CameraStateManager CameraStateManager;


public readonly WorldRenderer WorldRenderer;
public readonly GuiRenderer GuiRenderer;
public readonly CameraStateManager CameraStateManager;

public readonly BlitRenderer BlitRenderer;
public readonly DebugRenderer DebugRenderer;


public GameRenderer(VoxelClient client) : base(client) {
//Jank but OK
client.GameRenderer = this;
Expand All @@ -31,12 +37,34 @@ public GameRenderer(VoxelClient client) : base(client) {
WorldRenderer = new(client);
GuiRenderer = new(client);


DebugRenderer = new DebugRenderer(client);
BlitRenderer = new(client);
DebugRenderer = new(client);
}

public override void CreatePipeline(MainFramebuffer framebuffer) {
WorldRenderer.CreatePipeline(framebuffer);
GuiRenderer.CreatePipeline(framebuffer);

BlitRenderer.CreatePipeline(framebuffer);
DebugRenderer.CreatePipeline(framebuffer);
}
public override void Render(double delta) {

if (needMainBufferRefresh) {
needMainBufferRefresh = false;

if (Framebuffer != null)
Framebuffer.Dispose();
Framebuffer = new MainFramebuffer(ResourceFactory, RenderSystem.GraphicsDevice.MainSwapchain.Framebuffer, (uint)Client.NativeWindow.Width, (uint)Client.NativeWindow.Height, msaaLevel);

CreatePipeline(Framebuffer);
}

CommandList.SetFramebuffer(Framebuffer.Framebuffer);
CommandList.ClearColorTarget(0, RgbaFloat.Grey);
CommandList.ClearColorTarget(1, RgbaFloat.Green);
CommandList.ClearDepthStencil(1);

MainCamera.position = Client.PlayerEntity?.SmoothPosition(Client.smoothFactor) + Client.PlayerEntity?.eyeOffset ?? dvec3.Zero;
MainCamera.rotationVec = Client.PlayerEntity?.SmoothRotation(Client.smoothFactor) ?? dvec2.Zero;
CameraStateManager.SetToCamera(MainCamera, Client.timeSinceLastTick);
Expand All @@ -45,12 +73,29 @@ public override void Render(double delta) {
GuiRenderer.Render(delta);

DebugRenderer.Render(delta);

Framebuffer.Resolve(RenderSystem);

BlitRenderer.Blit(Framebuffer.ResolvedMainColor, RenderSystem.GraphicsDevice.MainSwapchain.Framebuffer, true);
}


public void RecreateMainFramebuffer() {
needMainBufferRefresh = true;
}

public void SetMSAA(uint value) {
msaaLevel = value;
RecreateMainFramebuffer();
}

public override void Dispose() {
WorldRenderer.Dispose();
GuiRenderer.Dispose();

BlitRenderer.Dispose();
DebugRenderer.Dispose();

Framebuffer.Dispose();
}
}
25 changes: 13 additions & 12 deletions Client/Rendering/Gui/GuiRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
using Veldrid;
using Voxel.Client.Rendering.VertexTypes;

namespace Voxel.Client.Rendering.Gui;
namespace Voxel.Client.Rendering.Gui;

public class GuiRenderer : Renderer, IDisposable {
public readonly Pipeline GuiPipeline;
public Pipeline GuiPipeline;

public GuiRenderer(VoxelClient client) : base(client) {
if (!client.RenderSystem.ShaderManager.GetShaders("shaders/gui", out var shaders))

}

public override void CreatePipeline(MainFramebuffer framebuffer) {
if (!Client.RenderSystem.ShaderManager.GetShaders("shaders/gui", out var shaders))
throw new("Shaders not present.");
GuiPipeline = ResourceFactory.CreateGraphicsPipeline(new() {

GuiPipeline = framebuffer.AddDependency(ResourceFactory.CreateGraphicsPipeline(new() {
BlendState = BlendStateDescription.SingleAlphaBlend,
DepthStencilState = new() {
DepthComparison = ComparisonKind.Never,
DepthTestEnabled = false,
DepthWriteEnabled = false,
DepthComparison = ComparisonKind.Never, DepthTestEnabled = false, DepthWriteEnabled = false,
},
Outputs = RenderSystem.GraphicsDevice.SwapchainFramebuffer.OutputDescription,
PrimitiveTopology = PrimitiveTopology.TriangleList,
Expand All @@ -36,15 +38,14 @@ public GuiRenderer(VoxelClient client) : base(client) {
},
Shaders = shaders
}
});
}));
}

public override void Render(double delta) {
CommandList.SetPipeline(GuiPipeline);
//CommandList.SetPipeline(GuiPipeline);

//CommandList.SetGraphicsResourceSet(0, TerrainAtlas.AtlasResourceSet);

CommandList.SetIndexBuffer(RenderSystem.CommonIndexBuffer, IndexFormat.UInt32);
//CommandList.SetIndexBuffer(RenderSystem.CommonIndexBuffer, IndexFormat.UInt32);
}
public override void Dispose() {}
}
Loading

0 comments on commit fcccf5c

Please sign in to comment.