Skip to content

Commit

Permalink
For some reason it do not work with Raylib :/
Browse files Browse the repository at this point in the history
  • Loading branch information
MrScautHD committed Jun 2, 2024
1 parent b75efbb commit 2dcc8b1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
63 changes: 55 additions & 8 deletions src/Sparkle/CSharp/Effects/Types/PbrEffect.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using OpenTK.Graphics.OpenGL;
using Raylib_CSharp;
using Raylib_CSharp.Colors;
using Raylib_CSharp.Images;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Rendering.Gl;
using Raylib_CSharp.Shaders;
using Sparkle.CSharp.Logging;
using Sparkle.CSharp.Scenes;
using PixelFormat = Raylib_CSharp.Images.PixelFormat;

namespace Sparkle.CSharp.Effects.Types;

Expand Down Expand Up @@ -174,18 +177,22 @@ public class PbrEffect : Effect {

private void LoadBuffer() {
if (this.GlVersion == GlVersion.OpenGl33) {
//this._lightBuffer = (int) RlGl.LoadTexture(nint.Zero, 0, 0, PixelFormat.CompressedDxt1Rgba, 0);
GL.GenBuffers(1, ref this._lightBuffer);
}
else {
this._lightBuffer = (int) RlGl.LoadShaderBuffer(0, nint.Zero, RlGl.DynamicCopy);
//GL.GenBuffers(1, ref this._lightBuffer);
this._lightBuffer = (int) RlGl.LoadShaderBuffer((uint) (1 * Marshal.SizeOf(typeof(LightData))), nint.Zero, RlGl.DynamicCopy);
}
}

public void UpdateBuffer() {
public unsafe void UpdateBuffer() {
LightData[] lightData = this._lights.Values.ToArray();

if (this.GlVersion == GlVersion.OpenGl33) {

//RlGl.UpdateTexture();

// TODO: Replace this System with Textures and remove "OpenTK".
GL.UseProgram((int) this.Shader.Id);
GL.BindBuffer(BufferTarget.UniformBuffer, this._lightBuffer);
Expand All @@ -198,13 +205,51 @@ public class PbrEffect : Effect {
GL.BindBuffer(BufferTarget.UniformBuffer, 0);
}
else {
GCHandle pinnedArray = GCHandle.Alloc(lightData, GCHandleType.Pinned);
RlGl.UpdateShaderBuffer((uint) this._lightBuffer, pinnedArray.AddrOfPinnedObject() , (uint) (lightData.Length * Marshal.SizeOf(typeof(LightData))), 0);
pinnedArray.Free();
//GL.UseProgram((int) this.Shader.Id);
//GL.BindBufferBase(BufferTarget.ShaderStorageBuffer, 0, this._lightBuffer);
//
//GL.BufferData(BufferTarget.ShaderStorageBuffer, lightData.Length * Marshal.SizeOf(typeof(LightData)), nint.Zero, BufferUsage.DynamicCopy);
//GL.BufferSubData(BufferTarget.ShaderStorageBuffer, 0, lightData);



//GL.BindBufferBase(BufferTarget.ShaderStorageBuffer, 0, this._lightBuffer);




//GL.UseProgram((int) this.Shader.Id);
//GL.BindBufferBase(BufferTarget.ShaderStorageBuffer, 0, this._lightBuffer);
//
//GL.BindBuffer(BufferTarget.ShaderStorageBuffer, this._lightBuffer); //GL.BufferData(BufferTarget.ShaderStorageBuffer, lightData.Length * Marshal.SizeOf(typeof(LightData)), nint.Zero, BufferUsage.DynamicCopy);
//fixed (void* dataPtr = lightData) {
// GL.BufferSubData(BufferTarget.ShaderStorageBuffer, 0, lightData.Length * Marshal.SizeOf(typeof(LightData)), dataPtr);
//}
//GL.UseProgram(0);

RlGl.EnableShader(this.Shader.Id);
RlGl.BindShaderBuffer(this.Shader.Id, 0);
RlGl.DisableShader();



//RlGl.EnableShader(this.Shader.Id);
//RlGl.BindShaderBuffer(this.Shader.Id, 0);
//
//GCHandle pinnedArray = GCHandle.Alloc(lightData, GCHandleType.Pinned);
//RlGl.UpdateShaderBuffer((uint) this._lightBuffer, pinnedArray.AddrOfPinnedObject(), (uint) (lightData.Length * Marshal.SizeOf(typeof(LightData))), 0);
//pinnedArray.Free();
//
//RlGl.DisableShader();




//RlGl.EnableShader(this.Shader.Id);
//RlGl.BindShaderBuffer(this.Shader.Id, 0);
//
//fixed (void* dataPtr = lightData) {
// RlGl.UpdateShaderBuffer((uint) this._lightBuffer, (nint) dataPtr, (uint) (lightData.Length * Marshal.SizeOf(typeof(LightData))), 0);
//}
//
//RlGl.DisableShader();
}
}

Expand All @@ -213,6 +258,8 @@ public class PbrEffect : Effect {
GL.DeleteBuffers(1, this._lightBuffer);
}
else {
//GL.DeleteBuffers(1, this._lightBuffer);

RlGl.UnloadShaderBuffer((uint) this._lightBuffer);
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/Sparkle/content/shaders/glsl330/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ vec4 ComputePBR() {
break;
}

if (light.enabled == 0) {
return vec4(radiance, 1.0);;
}

// Cook-Torrance BRDF distribution function
float nDotV = max(dot(N, V), 0.0000001);
float nDotL = max(dot(N, L), 0.0000001);
Expand Down
6 changes: 3 additions & 3 deletions src/Sparkle/content/shaders/glsl430/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ vec4 ComputePBR() {
break;
}

if (light.enabled == 0) {
return vec4(radiance, 1.0);;
}
// if (light.enabled == 0) {
// return vec4(radiance, 1.0);
// }

// Cook-Torrance BRDF distribution function
float nDotV = max(dot(N, V), 0.0000001);
Expand Down

0 comments on commit 2dcc8b1

Please sign in to comment.