Skip to content

Commit

Permalink
Fix vertex pulling
Browse files Browse the repository at this point in the history
  • Loading branch information
deccer committed Dec 8, 2023
1 parent df7aabb commit 3efa064
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ mat3 CreateTbnMatrix(in mat3 transform)
void main()
{
GpuModelMeshInstance modelMeshInstance = instanceBuffer.Instances[gl_BaseInstance + gl_DrawID];

v_mesh_material_id = modelMeshInstance.MaterialId.x;
v_position = (modelMeshInstance.WorldMatrix * vec4(i_position, 1.0)).xyz;

v_uv = i_uv;
v_mesh_material_id = modelMeshInstance.MaterialId.x;
v_tbn = CreateTbnMatrix(mat3(transpose(inverse(mat3(modelMeshInstance.WorldMatrix)))));

gl_Position = cameraInformation.ProjectionMatrix * cameraInformation.ViewMatrix * vec4(v_position, 1.0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
struct PackedVec2
{
float x;
float y;
};

struct PackedVec3
{
float x;
float y;
float z;
};

struct PackedVec4
{
float x;
float y;
float z;
float w;
};

vec2 PackedToVec2(in PackedVec2 v)
{
return vec2(v.x, v.y);
}

PackedVec2 Vec2ToPacked(in vec2 v)
{
return PackedVec2(v.x, v.y);
}

vec3 PackedToVec3(in PackedVec3 v)
{
return vec3(v.x, v.y, v.z);
}

PackedVec3 Vec3ToPacked(in vec3 v)
{
return PackedVec3(v.x, v.y, v.z);
}

vec4 PackedToVec4(in PackedVec4 v)
{
return vec4(v.x, v.y, v.z, v.w);
}

PackedVec4 Vec4ToPacked(in vec4 v)
{
return PackedVec4(v.x, v.y, v.z, v.w);
}

struct Vertex
{
PackedVec3 position;
PackedVec3 normal;
PackedVec2 uv;
PackedVec4 tangent;
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ struct GpuModelMeshInstance

layout(binding = 1, std430) readonly buffer ModelMeshInstanceBuffer
{
GpuModelMeshInstance Instances[];
} modelMeshInstanceBuffer;
GpuModelMeshInstance ModelMeshInstances[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ layout(location = 3) out flat int v_model_mesh_material_id;

void main()
{
GpuModelMeshInstance modelMeshInstance = modelMeshInstanceBuffer.Instances[gl_BaseInstance];
GpuModelMeshInstance modelMeshInstance = ModelMeshInstances[gl_BaseInstance];

v_model_mesh_material_id = modelMeshInstance.MaterialId.x;
v_position = (modelMeshInstance.World * vec4(i_position, 1.0)).xyz;
v_normal = normalize(inverse(transpose(mat3(modelMeshInstance.World))) * i_normal);
v_uv = i_uv;
v_model_mesh_material_id = modelMeshInstance.MaterialId.x;

gl_Position = ViewProj * vec4(v_position, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#version 460 core

#include "BaseTypes.glsl"

layout (location = 0) out gl_PerVertex
{
vec4 gl_Position;
};
layout(location = 0) out vec3 v_position;
layout(location = 1) out vec3 v_normal;
layout(location = 2) out vec2 v_uv;
layout(location = 3) out flat int v_model_mesh_material_id;

layout(std430, binding = 3) restrict readonly buffer VertexBuffer
{
Vertex Vertices[];
};

#include "Common.glsl"

void main()
{
Vertex vertex = Vertices[gl_VertexID];

GpuModelMeshInstance modelMeshInstance = ModelMeshInstances[gl_BaseInstance];
v_position = (modelMeshInstance.World * vec4(PackedToVec3(vertex.position), 1.0)).xyz;
v_normal = normalize(inverse(transpose(mat3(modelMeshInstance.World))) * PackedToVec3(vertex.normal));
v_uv = PackedToVec2(vertex.uv);
v_model_mesh_material_id = modelMeshInstance.MaterialId.x;
gl_Position = ViewProj * vec4(v_position, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal sealed class DeferredRenderingApplication : GraphicsApplication
private ISampler? _pointSampler;

private IGraphicsPipeline? _gBufferGraphicsPipeline;
private IGraphicsPipeline? _gBufferVertexPullingGraphicsPipeline;
private FramebufferDescriptor _gBufferFramebufferDescriptor;
private ITexture? _gBufferBaseColorTexture;
private ITexture? _gBufferNormalTexture;
Expand All @@ -56,6 +57,7 @@ internal sealed class DeferredRenderingApplication : GraphicsApplication
private IBuffer _gpuMaterialBuffer;

private IDictionary<string, ITexture> _textures;
private bool _useVertexPulling;

public DeferredRenderingApplication(
ILogger logger,
Expand Down Expand Up @@ -155,24 +157,46 @@ protected override void Render(float deltaTime)
_gpuModelMeshInstanceBuffer.Update(_gpuModelMeshInstances.ToArray(), 0);

GraphicsContext.BeginRenderPass(_gBufferFramebufferDescriptor);
GraphicsContext.BindGraphicsPipeline(_gBufferGraphicsPipeline);
_gBufferGraphicsPipeline.BindAsUniformBuffer(_gpuCameraConstantsBuffer, 0);
_gBufferGraphicsPipeline.BindAsShaderStorageBuffer(_gpuModelMeshInstanceBuffer, 1);
_gBufferGraphicsPipeline.BindAsShaderStorageBuffer(_gpuMaterialBuffer, 2);
_gBufferGraphicsPipeline.BindAsVertexBuffer(_gpuVertexBuffer, 0, Offset.Zero);
_gBufferGraphicsPipeline.BindAsIndexBuffer(_gpuIndexBuffer);

for (var i = 0; i < _drawCommands.Count; i++)
if (_useVertexPulling)
{
var drawCommand = _drawCommands[i];
_gBufferGraphicsPipeline.DrawElementsInstancedBaseVertexBaseInstance(
drawCommand.IndexCount,
drawCommand.IndexOffset,
1,
drawCommand.VertexOffset,
i);
GraphicsContext.BindGraphicsPipeline(_gBufferVertexPullingGraphicsPipeline);
_gBufferVertexPullingGraphicsPipeline.BindAsUniformBuffer(_gpuCameraConstantsBuffer, 0);
_gBufferVertexPullingGraphicsPipeline.BindAsShaderStorageBuffer(_gpuModelMeshInstanceBuffer, 1);
_gBufferVertexPullingGraphicsPipeline.BindAsShaderStorageBuffer(_gpuMaterialBuffer, 2);
_gBufferVertexPullingGraphicsPipeline.BindAsShaderStorageBuffer(_gpuVertexBuffer, 3);
_gBufferVertexPullingGraphicsPipeline.BindAsIndexBuffer(_gpuIndexBuffer);

for (var i = 0; i < _drawCommands.Count; i++)
{
var drawCommand = _drawCommands[i];
_gBufferVertexPullingGraphicsPipeline.DrawElementsInstancedBaseVertexBaseInstance(
drawCommand.IndexCount,
drawCommand.IndexOffset,
1,
drawCommand.VertexOffset,
i);
}
}
else
{
GraphicsContext.BindGraphicsPipeline(_gBufferGraphicsPipeline);
_gBufferGraphicsPipeline.BindAsUniformBuffer(_gpuCameraConstantsBuffer, 0);
_gBufferGraphicsPipeline.BindAsShaderStorageBuffer(_gpuModelMeshInstanceBuffer, 1);
_gBufferGraphicsPipeline.BindAsShaderStorageBuffer(_gpuMaterialBuffer, 2);
_gBufferGraphicsPipeline.BindAsVertexBuffer(_gpuVertexBuffer, 0);
_gBufferGraphicsPipeline.BindAsIndexBuffer(_gpuIndexBuffer);

for (var i = 0; i < _drawCommands.Count; i++)
{
var drawCommand = _drawCommands[i];
_gBufferGraphicsPipeline.DrawElementsInstancedBaseVertexBaseInstance(
drawCommand.IndexCount,
drawCommand.IndexOffset,
1,
drawCommand.VertexOffset,
i);
}
}

GraphicsContext.EndRender();
GL.PopDebugGroup();

Expand Down Expand Up @@ -219,6 +243,8 @@ protected override void Render(float deltaTime)
ImGui.SliderFloat("Camera Sensitivity", ref sensitivity, 0.01f, 1.0f);
_camera.Sensitivity = sensitivity;

ImGui.Checkbox("Use Vertex Pulling", ref _useVertexPulling);

ImGui.Image((nint)_gBufferBaseColorTexture.Id, new Vector2(320, 180), new Vector2(0, 1), new Vector2(1, 0));
ImGui.Image((nint)_gBufferNormalTexture.Id, new Vector2(320, 180), new Vector2(0, 1), new Vector2(1, 0));
ImGui.Image((nint)_gBufferDepthTexture.Id, new Vector2(320, 180), new Vector2(0, 1), new Vector2(1, 0));
Expand Down Expand Up @@ -365,8 +391,7 @@ private void PrepareScene()

var meshDatasAsArray = meshPrimitives.ToArray();

_gpuVertexBuffer =
GraphicsContext.CreateVertexBuffer("SceneVertices", meshDatasAsArray, VertexType.PositionNormalUvTangent);
_gpuVertexBuffer = GraphicsContext.CreateVertexBuffer("SceneVertices", meshDatasAsArray);
_gpuIndexBuffer = GraphicsContext.CreateIndexBuffer("SceneIndices", meshDatasAsArray);
}

Expand Down Expand Up @@ -440,6 +465,18 @@ private bool CreatePipelines()
}

_gBufferGraphicsPipeline = gBufferGraphicsPipelineResult.Value;

var gBufferVertexPullingGraphicsPipelineResult = GraphicsContext.CreateGraphicsPipelineBuilder()
.WithShadersFromFiles("Shaders/SceneVertexPulling.vs.glsl", "Shaders/Scene.fs.glsl")
.WithCullingEnabled(CullMode.Back)
.Build("GBuffer-Pipeline");
if (gBufferVertexPullingGraphicsPipelineResult.IsFailure)
{
_logger.Error(gBufferVertexPullingGraphicsPipelineResult.Error);
return false;
}

_gBufferVertexPullingGraphicsPipeline = gBufferVertexPullingGraphicsPipelineResult.Value;

var finalGraphicsPipelineResult = GraphicsContext.CreateGraphicsPipelineBuilder()
.WithShadersFromFiles("Shaders/FST.vs.glsl", "Shaders/Texture.fs.glsl")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ private bool LoadMeshes()
vertexOffset += meshData.VertexCount;
}

_skullVertexBuffer = GraphicsContext.CreateVertexBuffer("SkullVertices", meshDates, VertexType.PositionNormalUvTangent);
_skullVertexBuffer = GraphicsContext.CreateVertexBuffer("SkullVertices", meshDates);
_skullIndexBuffer = GraphicsContext.CreateIndexBuffer("SkullIndices", meshDates);

return true;
Expand Down
4 changes: 0 additions & 4 deletions src/EngineKit/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,5 @@ public static void AddEngine(this IServiceCollection services)

services.AddSingleton<IImageLoader, SixLaborsImageLoader>();
services.AddSingleton<IKtxImageLoader, KtxImageLoader>();

services.AddSingleton<IDictionary<int, IInputLayout>, Dictionary<int, IInputLayout>>();
services.AddSingleton<IDictionary<IPipeline, GraphicsPipelineDescriptor>, Dictionary<IPipeline, GraphicsPipelineDescriptor>>();
services.AddSingleton<IDictionary<IPipeline, ComputePipelineDescriptor>, Dictionary<IPipeline, ComputePipelineDescriptor>>();
}
}
18 changes: 3 additions & 15 deletions src/EngineKit/Graphics/GraphicsContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using EngineKit.Extensions;
Expand Down Expand Up @@ -179,10 +178,8 @@ public IBuffer CreateVertexBuffer<TVertex>(Label label)

public IBuffer CreateVertexBuffer(
Label label,
MeshPrimitive[] meshPrimitives,
VertexType targetVertexType)
MeshPrimitive[] meshPrimitives)
{
//TODO(deccer) return vertex buffers depending on targetVertexType
var bufferData = new List<VertexPositionNormalUvTangent>(1_024_000);
foreach (var meshPrimitive in meshPrimitives)
{
Expand All @@ -200,17 +197,8 @@ public IBuffer CreateVertexBuffer(
meshPrimitive.RealTangents[i]));
}
}

IBuffer vertexBuffer;
switch (targetVertexType)
{
case VertexType.PositionNormalUvTangent:
vertexBuffer = new Buffer<VertexPositionNormalUvTangent>(BufferTarget.VertexBuffer, label);
break;
default:
throw new InvalidEnumArgumentException(nameof(targetVertexType));
}


var vertexBuffer = new Buffer<VertexPositionNormalUvTangent>(BufferTarget.VertexBuffer, label);
vertexBuffer.AllocateStorage(bufferData.ToArray(), StorageAllocationFlags.None);
return vertexBuffer;
}
Expand Down
19 changes: 14 additions & 5 deletions src/EngineKit/Graphics/IGraphicsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ void BlitFramebufferToSwapchain(
int targetWidth,
int targetHeight);

bool TryMapBuffer(IBuffer buffer, MemoryAccess memoryAccess, out nint bufferPtr);
bool TryMapBuffer(
IBuffer buffer,
MemoryAccess memoryAccess,
out nint bufferPtr);

void UnmapBuffer(IBuffer buffer);

Expand All @@ -45,8 +48,7 @@ IBuffer CreateVertexBuffer<TVertex>(Label label)

IBuffer CreateVertexBuffer(
Label label,
MeshPrimitive[] meshPrimitives,
VertexType targetVertexType);
MeshPrimitive[] meshPrimitives);

ISampler CreateSampler(SamplerDescriptor samplerDescriptor);

Expand Down Expand Up @@ -98,9 +100,15 @@ void CopyTexture(
FramebufferBit framebufferBit,
BlitFramebufferFilter interpolationFilter);

IMeshPool CreateMeshPool(Label label, int vertexBufferCapacity, int indexBufferCapacity);
IMeshPool CreateMeshPool(
Label label,
int vertexBufferCapacity,
int indexBufferCapacity);

IMaterialPool CreateMaterialPool(Label label, int materialBufferCapacity, ISamplerLibrary samplerLibrary);
IMaterialPool CreateMaterialPool(
Label label,
int materialBufferCapacity,
ISamplerLibrary samplerLibrary);

void EndRender();

Expand All @@ -115,5 +123,6 @@ void CopyTexture(
FramebufferDescriptor CreateSingleFramebufferDescriptorFromTexture(ITexture texture);

void ClearResourceBindings();

void UseViewport(Viewport viewport);
}
Loading

0 comments on commit 3efa064

Please sign in to comment.