-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Renderable and SimpleRenderSystem
- Loading branch information
Showing
7 changed files
with
182 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Bliss.CSharp.Colors; | ||
using Bliss.CSharp.Geometry; | ||
using Bliss.CSharp.Transformations; | ||
|
||
namespace Bliss.CSharp.Rendering; | ||
|
||
public class Renderable { | ||
|
||
public Model Model; | ||
public Color Color; | ||
public Transform Transform; | ||
|
||
public readonly uint Id; | ||
private static uint _ids; | ||
|
||
public Renderable(Model model, Color color, Transform transform) { | ||
this.Id = ++_ids; | ||
this.Model = model; | ||
this.Color = color; | ||
this.Transform = transform; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Bliss/CSharp/Rendering/Systems/SimplePushConstantData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Numerics; | ||
|
||
namespace Bliss.CSharp.Rendering.Systems; | ||
|
||
public struct SimplePushConstantData { | ||
|
||
public Matrix4x4 ModelMatrix; | ||
public Matrix4x4 NormalMatrix; | ||
|
||
/// <summary> | ||
/// Represents a container class for simple push constant data used in rendering systems. | ||
/// </summary> | ||
public SimplePushConstantData() { | ||
this.ModelMatrix = Matrix4x4.Identity; | ||
this.NormalMatrix = Matrix4x4.Identity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using Bliss.CSharp.Rendering.Vulkan; | ||
using Silk.NET.Vulkan; | ||
|
||
namespace Bliss.CSharp.Rendering.Systems; | ||
|
||
public class SimpleRenderSystem : Disposable { | ||
|
||
public readonly Vk Vk; | ||
public readonly BlissDevice Device; | ||
|
||
private BlissPipeline _pipeline; | ||
private PipelineLayout _pipelineLayout; | ||
|
||
public SimpleRenderSystem(Vk vk, BlissDevice device, RenderPass renderPass, DescriptorSetLayout globalSetLayout) { | ||
this.Vk = vk; | ||
this.Device = device; | ||
|
||
this.CreatePipelineLayout(globalSetLayout); | ||
this.CreatePipeline(renderPass); | ||
} | ||
|
||
public unsafe void Render(FrameInfo frameInfo) { | ||
this._pipeline.Bind(frameInfo.CommandBuffer); | ||
this.Vk.CmdBindDescriptorSets(frameInfo.CommandBuffer, PipelineBindPoint.Graphics, this._pipelineLayout, 0, 1, frameInfo.GlobalDescriptorSet, 0, null); | ||
|
||
foreach (Renderable renderable in frameInfo.RenderableObjects) { | ||
SimplePushConstantData push = new() { | ||
ModelMatrix = renderable.Transform.GetMatrix(), | ||
NormalMatrix = renderable.Transform.GetNormalMatrix() | ||
}; | ||
|
||
this.Vk.CmdPushConstants(frameInfo.CommandBuffer, this._pipelineLayout, ShaderStageFlags.VertexBit | ShaderStageFlags.FragmentBit, 0, (uint) Marshal.SizeOf<SimplePushConstantData>(), ref push); | ||
|
||
renderable.Model.Bind(frameInfo.CommandBuffer); | ||
renderable.Model.Draw(frameInfo.CommandBuffer); | ||
} | ||
} | ||
|
||
private unsafe void CreatePipelineLayout(DescriptorSetLayout globalSetLayout) { | ||
DescriptorSetLayout[] descriptorSetLayouts = new DescriptorSetLayout[] { | ||
globalSetLayout | ||
}; | ||
|
||
PushConstantRange pushConstantRange = new() { | ||
StageFlags = ShaderStageFlags.VertexBit | ShaderStageFlags.FragmentBit, | ||
Offset = 0, | ||
Size = (uint) Marshal.SizeOf<SimplePushConstantData>() | ||
}; | ||
|
||
fixed (DescriptorSetLayout* descriptorSetLayoutPtr = descriptorSetLayouts) { | ||
PipelineLayoutCreateInfo pipelineLayoutInfo = new() { | ||
SType = StructureType.PipelineLayoutCreateInfo, | ||
SetLayoutCount = (uint) descriptorSetLayouts.Length, | ||
PSetLayouts = descriptorSetLayoutPtr, | ||
PushConstantRangeCount = 1, | ||
PPushConstantRanges = &pushConstantRange, | ||
}; | ||
|
||
if (this.Vk.CreatePipelineLayout(this.Device.GetVkDevice(), pipelineLayoutInfo, null, out this._pipelineLayout) != Result.Success) { | ||
throw new Exception("Failed to create pipeline layout!"); | ||
} | ||
} | ||
} | ||
|
||
private void CreatePipeline(RenderPass renderPass) { | ||
Debug.Assert(this._pipelineLayout.Handle != 0, "Cannot create pipeline before pipeline layout"); | ||
|
||
PipelineConfigInfo pipelineConfig = new(); | ||
BlissPipeline.GetDefaultPipelineConfigInfo(ref pipelineConfig); | ||
BlissPipeline.EnableMultiSampling(ref pipelineConfig, this.Device.MsaaSamples); | ||
|
||
pipelineConfig.RenderPass = renderPass; | ||
pipelineConfig.PipelineLayout = this._pipelineLayout; | ||
|
||
this._pipeline = new BlissPipeline(this.Vk, this.Device, "simpleShader.vert.spv", "simpleShader.frag.spv", pipelineConfig); | ||
} | ||
|
||
protected override unsafe void Dispose(bool disposing) { | ||
if (disposing) { | ||
this._pipeline.Dispose(); | ||
this.Vk.DestroyPipelineLayout(this.Device.GetVkDevice(), this._pipelineLayout, null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters