Skip to content

Commit

Permalink
Working on default shader.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrScautHD committed Aug 1, 2024
1 parent 227584e commit ed71bfc
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/Bliss.Test/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public void Run() {
Input.Init(this.Window);

this.Init();

uint frames = BlissSwapChain.MaxDefaultFramesInFlight;


Logger.Info("Start main Loops...");
this.Window.Run();
Expand Down
2 changes: 1 addition & 1 deletion src/Bliss/Bliss.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="content\" />
<Folder Include="CSharp\Audio\" />
<Folder Include="CSharp\Shaders\" />
</ItemGroup>

<!-- Libraries -->
<ItemGroup>
<PackageReference Include="GLSLangSharp" Version="0.4.15" />
<PackageReference Include="Silk.NET.Vulkan.Extensions.EXT" Version="2.21.0" />
<PackageReference Include="Silk.NET.Vulkan.Extensions.KHR" Version="2.21.0" />
<PackageReference Include="Silk.NET.Windowing" Version="2.21.0" />
Expand Down
6 changes: 0 additions & 6 deletions src/Bliss/CSharp/Bliss.cs

This file was deleted.

6 changes: 3 additions & 3 deletions src/Bliss/CSharp/Interact/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public static char[] GetPressedChars() {
/// <summary>
/// Retrieves a list of connected gamepads.
/// </summary>
/// <returns>A read-only list of IGamepad objects representing the connected gamepads.</returns
/// <returns>A read-only list of IGamepad objects representing the connected gamepads.</returns>
public static IReadOnlyList<IGamepad> GetGamepads() {
return _context.Gamepads;
}
Expand Down Expand Up @@ -542,7 +542,7 @@ public static IReadOnlyList<Axis> GetJoystickAxes(int joystick) {
/// Retrieves the currently pressed buttons of the specified joystick.
/// </summary>
/// <param name="joystick">The index of the joystick to get buttons from.</param>
/// <returns>The list of buttons currently pressed on the specified joystick.</returns
/// <returns>The list of buttons currently pressed on the specified joystick.</returns>
public static IReadOnlyList<Button> GetJoystickButtons(int joystick) {
return _context.Joysticks[joystick].Buttons;
}
Expand Down Expand Up @@ -597,7 +597,7 @@ public static bool IsJoystickButtonReleased(ButtonName button) {
/// <param name="button">The joystick button to check.</param>
/// <returns>
/// <c>true</c> if the joystick button is in the up state; otherwise, <c>false</c>.
/// </
/// </returns>
public static bool IsJoystickButtonUp(ButtonName button) {
return !_joystickButtonsDown.Contains(button);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bliss/CSharp/Rendering/Systems/SimpleRenderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void CreatePipeline(RenderPass renderPass) {
pipelineConfig.RenderPass = renderPass;
pipelineConfig.PipelineLayout = this._pipelineLayout;

this._pipeline = new BlissPipeline(this.Vk, this.Device, "simpleShader.vert.spv", "simpleShader.frag.spv", pipelineConfig);
this._pipeline = new BlissPipeline(this.Vk, this.Device, "content/shaders/default_shader.frag", "content/shaders/default_shader.vert", pipelineConfig);
}

protected override unsafe void Dispose(bool disposing) {
Expand Down
33 changes: 30 additions & 3 deletions src/Bliss/CSharp/Rendering/Vulkan/BlissPipeline.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Bliss.CSharp.Geometry;
using Bliss.CSharp.Logging;
using GLSLang;
using Microsoft.FSharp.Collections;
using Microsoft.FSharp.Core;
using Silk.NET.Core.Native;
using Silk.NET.Vulkan;
using BlendFactor = Silk.NET.Vulkan.BlendFactor;
using FrontFace = Silk.NET.Vulkan.FrontFace;
using Pipeline = Silk.NET.Vulkan.Pipeline;
using PrimitiveTopology = Silk.NET.Vulkan.PrimitiveTopology;

namespace Bliss.CSharp.Rendering.Vulkan;

Expand Down Expand Up @@ -136,8 +143,8 @@ public void Bind(CommandBuffer commandBuffer) {
/// <param name="fragPath">The path to the fragment shader file.</param>
/// <param name="configInfo">The pipeline configuration information.</param>
private unsafe void CreateGraphicsPipeline(string vertPath, string fragPath, PipelineConfigInfo configInfo) {
byte[] vertSource = this.GetShaderBytes(vertPath);
byte[] fragSource = this.GetShaderBytes(fragPath);
byte[] vertSource = this.GetShaderBytes(vertPath, ShaderStage.Vertex);
byte[] fragSource = this.GetShaderBytes(fragPath, ShaderStage.Fragment);

this._vertShaderModule = this.CreateShaderModule(vertSource);
this._fragShaderModule = this.CreateShaderModule(fragSource);
Expand Down Expand Up @@ -229,7 +236,7 @@ private unsafe void CreateGraphicsPipeline(string vertPath, string fragPath, Pip
/// </summary>
/// <param name="filename">The name of the shader file.</param>
/// <returns>The byte array representing the shader file.</returns>
private byte[] GetShaderBytes(string filename) {
private byte[] GetShaderBytesWithSpv(string filename) {
Assembly assembly = Assembly.GetExecutingAssembly();
string? resourceName = assembly.GetManifestResourceNames().FirstOrDefault(s => s.EndsWith(filename));

Expand All @@ -244,6 +251,26 @@ private byte[] GetShaderBytes(string filename) {
return ms.ToArray();
}

// TODO: Use the Nuget packet (GLSLLangSharp, or Spv-V.... to compile the shaders from the .vert, .frag to .spv format). (Should work fine need to be tested!)
public byte[] GetShaderBytes(string filename, ShaderStage shaderStage, List<string>? defines = default) {
Assembly assembly = Assembly.GetExecutingAssembly();
string? resourceName = assembly.GetManifestResourceNames().FirstOrDefault(s => s.EndsWith(filename));

if (resourceName == null) {
throw new ApplicationException($"No shader file found with name {filename}");
}

var result = GLSLang.GLSLang.tryCompile(shaderStage, "main", defines != null ? ListModule.OfSeq(defines) : FSharpList<string>.Empty, resourceName);

if (FSharpOption<byte[]>.get_IsSome(result.Item1)) {
Logger.Info($"Shader at [{filename}] complied successfully!");
return result.Item1.Value;
}
else {
throw new ApplicationException($"Shader at [{filename}] failed at compilation!");
}
}

/// <summary>
/// Creates a shader module.
/// </summary>
Expand Down
66 changes: 66 additions & 0 deletions src/Bliss/content/shaders/default_shader.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#version 450

layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec3 fragPosWorld;
layout(location = 2) in vec3 fragNormalWorld;

layout(location = 0) out vec4 outColor;


struct PointLight {
vec4 position; // ignore w
vec4 color; // w is intensity
};

layout(set = 0, binding = 0) uniform GlobalUbo
{
mat4 projection;
mat4 view;
vec4 front;
vec4 ambientColor;
int numLights;
int padding1;
int padding2;
int padding3;
PointLight pointLights[10];
} ubo;

layout(push_constant) uniform Push
{
mat4 modelMatrix;
mat4 normalMatrix;
} push;



void main() {
vec3 diffuseLight = ubo.ambientColor.xyz * ubo.ambientColor.w;
vec3 specularLight = vec3(0.0);
vec3 surfaceNormal = normalize(fragNormalWorld);

// already had front vec in camera
vec3 viewDirection = -ubo.front.xyz;

for (int i = 0; i < ubo.numLights; i++)
{

PointLight light = ubo.pointLights[i];
vec3 directionToLight = light.position.xyz - fragPosWorld;
float attenuation = 1.0 / dot(directionToLight, directionToLight); // distance squared
directionToLight = normalize(directionToLight);

float cosAngIncidence = max(dot(surfaceNormal, directionToLight), 0);
vec3 intensity = light.color.xyz * light.color.w * attenuation;

diffuseLight += intensity * cosAngIncidence;

// specular lighting
vec3 halfAngle = normalize(directionToLight + viewDirection);
float blinnTerm = dot(surfaceNormal, halfAngle);
blinnTerm = clamp(blinnTerm, 0, 1);
blinnTerm = pow(blinnTerm, 64.0);
specularLight += intensity * blinnTerm;
}

outColor = vec4(diffuseLight * fragColor + specularLight * fragColor, 1.0);
}
47 changes: 47 additions & 0 deletions src/Bliss/content/shaders/default_shader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#version 450

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
layout(location = 2) in vec3 normal;
layout(location = 3) in vec2 uv;

layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec3 fragPosWorld;
layout(location = 2) out vec3 fragNormalWorld;

struct PointLight {
vec4 position; // ignore w
vec4 color; // w is intensity
};

layout(set = 0, binding = 0) uniform GlobalUbo
{
mat4 projection;
mat4 view;
vec4 front;
vec4 ambientColor;
int numLights;
int padding1;
int padding2;
int padding3;
PointLight pointLights[10];
} ubo;

layout(push_constant) uniform Push
{
mat4 modelMatrix;
mat4 normalMatrix;
} push;


void main() {
vec4 positionWorld = push.modelMatrix * vec4(position, 1.0);
gl_Position = ubo.projection * ubo.view * positionWorld;

fragNormalWorld = normalize(mat3(push.normalMatrix) * normal);
fragPosWorld = positionWorld.xyz;
fragColor = color;
}


//#extension GL_KHR_vulkan_glsl:enable

0 comments on commit ed71bfc

Please sign in to comment.