Skip to content

Commit

Permalink
* add smooth resize
Browse files Browse the repository at this point in the history
* rename Transformations.glsl to Math.glsl
* make binding=0 the default temporary UBO
* small things
  • Loading branch information
BoyBaykiller committed Jan 7, 2025
1 parent 4de5a7e commit caa93b7
Show file tree
Hide file tree
Showing 65 changed files with 320 additions and 206 deletions.
2 changes: 1 addition & 1 deletion BBG/Source/Cmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static partial class BBG
public static unsafe class Cmd
{
// Application specific
public const int SET_UNIFORMS_UBO_BLOCK_BINDING = 7;
public const int SET_UNIFORMS_UBO_BLOCK_BINDING = 0;

[Flags]
public enum MemoryBarrierMask : uint
Expand Down
18 changes: 8 additions & 10 deletions BBG/Source/Objects/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ public enum MemAccess : uint

/// <summary>
/// Same as <see cref="MappedIncoherent"/> except that it's write-only AND leverages ReBAR/SAM on AMD drivers. <br/>
/// https://gist.github.com/BoyBaykiller/6334c26912e6d2cf0da5c7a76d341a41 <br/>
/// <![CDATA[https://vanguard.amd.com/project/feedback/view.html?cap=00de81e7400a4f968b2b7c7ed88e35b8&f={662A7A4D-8782-46AA-BB66-31734E56D968}&uf={01316738-8958-4B49-8CFE-1B68A831626C}]]>
/// </summary>
MappedIncoherentWriteOnlyReBAR = BufferStorageMask.MapPersistentBit | BufferStorageMask.MapWriteBit,
MappedCoherentWriteOnlyReBAR = BufferStorageMask.MapPersistentBit | BufferStorageMask.MapCoherentBit | BufferStorageMask.MapWriteBit,

/// <summary>
/// The buffer must be written or read by to using the Upload/Download functions.
Expand Down Expand Up @@ -116,7 +114,7 @@ public void Allocate(MemLocation memLocation, MemAccess memAccess, nint size, vo
memLocation == MemLocation.DeviceLocal &&
memAccess != MemAccess.MappedCoherent &&
memAccess != MemAccess.MappedIncoherent &&
memAccess != MemAccess.MappedIncoherentWriteOnlyReBAR &&
memAccess != MemAccess.MappedCoherentWriteOnlyReBAR &&
data != null;

if (!useFastUploadPathAMD)
Expand All @@ -137,7 +135,7 @@ public void Allocate(MemLocation memLocation, MemAccess memAccess, nint size, vo
{
Memory = GL.MapNamedBufferRange(ID, 0, size, (MapBufferAccessMask)memAccess);
}
if (memAccess == MemAccess.MappedIncoherent || memAccess == MemAccess.MappedIncoherentWriteOnlyReBAR)
if (memAccess == MemAccess.MappedIncoherent || memAccess == MemAccess.MappedCoherentWriteOnlyReBAR)
{
Memory = GL.MapNamedBufferRange(ID, 0, size, (MapBufferAccessMask)memAccess | MapBufferAccessMask.MapFlushExplicitBit);
}
Expand Down Expand Up @@ -170,23 +168,23 @@ public void CopyTo(Buffer buffer, nint readOffset, nint writeOffset, nint size)
GL.CopyNamedBufferSubData(ID, buffer.ID, readOffset, writeOffset, size);
}

public void Clear(nint offset, nint size, in uint data)
public void Fill(nint offset, nint size, in uint data)
{
fixed (void* ptr = &data)
{
Clear(Texture.InternalFormat.R32Uint, Texture.PixelFormat.RInteger, Texture.PixelType.Int, offset, size, ptr);
Fill(Texture.InternalFormat.R32Uint, Texture.PixelFormat.RInteger, Texture.PixelType.Int, offset, size, ptr);
}
}

public void Clear(nint offset, nint size, in float data)
public void Fill(nint offset, nint size, in float data)
{
fixed (void* ptr = &data)
{
Clear(Texture.InternalFormat.R32Float, Texture.PixelFormat.R, Texture.PixelType.Float, offset, size, ptr);
Fill(Texture.InternalFormat.R32Float, Texture.PixelFormat.R, Texture.PixelType.Float, offset, size, ptr);
}
}

public void Clear(Texture.InternalFormat internalFormat, Texture.PixelFormat pixelFormat, Texture.PixelType pixelType, nint offset, nint size, void* data)
public void Fill(Texture.InternalFormat internalFormat, Texture.PixelFormat pixelFormat, Texture.PixelType pixelType, nint offset, nint size, void* data)
{
if (size == 0) return;
GL.ClearNamedBufferSubData(ID, (SizedInternalFormat)internalFormat, offset, size, (PixelFormat)pixelFormat, (PixelType)pixelType, data);
Expand Down
60 changes: 34 additions & 26 deletions BBG/Source/Objects/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ private AbstractShader(ShaderStage shaderStage, string source, string localShade

public static class Preprocessor
{
public static readonly bool SUPPORTS_LINE_SOURCEFILE = GetGLSLLineSourcefileSupport(out GLSL_EXTENSION_NAME_LINE_SOURCEFILE);
private static readonly string? GLSL_EXTENSION_NAME_LINE_SOURCEFILE; // The required GLSL-extension that enables #line "filename" or null if none
public static readonly bool SUPPORTS_LINE_DIRECTIVE_SOURCEFILE = GetGLSLLineSourcefileSupport(out EXTENSION_NAME_LINE_DIRECTIVE_SOURCEFILE);
private static readonly string? EXTENSION_NAME_LINE_DIRECTIVE_SOURCEFILE; // The required GLSL-extension that enables #line "filename" or null if none

public enum Keyword : int
{
Expand Down Expand Up @@ -130,8 +130,8 @@ public static string PreProcess(string source, IReadOnlyDictionary<string, strin
int versionStatementLineCount = CountLines(result, afterVersionStatement);
result = result.Insert(afterVersionStatement,
$"""
#if {(GLSL_EXTENSION_NAME_LINE_SOURCEFILE != null ? 1 : 0)}
#extension {GLSL_EXTENSION_NAME_LINE_SOURCEFILE} : enable
#if {(EXTENSION_NAME_LINE_DIRECTIVE_SOURCEFILE != null ? 1 : 0)}
#extension {EXTENSION_NAME_LINE_DIRECTIVE_SOURCEFILE} : enable
#endif
// Keep in sync between shader and client code!
Expand Down Expand Up @@ -191,34 +191,33 @@ StringBuilder RecursiveResolveKeywords(string source, string name = null)
if (pathsAlreadyIncluded.Contains(path))
{
includedText = $"// Omitted including \"{path}\" as it's already part of this file";
result.AppendLine(includedText);
}
else
{
includedText = File.ReadAllText(path);
pathsAlreadyIncluded.Add(path);
}

int lineCount = CountLines(source, currentIndex);

string newLine = "#line 1";
if (SUPPORTS_LINE_SOURCEFILE)
{
newLine += $" \"{path}\"";
}
newLine += $" // Including \"{path}\"";
result.AppendLine(newLine);

result.Append(RecursiveResolveKeywords(includedText, path));
result.Append('\n');

string origionalLine = $"#line {lineCount + 1}";
string safeSourceName = name ?? "No source name given";
if (SUPPORTS_LINE_SOURCEFILE)
{
origionalLine += $" \"{safeSourceName}\"";
string lineDirective = "#line 1";
if (SUPPORTS_LINE_DIRECTIVE_SOURCEFILE)
{
lineDirective += $" \"{path}\"";
}
lineDirective += $" // Including \"{path}\"";
result.AppendLine(lineDirective);

result.Append(RecursiveResolveKeywords(includedText, path));
result.Append('\n');

string origionalLine = $"#line {CountLines(source, currentIndex) + 1}";
string safeSourceName = name ?? "No source name given";
if (SUPPORTS_LINE_DIRECTIVE_SOURCEFILE)
{
origionalLine += $" \"{safeSourceName}\"";
}
origionalLine += $" // Included \"{path}\"";
result.AppendLine(origionalLine);
}
origionalLine += $" // Included \"{path}\"";
result.AppendLine(origionalLine);
}
}

Expand Down Expand Up @@ -251,7 +250,7 @@ private static StringBuilder RemoveUnusedShaderStorageBlocks(string text)
{
Group shaderStorageBlock = match.Groups[0];
Group instanceName = match.Groups[1];

bool instanceNameReferenced = new Regex($@"\b{instanceName.Value}\.").Match(text, currentIndex).Success;
int end = shaderStorageBlock.Index;
if (instanceNameReferenced)
Expand All @@ -270,6 +269,15 @@ private static StringBuilder RemoveUnusedShaderStorageBlocks(string text)
}
}

if (numReferencedDeclarations >= 16)
{
Logger.Log(Logger.LogLevel.Warn, """
The number of shader storage blocks referenced by the shader exceeds the limit
in current NVIDIA drivers: https://forums.developer.nvidia.com/t/increase-maximum-allowed-shader-storage-blocks/293755/1
This shader will fail to compile on NVIDIA GPUs!
""");
}

return result;
}

Expand Down
6 changes: 3 additions & 3 deletions BBG/Source/Objects/ShaderProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public bool GetLinkStatus()
return success == 1;
}

public void Upload(int location, in Matrix4 Matrix4, int count = 1, bool transpose = false)
public void Upload(int location, in Matrix4 matrix4, int count = 1, bool transpose = false)
{
fixed (Matrix4* ptr = &Matrix4)
fixed (Matrix4* ptr = &matrix4)
{
GL.ProgramUniformMatrix4fv(ID, location, count, transpose, (float*)ptr);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ public static void SetShaderInsertionValue(string key, string value)
if (recompiledShadersNames != string.Empty)
{
Logger.Log(Logger.LogLevel.Info,
$"{nameof(AbstractShader.Preprocessor.Keyword.AppInclude)} \"{key}\" was assigned new value \"{value}\", " +
$"{nameof(AbstractShader.Preprocessor.Keyword.AppInsert)} \"{key}\" was assigned new value \"{value}\", " +
$"causing shader recompilation for {recompiledShadersNames}"
);
}
Expand Down
6 changes: 3 additions & 3 deletions BBG/Source/Objects/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ public void Download(PixelFormat pixelFormat, PixelType pixelType, void* pixels,
GL.GetTextureImage(ID, level, (OpenTK.Graphics.OpenGL.PixelFormat)pixelFormat, (OpenTK.Graphics.OpenGL.PixelType)pixelType, bufSize, pixels);
}

public void Clear<T>(PixelFormat pixelFormat, PixelType pixelType, in T value, int level = 0) where T : unmanaged
public void Fill<T>(PixelFormat pixelFormat, PixelType pixelType, in T value, int level = 0) where T : unmanaged
{
fixed (void* ptr = &value)
{
Clear(pixelFormat, pixelType, ptr, level);
Fill(pixelFormat, pixelType, ptr, level);
}
}
public void Clear(PixelFormat pixelFormat, PixelType pixelType, void* data, int level = 0)
public void Fill(PixelFormat pixelFormat, PixelType pixelType, void* data, int level = 0)
{
GL.ClearTexImage(ID, level, (OpenTK.Graphics.OpenGL.PixelFormat)pixelFormat, (OpenTK.Graphics.OpenGL.PixelType)pixelType, data);
}
Expand Down
4 changes: 2 additions & 2 deletions BBG/Source/Objects/TimerQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static partial class BBG
{
public class TimerQuery : IDisposable
{
public float MeasuredMilliseconds { get; private set; }
public float ElapsedMilliseconds { get; private set; }
public readonly int ID;
public TimerQuery()
{
Expand All @@ -31,7 +31,7 @@ public void End()
GL.EndQuery(QueryTarget.TimeElapsed);

GL.GetQueryObjecti64(ID, QueryObjectParameterName.QueryResult, out long resultNanoSec);
MeasuredMilliseconds = resultNanoSec / 1000000.0f;
ElapsedMilliseconds = resultNanoSec / 1000000.0f;
}

public void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion IDKEngine/IDKEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<TieredCompilationQuickJit>false</TieredCompilationQuickJit> <!-- True by default-->

<!--<OptimizationPreference>Speed</OptimizationPreference>-->
<!--<IlcInstructionSet>native</IlcInstructionSet>-->
<!--<IlcInstructionSet>avx2</IlcInstructionSet>-->
<!--<IlcDisableReflection>true</IlcDisableReflection>-->

</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions IDKEngine/Resource/Shaders/AtmosphericScattering/compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#version 460 core

AppInclude(include/Constants.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)

layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

Expand All @@ -13,7 +13,7 @@ layout(binding = 0) restrict writeonly uniform imageCube ImgResult;
vec2 Rsi(vec3 r0, vec3 rd, float sr);
vec3 Atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g);

layout(std140, binding = 7) uniform SettingsUBO
layout(std140, binding = 0) uniform SettingsUBO
{
int ISteps;
int JSteps;
Expand Down
2 changes: 1 addition & 1 deletion IDKEngine/Resource/Shaders/Bloom/compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ layout(binding = 0) restrict writeonly uniform image2D ImgResult;
layout(binding = 0) uniform sampler2D SamplerDownsample;
layout(binding = 1) uniform sampler2D SamplerUpsample;

layout(std140, binding = 7) uniform SettingsUBO
layout(std140, binding = 0) uniform SettingsUBO
{
float Threshold;
float MaxColor;
Expand Down
2 changes: 1 addition & 1 deletion IDKEngine/Resource/Shaders/DeferredLighting/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AppInclude(include/StaticStorageBuffers.glsl)

AppInclude(include/Pbr.glsl)
AppInclude(include/Compression.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)
AppInclude(include/StaticUniformBuffers.glsl)

layout(location = 0) out vec4 OutFragColor;
Expand Down
2 changes: 1 addition & 1 deletion IDKEngine/Resource/Shaders/GBuffer/MeshPath/mesh.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

AppInclude(include/Constants.glsl)
AppInclude(include/Compression.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)
AppInclude(include/StaticUniformBuffers.glsl)
AppInclude(include/StaticStorageBuffers.glsl)

Expand Down
2 changes: 1 addition & 1 deletion IDKEngine/Resource/Shaders/GBuffer/VertexPath/vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#version 460 core

AppInclude(include/Compression.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)
AppInclude(include/StaticStorageBuffers.glsl)
AppInclude(include/StaticUniformBuffers.glsl)

Expand Down
2 changes: 1 addition & 1 deletion IDKEngine/Resource/Shaders/GBuffer/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
AppInclude(include/Surface.glsl)

AppInclude(include/Compression.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)
AppInclude(include/StaticStorageBuffers.glsl)
AppInclude(include/StaticUniformBuffers.glsl)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct HitInfo
uint InstanceID;
};

layout(std140, binding = 7) uniform SettingsUBO
layout(std140, binding = 0) uniform SettingsUBO
{
float FocalLength;
float LenseRadius;
Expand Down
4 changes: 2 additions & 2 deletions IDKEngine/Resource/Shaders/PathTracing/FirstHit/compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
AppInclude(include/Ray.glsl)
AppInclude(include/Sampling.glsl)
AppInclude(include/Compression.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)
AppInclude(include/StaticStorageBuffers.glsl)
AppInclude(include/StaticUniformBuffers.glsl)
AppInclude(PathTracing/include/Constants.glsl)
Expand All @@ -23,7 +23,7 @@ layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z =

layout(binding = 0) restrict readonly writeonly uniform image2D ImgResult;

layout(std140, binding = 7) uniform SettingsUBO
layout(std140, binding = 0) uniform SettingsUBO
{
float FocalLength;
float LenseRadius;
Expand Down
4 changes: 2 additions & 2 deletions IDKEngine/Resource/Shaders/PathTracing/NHit/compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
AppInclude(include/Ray.glsl)
AppInclude(include/Sampling.glsl)
AppInclude(include/Compression.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)
AppInclude(include/StaticStorageBuffers.glsl)
AppInclude(include/StaticUniformBuffers.glsl)
AppInclude(PathTracing/include/Constants.glsl)
Expand All @@ -19,7 +19,7 @@ AppInclude(include/BVHIntersect.glsl)

layout(local_size_x = N_HIT_PROGRAM_LOCAL_SIZE_X, local_size_y = 1, local_size_z = 1) in;

layout(std140, binding = 7) uniform SettingsUBO
layout(std140, binding = 0) uniform SettingsUBO
{
float FocalLength;
float LenseRadius;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AppInclude(include/Pbr.glsl)
AppInclude(include/Random.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)
AppInclude(PathTracing/include/Bsdf.glsl)

#define ENUM_BSDF uint
Expand Down
4 changes: 2 additions & 2 deletions IDKEngine/Resource/Shaders/SSAO/compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

AppInclude(include/Sampling.glsl)
AppInclude(include/Compression.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)
AppInclude(include/StaticUniformBuffers.glsl)

layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

layout(binding = 0) restrict writeonly uniform image2D ImgResult;

layout(std140, binding = 7) uniform SettingsUBO
layout(std140, binding = 0) uniform SettingsUBO
{
int SampleCount;
float Radius;
Expand Down
4 changes: 2 additions & 2 deletions IDKEngine/Resource/Shaders/SSR/compute.glsl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#version 460 core

AppInclude(include/Compression.glsl)
AppInclude(include/Transformations.glsl)
AppInclude(include/Math.glsl)
AppInclude(include/StaticUniformBuffers.glsl)

layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

layout(binding = 0) restrict writeonly uniform image2D ImgResult;
layout(binding = 0) uniform sampler2D SamplerSrc;

layout(std140, binding = 7) uniform SettingsUBO
layout(std140, binding = 0) uniform SettingsUBO
{
int SampleCount;
int BinarySearchCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ layout(binding = 0) restrict writeonly uniform uimage2D ImgResult;
layout(binding = 1) restrict writeonly uniform image2D ImgDebug;
layout(binding = 0) uniform sampler2D SamplerShaded;

layout(std140, binding = 7) uniform SettingsUBO
layout(std140, binding = 0) uniform SettingsUBO
{
ENUM_DEBUG_MODE DebugMode;
float SpeedFactor;
Expand Down
Loading

0 comments on commit caa93b7

Please sign in to comment.