From caa93b71bf8df36fa215567f77415635bd9bd4c7 Mon Sep 17 00:00:00 2001 From: BoyBaykiller Date: Tue, 7 Jan 2025 04:52:10 +0100 Subject: [PATCH] * add smooth resize * rename Transformations.glsl to Math.glsl * make binding=0 the default temporary UBO * small things --- BBG/Source/Cmd.cs | 2 +- BBG/Source/Objects/Buffer.cs | 18 +++-- BBG/Source/Objects/Shader.cs | 60 +++++++++------- BBG/Source/Objects/ShaderProgram.cs | 6 +- BBG/Source/Objects/Texture.cs | 6 +- BBG/Source/Objects/TimerQuery.cs | 4 +- IDKEngine/IDKEngine.csproj | 2 +- .../AtmosphericScattering/compute.glsl | 4 +- IDKEngine/Resource/Shaders/Bloom/compute.glsl | 2 +- .../Shaders/DeferredLighting/fragment.glsl | 2 +- .../Shaders/GBuffer/MeshPath/mesh.glsl | 2 +- .../Shaders/GBuffer/VertexPath/vertex.glsl | 2 +- .../Resource/Shaders/GBuffer/fragment.glsl | 2 +- .../PathTracing/FinalDraw/compute.glsl | 2 +- .../Shaders/PathTracing/FirstHit/compute.glsl | 4 +- .../Shaders/PathTracing/NHit/compute.glsl | 4 +- .../Shaders/PathTracing/include/Shading.glsl | 2 +- IDKEngine/Resource/Shaders/SSAO/compute.glsl | 4 +- IDKEngine/Resource/Shaders/SSR/compute.glsl | 4 +- .../ShadingRateClassification/compute.glsl | 2 +- .../debugCompute.glsl | 2 +- .../Shadows/PointShadow/MeshPath/mesh.glsl | 2 +- .../PointShadow/VertexPath/vertex.glsl | 10 +-- .../Shaders/ShadowsRayTraced/compute.glsl | 2 +- .../Resource/Shaders/TAAResolve/compute.glsl | 2 +- .../TonemapAndGammaCorrect/compute.glsl | 2 +- .../UnprojectEquirectangular/compute.glsl | 2 +- .../Shaders/VXGI/ConeTracing/compute.glsl | 4 +- .../Voxelize/DebugVisualization/compute.glsl | 2 +- .../VXGI/Voxelize/Voxelize/fragment.glsl | 2 +- .../VXGI/Voxelize/Voxelize/vertex.glsl | 2 +- .../VolumetricLight/Upscale/compute.glsl | 2 +- .../Shaders/VolumetricLight/compute.glsl | 4 +- .../{Transformations.glsl => Math.glsl} | 33 +++++++++ .../Resource/Shaders/include/Random.glsl | 2 +- .../Resource/Shaders/include/Sampling.glsl | 2 +- .../Shaders/include/StaticUniformBuffers.glsl | 16 +++-- .../Resource/Shaders/include/TraceCone.glsl | 2 +- IDKEngine/Resource/imgui.ini | 2 +- IDKEngine/Source/Application.cs | 38 +++++----- IDKEngine/Source/Bvh/BLAS.cs | 17 ++--- IDKEngine/Source/Bvh/BVH.cs | 14 ++-- IDKEngine/Source/Bvh/TLAS.cs | 13 ++-- IDKEngine/Source/ModelManager.cs | 4 +- .../Source/Render/AtmosphericScatterer.cs | 3 +- IDKEngine/Source/Render/Bloom.cs | 10 +-- IDKEngine/Source/Render/CpuPointShadow.cs | 2 +- IDKEngine/Source/Render/Gui.cs | 5 +- IDKEngine/Source/Render/LightManager.cs | 7 +- .../Render/LightingShadingRateClassifier.cs | 5 +- IDKEngine/Source/Render/PathTracer.cs | 19 ++--- IDKEngine/Source/Render/PointShadowManager.cs | 10 ++- IDKEngine/Source/Render/RasterPipeline.cs | 16 ++--- IDKEngine/Source/Render/SSAO.cs | 3 +- IDKEngine/Source/Render/SSR.cs | 5 +- IDKEngine/Source/Render/SkyBoxManager.cs | 12 +++- IDKEngine/Source/Render/TAAResolve.cs | 3 +- .../Source/Render/TonemapAndGammaCorrecter.cs | 3 +- .../Render/VXGI/ConeTracing/ConeTracer.cs | 3 +- .../Source/Render/VXGI/Voxelizer/Voxelizer.cs | 12 ++-- IDKEngine/Source/Render/VolumetricLighting.cs | 6 +- IDKEngine/Source/Utils/ModelLoader.cs | 2 +- IDKEngine/Source/Utils/MyMath.cs | 69 ++++++++++++++----- IDKEngine/Source/Utils/RNG.cs | 6 +- Readme.md | 10 +-- 65 files changed, 320 insertions(+), 206 deletions(-) rename IDKEngine/Resource/Shaders/include/{Transformations.glsl => Math.glsl} (82%) diff --git a/BBG/Source/Cmd.cs b/BBG/Source/Cmd.cs index 24b0507e..574b26ed 100644 --- a/BBG/Source/Cmd.cs +++ b/BBG/Source/Cmd.cs @@ -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 diff --git a/BBG/Source/Objects/Buffer.cs b/BBG/Source/Objects/Buffer.cs index e1a43173..d05e079d 100644 --- a/BBG/Source/Objects/Buffer.cs +++ b/BBG/Source/Objects/Buffer.cs @@ -52,10 +52,8 @@ public enum MemAccess : uint /// /// Same as except that it's write-only AND leverages ReBAR/SAM on AMD drivers.
- /// https://gist.github.com/BoyBaykiller/6334c26912e6d2cf0da5c7a76d341a41
- /// ///
- MappedIncoherentWriteOnlyReBAR = BufferStorageMask.MapPersistentBit | BufferStorageMask.MapWriteBit, + MappedCoherentWriteOnlyReBAR = BufferStorageMask.MapPersistentBit | BufferStorageMask.MapCoherentBit | BufferStorageMask.MapWriteBit, /// /// The buffer must be written or read by to using the Upload/Download functions. @@ -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) @@ -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); } @@ -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); diff --git a/BBG/Source/Objects/Shader.cs b/BBG/Source/Objects/Shader.cs index bffbb11d..bbd807e6 100644 --- a/BBG/Source/Objects/Shader.cs +++ b/BBG/Source/Objects/Shader.cs @@ -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 { @@ -130,8 +130,8 @@ public static string PreProcess(string source, IReadOnlyDictionary= 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; } diff --git a/BBG/Source/Objects/ShaderProgram.cs b/BBG/Source/Objects/ShaderProgram.cs index 5b1c95f0..51673dd2 100644 --- a/BBG/Source/Objects/ShaderProgram.cs +++ b/BBG/Source/Objects/ShaderProgram.cs @@ -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); } @@ -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}" ); } diff --git a/BBG/Source/Objects/Texture.cs b/BBG/Source/Objects/Texture.cs index 3e302304..049b1df4 100644 --- a/BBG/Source/Objects/Texture.cs +++ b/BBG/Source/Objects/Texture.cs @@ -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(PixelFormat pixelFormat, PixelType pixelType, in T value, int level = 0) where T : unmanaged + public void Fill(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); } diff --git a/BBG/Source/Objects/TimerQuery.cs b/BBG/Source/Objects/TimerQuery.cs index 9aab464b..adb97674 100644 --- a/BBG/Source/Objects/TimerQuery.cs +++ b/BBG/Source/Objects/TimerQuery.cs @@ -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() { @@ -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() diff --git a/IDKEngine/IDKEngine.csproj b/IDKEngine/IDKEngine.csproj index 265d88c9..e222c353 100644 --- a/IDKEngine/IDKEngine.csproj +++ b/IDKEngine/IDKEngine.csproj @@ -21,7 +21,7 @@ false - + diff --git a/IDKEngine/Resource/Shaders/AtmosphericScattering/compute.glsl b/IDKEngine/Resource/Shaders/AtmosphericScattering/compute.glsl index ed048457..7cf13093 100644 --- a/IDKEngine/Resource/Shaders/AtmosphericScattering/compute.glsl +++ b/IDKEngine/Resource/Shaders/AtmosphericScattering/compute.glsl @@ -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; @@ -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; diff --git a/IDKEngine/Resource/Shaders/Bloom/compute.glsl b/IDKEngine/Resource/Shaders/Bloom/compute.glsl index c91fe861..f937fe95 100644 --- a/IDKEngine/Resource/Shaders/Bloom/compute.glsl +++ b/IDKEngine/Resource/Shaders/Bloom/compute.glsl @@ -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; diff --git a/IDKEngine/Resource/Shaders/DeferredLighting/fragment.glsl b/IDKEngine/Resource/Shaders/DeferredLighting/fragment.glsl index 198108c6..0906390e 100644 --- a/IDKEngine/Resource/Shaders/DeferredLighting/fragment.glsl +++ b/IDKEngine/Resource/Shaders/DeferredLighting/fragment.glsl @@ -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; diff --git a/IDKEngine/Resource/Shaders/GBuffer/MeshPath/mesh.glsl b/IDKEngine/Resource/Shaders/GBuffer/MeshPath/mesh.glsl index dbfefe3b..c184e730 100644 --- a/IDKEngine/Resource/Shaders/GBuffer/MeshPath/mesh.glsl +++ b/IDKEngine/Resource/Shaders/GBuffer/MeshPath/mesh.glsl @@ -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) diff --git a/IDKEngine/Resource/Shaders/GBuffer/VertexPath/vertex.glsl b/IDKEngine/Resource/Shaders/GBuffer/VertexPath/vertex.glsl index e82481ac..548b7560 100644 --- a/IDKEngine/Resource/Shaders/GBuffer/VertexPath/vertex.glsl +++ b/IDKEngine/Resource/Shaders/GBuffer/VertexPath/vertex.glsl @@ -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) diff --git a/IDKEngine/Resource/Shaders/GBuffer/fragment.glsl b/IDKEngine/Resource/Shaders/GBuffer/fragment.glsl index 861f518b..db0b361c 100644 --- a/IDKEngine/Resource/Shaders/GBuffer/fragment.glsl +++ b/IDKEngine/Resource/Shaders/GBuffer/fragment.glsl @@ -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) diff --git a/IDKEngine/Resource/Shaders/PathTracing/FinalDraw/compute.glsl b/IDKEngine/Resource/Shaders/PathTracing/FinalDraw/compute.glsl index cb117e29..cff2f73f 100644 --- a/IDKEngine/Resource/Shaders/PathTracing/FinalDraw/compute.glsl +++ b/IDKEngine/Resource/Shaders/PathTracing/FinalDraw/compute.glsl @@ -16,7 +16,7 @@ struct HitInfo uint InstanceID; }; -layout(std140, binding = 7) uniform SettingsUBO +layout(std140, binding = 0) uniform SettingsUBO { float FocalLength; float LenseRadius; diff --git a/IDKEngine/Resource/Shaders/PathTracing/FirstHit/compute.glsl b/IDKEngine/Resource/Shaders/PathTracing/FirstHit/compute.glsl index 1fccb20d..a845237a 100644 --- a/IDKEngine/Resource/Shaders/PathTracing/FirstHit/compute.glsl +++ b/IDKEngine/Resource/Shaders/PathTracing/FirstHit/compute.glsl @@ -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) @@ -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; diff --git a/IDKEngine/Resource/Shaders/PathTracing/NHit/compute.glsl b/IDKEngine/Resource/Shaders/PathTracing/NHit/compute.glsl index b51ee523..1e0105b1 100644 --- a/IDKEngine/Resource/Shaders/PathTracing/NHit/compute.glsl +++ b/IDKEngine/Resource/Shaders/PathTracing/NHit/compute.glsl @@ -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) @@ -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; diff --git a/IDKEngine/Resource/Shaders/PathTracing/include/Shading.glsl b/IDKEngine/Resource/Shaders/PathTracing/include/Shading.glsl index 26946638..96159f8e 100644 --- a/IDKEngine/Resource/Shaders/PathTracing/include/Shading.glsl +++ b/IDKEngine/Resource/Shaders/PathTracing/include/Shading.glsl @@ -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 diff --git a/IDKEngine/Resource/Shaders/SSAO/compute.glsl b/IDKEngine/Resource/Shaders/SSAO/compute.glsl index bf8e1102..f7f2e071 100644 --- a/IDKEngine/Resource/Shaders/SSAO/compute.glsl +++ b/IDKEngine/Resource/Shaders/SSAO/compute.glsl @@ -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; diff --git a/IDKEngine/Resource/Shaders/SSR/compute.glsl b/IDKEngine/Resource/Shaders/SSR/compute.glsl index 41c9b3fd..1156ee9f 100644 --- a/IDKEngine/Resource/Shaders/SSR/compute.glsl +++ b/IDKEngine/Resource/Shaders/SSR/compute.glsl @@ -1,7 +1,7 @@ #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; @@ -9,7 +9,7 @@ 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; diff --git a/IDKEngine/Resource/Shaders/ShadingRateClassification/compute.glsl b/IDKEngine/Resource/Shaders/ShadingRateClassification/compute.glsl index 4c165507..1927817d 100644 --- a/IDKEngine/Resource/Shaders/ShadingRateClassification/compute.glsl +++ b/IDKEngine/Resource/Shaders/ShadingRateClassification/compute.glsl @@ -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; diff --git a/IDKEngine/Resource/Shaders/ShadingRateClassification/debugCompute.glsl b/IDKEngine/Resource/Shaders/ShadingRateClassification/debugCompute.glsl index a271c12e..d9941210 100644 --- a/IDKEngine/Resource/Shaders/ShadingRateClassification/debugCompute.glsl +++ b/IDKEngine/Resource/Shaders/ShadingRateClassification/debugCompute.glsl @@ -10,7 +10,7 @@ layout(binding = 0) uniform sampler2D SamplerSrc; layout(binding = 1) uniform usampler2D SamplerDebugShadingRate; layout(binding = 1) uniform sampler2D SamplerDebugOtherData; // speed, luminance, or coefficient of variation of luminance -layout(std140, binding = 7) uniform SettingsUBO +layout(std140, binding = 0) uniform SettingsUBO { ENUM_DEBUG_MODE DebugMode; float SpeedFactor; diff --git a/IDKEngine/Resource/Shaders/Shadows/PointShadow/MeshPath/mesh.glsl b/IDKEngine/Resource/Shaders/Shadows/PointShadow/MeshPath/mesh.glsl index 6a69d49e..59a1f3f6 100644 --- a/IDKEngine/Resource/Shaders/Shadows/PointShadow/MeshPath/mesh.glsl +++ b/IDKEngine/Resource/Shaders/Shadows/PointShadow/MeshPath/mesh.glsl @@ -9,7 +9,7 @@ #define DECLARE_MESHLET_RENDERING_TYPES AppInclude(include/Constants.glsl) -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) AppInclude(include/StaticUniformBuffers.glsl) AppInclude(include/StaticStorageBuffers.glsl) diff --git a/IDKEngine/Resource/Shaders/Shadows/PointShadow/VertexPath/vertex.glsl b/IDKEngine/Resource/Shaders/Shadows/PointShadow/VertexPath/vertex.glsl index 8396e6e2..5f97d8ba 100644 --- a/IDKEngine/Resource/Shaders/Shadows/PointShadow/VertexPath/vertex.glsl +++ b/IDKEngine/Resource/Shaders/Shadows/PointShadow/VertexPath/vertex.glsl @@ -17,12 +17,12 @@ void main() { vec3 vertexPosition = Unpack(vertexPositionsSSBO.Positions[gl_VertexID]); - uint faceAndMeshInstanceID = visibleMeshInstanceSSBO.MeshInstanceIDs[gl_InstanceID + gl_BaseInstance * 6]; - uint faceID = faceAndMeshInstanceID >> 29; - uint meshInstanceID = faceAndMeshInstanceID & ((1u << 29) - 1); + uint faceAndMeshInstanceId = visibleMeshInstanceSSBO.MeshInstanceIDs[gl_InstanceID + gl_BaseInstance * 6]; + uint faceId = faceAndMeshInstanceId >> 29; + uint meshInstanceID = faceAndMeshInstanceId & ((1u << 29) - 1); mat4 modelMatrix = mat4(meshInstanceSSBO.MeshInstances[meshInstanceID].ModelMatrix); vec3 fragPos = vec3(modelMatrix * vec4(vertexPosition, 1.0)); - gl_Position = shadowsUBO.PointShadows[ShadowIndex].ProjViewMatrices[faceID] * vec4(fragPos, 1.0); - gl_Layer = int(faceID); + gl_Position = shadowsUBO.PointShadows[ShadowIndex].ProjViewMatrices[faceId] * vec4(fragPos, 1.0); + gl_Layer = int(faceId); } \ No newline at end of file diff --git a/IDKEngine/Resource/Shaders/ShadowsRayTraced/compute.glsl b/IDKEngine/Resource/Shaders/ShadowsRayTraced/compute.glsl index fa71c9ea..8e5fab4f 100644 --- a/IDKEngine/Resource/Shaders/ShadowsRayTraced/compute.glsl +++ b/IDKEngine/Resource/Shaders/ShadowsRayTraced/compute.glsl @@ -2,7 +2,7 @@ AppInclude(include/Sampling.glsl) AppInclude(include/Compression.glsl) -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) AppInclude(include/StaticStorageBuffers.glsl) AppInclude(include/StaticUniformBuffers.glsl) diff --git a/IDKEngine/Resource/Shaders/TAAResolve/compute.glsl b/IDKEngine/Resource/Shaders/TAAResolve/compute.glsl index 9ff79fb7..a063b555 100644 --- a/IDKEngine/Resource/Shaders/TAAResolve/compute.glsl +++ b/IDKEngine/Resource/Shaders/TAAResolve/compute.glsl @@ -10,7 +10,7 @@ layout(binding = 0) restrict writeonly uniform image2D ImgResult; layout(binding = 0) uniform sampler2D SamplerHistoryColor; layout(binding = 1) uniform sampler2D SamplerCurrentColor; -layout(std140, binding = 7) uniform SettingsUBO +layout(std140, binding = 0) uniform SettingsUBO { bool IsNaiveTaa; float PreferAliasingOverBlur; diff --git a/IDKEngine/Resource/Shaders/TonemapAndGammaCorrect/compute.glsl b/IDKEngine/Resource/Shaders/TonemapAndGammaCorrect/compute.glsl index 6a20e802..bfbcda22 100644 --- a/IDKEngine/Resource/Shaders/TonemapAndGammaCorrect/compute.glsl +++ b/IDKEngine/Resource/Shaders/TonemapAndGammaCorrect/compute.glsl @@ -7,7 +7,7 @@ layout(binding = 0) uniform sampler2D Sampler0; layout(binding = 1) uniform sampler2D Sampler1; layout(binding = 2) uniform sampler2D Sampler2; -layout(std140, binding = 7) uniform SettingsUBO +layout(std140, binding = 0) uniform SettingsUBO { float Exposure; float Saturation; diff --git a/IDKEngine/Resource/Shaders/UnprojectEquirectangular/compute.glsl b/IDKEngine/Resource/Shaders/UnprojectEquirectangular/compute.glsl index 3cfdc5ab..f878f548 100644 --- a/IDKEngine/Resource/Shaders/UnprojectEquirectangular/compute.glsl +++ b/IDKEngine/Resource/Shaders/UnprojectEquirectangular/compute.glsl @@ -1,6 +1,6 @@ #version 460 core -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; diff --git a/IDKEngine/Resource/Shaders/VXGI/ConeTracing/compute.glsl b/IDKEngine/Resource/Shaders/VXGI/ConeTracing/compute.glsl index 2978dbf2..7beb8abe 100644 --- a/IDKEngine/Resource/Shaders/VXGI/ConeTracing/compute.glsl +++ b/IDKEngine/Resource/Shaders/VXGI/ConeTracing/compute.glsl @@ -4,7 +4,7 @@ AppInclude(include/Surface.glsl) AppInclude(include/Sampling.glsl) AppInclude(include/TraceCone.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; @@ -12,7 +12,7 @@ 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 sampler3D SamplerVoxels; -layout(std140, binding = 7) uniform SettingsUBO +layout(std140, binding = 0) uniform SettingsUBO { int MaxSamples; float StepMultiplier; diff --git a/IDKEngine/Resource/Shaders/VXGI/Voxelize/DebugVisualization/compute.glsl b/IDKEngine/Resource/Shaders/VXGI/Voxelize/DebugVisualization/compute.glsl index f39b2cfe..1bff9b75 100644 --- a/IDKEngine/Resource/Shaders/VXGI/Voxelize/DebugVisualization/compute.glsl +++ b/IDKEngine/Resource/Shaders/VXGI/Voxelize/DebugVisualization/compute.glsl @@ -1,7 +1,7 @@ #version 460 core AppInclude(include/TraceCone.glsl) -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) AppInclude(include/IntersectionRoutines.glsl) AppInclude(include/StaticUniformBuffers.glsl) diff --git a/IDKEngine/Resource/Shaders/VXGI/Voxelize/Voxelize/fragment.glsl b/IDKEngine/Resource/Shaders/VXGI/Voxelize/Voxelize/fragment.glsl index 2bf2d17d..154232bd 100644 --- a/IDKEngine/Resource/Shaders/VXGI/Voxelize/Voxelize/fragment.glsl +++ b/IDKEngine/Resource/Shaders/VXGI/Voxelize/Voxelize/fragment.glsl @@ -10,7 +10,7 @@ AppInclude(include/Pbr.glsl) AppInclude(include/Surface.glsl) AppInclude(include/Compression.glsl) -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) AppInclude(include/StaticStorageBuffers.glsl) AppInclude(include/StaticUniformBuffers.glsl) diff --git a/IDKEngine/Resource/Shaders/VXGI/Voxelize/Voxelize/vertex.glsl b/IDKEngine/Resource/Shaders/VXGI/Voxelize/Voxelize/vertex.glsl index 85050344..4fa0736f 100644 --- a/IDKEngine/Resource/Shaders/VXGI/Voxelize/Voxelize/vertex.glsl +++ b/IDKEngine/Resource/Shaders/VXGI/Voxelize/Voxelize/vertex.glsl @@ -4,7 +4,7 @@ #define TAKE_FAST_GEOMETRY_SHADER_PATH AppInsert(TAKE_FAST_GEOMETRY_SHADER_PATH) AppInclude(include/Compression.glsl) -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) AppInclude(include/StaticUniformBuffers.glsl) AppInclude(include/StaticStorageBuffers.glsl) diff --git a/IDKEngine/Resource/Shaders/VolumetricLight/Upscale/compute.glsl b/IDKEngine/Resource/Shaders/VolumetricLight/Upscale/compute.glsl index 55457285..8a9c4f99 100644 --- a/IDKEngine/Resource/Shaders/VolumetricLight/Upscale/compute.glsl +++ b/IDKEngine/Resource/Shaders/VolumetricLight/Upscale/compute.glsl @@ -1,6 +1,6 @@ #version 460 core -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; diff --git a/IDKEngine/Resource/Shaders/VolumetricLight/compute.glsl b/IDKEngine/Resource/Shaders/VolumetricLight/compute.glsl index e7fa5636..8fc251c4 100644 --- a/IDKEngine/Resource/Shaders/VolumetricLight/compute.glsl +++ b/IDKEngine/Resource/Shaders/VolumetricLight/compute.glsl @@ -2,7 +2,7 @@ AppInclude(include/Pbr.glsl) AppInclude(include/Random.glsl) -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) AppInclude(include/StaticUniformBuffers.glsl) AppInclude(include/StaticStorageBuffers.glsl) @@ -11,7 +11,7 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; layout(binding = 0) restrict writeonly uniform image2D ImgResult; layout(binding = 1) restrict writeonly uniform image2D ImgResultDepth; -layout(std140, binding = 7) uniform SettingsUBO +layout(std140, binding = 0) uniform SettingsUBO { vec3 Absorbance; int SampleCount; diff --git a/IDKEngine/Resource/Shaders/include/Transformations.glsl b/IDKEngine/Resource/Shaders/include/Math.glsl similarity index 82% rename from IDKEngine/Resource/Shaders/include/Transformations.glsl rename to IDKEngine/Resource/Shaders/include/Math.glsl index a295c341..a7e6483e 100644 --- a/IDKEngine/Resource/Shaders/include/Transformations.glsl +++ b/IDKEngine/Resource/Shaders/include/Math.glsl @@ -141,3 +141,36 @@ vec3 PolarToCartesian(float azimuth, float elevation) { return PolarToCartesian(azimuth, elevation, 1.0); } + +uint NextPowerOfTwo(uint num) +{ + return 2u << findMSB(max(1u, num) - 1u); +} + +/// Source: https://developer.nvidia.com/blog/thinking-parallel-part-iii-tree-construction-gpu/ + +uint ExpandBits(uint v) +{ + v = (v * 0x00010001u) & 0xFF0000FFu; + v = (v * 0x00000101u) & 0x0F00F00Fu; + v = (v * 0x00000011u) & 0xC30C30C3u; + v = (v * 0x00000005u) & 0x49249249u; + + return v; +} + +// Calculates a 30-bit Morton code for the +// given 3D point located within the unit cube [0,1]. +uint GetMorton(vec3 normalizedV) +{ + uint x = clamp(uint(normalizedV.x * 1024.0f), 0u, 1023u); + uint y = clamp(uint(normalizedV.y * 1024.0f), 0u, 1023u); + uint z = clamp(uint(normalizedV.z * 1024.0f), 0u, 1023u); + + uint xx = ExpandBits(x); + uint yy = ExpandBits(y); + uint zz = ExpandBits(z); + uint result = xx * 4 + yy * 2 + zz; + + return result; +} diff --git a/IDKEngine/Resource/Shaders/include/Random.glsl b/IDKEngine/Resource/Shaders/include/Random.glsl index 34cffb6d..46e8bab2 100644 --- a/IDKEngine/Resource/Shaders/include/Random.glsl +++ b/IDKEngine/Resource/Shaders/include/Random.glsl @@ -1,5 +1,5 @@ AppInclude(include/Constants.glsl) -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) uint Random_RNGSeed; diff --git a/IDKEngine/Resource/Shaders/include/Sampling.glsl b/IDKEngine/Resource/Shaders/include/Sampling.glsl index 8f07449a..961a0eb2 100644 --- a/IDKEngine/Resource/Shaders/include/Sampling.glsl +++ b/IDKEngine/Resource/Shaders/include/Sampling.glsl @@ -1,5 +1,5 @@ AppInclude(include/Random.glsl) -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) vec3 SampleCone(vec3 normal, float phi, float sinTheta, float cosTheta) { diff --git a/IDKEngine/Resource/Shaders/include/StaticUniformBuffers.glsl b/IDKEngine/Resource/Shaders/include/StaticUniformBuffers.glsl index cc12508e..794a5478 100644 --- a/IDKEngine/Resource/Shaders/include/StaticUniformBuffers.glsl +++ b/IDKEngine/Resource/Shaders/include/StaticUniformBuffers.glsl @@ -4,24 +4,26 @@ AppInclude(include/GpuTypes.glsl) #define GPU_MAX_UBO_POINT_SHADOW_COUNT 128 #define GPU_MAX_UBO_LIGHT_COUNT 256 -layout(std140, binding = 0) uniform PerFrameDataUBO +// binding 0 reserved for temporary UBOs + +layout(std140, binding = 1) uniform PerFrameDataUBO { GpuPerFrameData perFrameDataUBO; }; -layout(std140, binding = 1) uniform LightsUBO +layout(std140, binding = 2) uniform LightsUBO { GpuLight Lights[GPU_MAX_UBO_LIGHT_COUNT]; int Count; } lightsUBO; -layout(std140, binding = 2) uniform ShadowsUBO +layout(std140, binding = 3) uniform ShadowsUBO { GpuPointShadow PointShadows[GPU_MAX_UBO_POINT_SHADOW_COUNT]; int Count; } shadowsUBO; -layout(std140, binding = 3) uniform TaaDataUBO +layout(std140, binding = 4) uniform TaaDataUBO { vec2 Jitter; int SampleCount; @@ -29,12 +31,12 @@ layout(std140, binding = 3) uniform TaaDataUBO int TemporalAntiAliasingMode; } taaDataUBO; -layout(std140, binding = 4) uniform SkyBoxUBO +layout(std140, binding = 5) uniform SkyBoxUBO { samplerCube Albedo; } skyBoxUBO; -layout(std140, binding = 5) uniform VoxelizerDataUBO +layout(std140, binding = 6) uniform VoxelizerDataUBO { vec3 GridMin; float _pad0; @@ -42,7 +44,7 @@ layout(std140, binding = 5) uniform VoxelizerDataUBO float _pad1; } voxelizerDataUBO; -layout(std140, binding = 6) uniform GBufferDataUBO +layout(std140, binding = 7) uniform GBufferDataUBO { sampler2D AlbedoAlpha; sampler2D Normal; diff --git a/IDKEngine/Resource/Shaders/include/TraceCone.glsl b/IDKEngine/Resource/Shaders/include/TraceCone.glsl index 59281171..c7637578 100644 --- a/IDKEngine/Resource/Shaders/include/TraceCone.glsl +++ b/IDKEngine/Resource/Shaders/include/TraceCone.glsl @@ -1,6 +1,6 @@ AppInclude(include/StaticUniformBuffers.glsl) AppInclude(include/Ray.glsl) -AppInclude(include/Transformations.glsl) +AppInclude(include/Math.glsl) vec4 TraceCone(sampler3D samplerVoxels, Ray ray, vec3 normal, float coneAngle, float stepMultiplier, float normalRayOffset, float alphaThreshold) { diff --git a/IDKEngine/Resource/imgui.ini b/IDKEngine/Resource/imgui.ini index 712cdac8..d93199ec 100644 --- a/IDKEngine/Resource/imgui.ini +++ b/IDKEngine/Resource/imgui.ini @@ -3,7 +3,7 @@ Size=1280,720 Collapsed=0 [Window][Debug##Default] -Pos=154,130 +Pos=101,0 Size=441,63 Collapsed=0 diff --git a/IDKEngine/Source/Application.cs b/IDKEngine/Source/Application.cs index 598e42b2..36ced3e9 100644 --- a/IDKEngine/Source/Application.cs +++ b/IDKEngine/Source/Application.cs @@ -104,7 +104,7 @@ public Vector2i RenderResolution private Gui gui; public bool IsBloom = true; public bool IsVolumetricLighting = true; - public bool RenderGui = true; + public bool RenderImGui = true; // All models and all lights and Camera (the types of different entities) public ModelManager ModelManager; @@ -278,7 +278,7 @@ protected override void OnRender(float dT) } BBG.Rendering.SetViewport(WindowFramebufferSize); - if (RenderGui) + if (RenderImGui) { gui.Draw(this, dT); } @@ -309,7 +309,7 @@ protected override void OnUpdate(float dT) ShouldClose(); } - if (!ImGuiNET.ImGui.GetIO().WantCaptureKeyboard) + if (!RenderImGui || !ImGuiNET.ImGui.GetIO().WantCaptureKeyboard) { if (KeyboardState[Keys.V] == Keyboard.InputState.Touched) { @@ -317,8 +317,8 @@ protected override void OnUpdate(float dT) } if (KeyboardState[Keys.G] == Keyboard.InputState.Touched) { - RenderGui = !RenderGui; - if (!RenderGui) + RenderImGui = !RenderImGui; + if (!RenderImGui) { RequestPresentationResolution = WindowFramebufferSize; } @@ -352,7 +352,7 @@ protected override void OnUpdate(float dT) if (RecorderVars.State != FrameRecorderState.Replaying) { - if (!ImGuiNET.ImGui.GetIO().WantCaptureMouse) + if (!RenderImGui || !ImGuiNET.ImGui.GetIO().WantCaptureMouse) { if (MouseState.CursorMode == CursorModeValue.CursorDisabled) { @@ -403,7 +403,7 @@ protected override void OnUpdate(float dT) Vector3 slidedDeltaStep = Plane.Project(deltaStep, hitInfo.SlidingPlane); Camera.Position = movingSphere.Center + slidedDeltaStep; - Camera.Velocity = Plane.Project(Camera.Velocity, hitInfo.SlidingPlane); + Camera.Velocity = Plane.Project(Camera.Velocity, hitInfo.SlidingPlane); prevSpherePos = movingSphere.Center; }); @@ -412,7 +412,7 @@ protected override void OnUpdate(float dT) Camera.SetPrevToCurrentPosition(); } - protected override void OnStart() + protected override unsafe void OnStart() { BBG.Initialize(Helper.GLDebugCallback); @@ -420,7 +420,7 @@ protected override void OnStart() Logger.Log(Logger.LogLevel.Info, $"API: {glContextInfo.APIName}"); Logger.Log(Logger.LogLevel.Info, $"GPU: {glContextInfo.DeviceInfo.Name}"); - Logger.Log(Logger.LogLevel.Info, $"{nameof(BBG.AbstractShader.Preprocessor.SUPPORTS_LINE_SOURCEFILE)} = {BBG.AbstractShader.Preprocessor.SUPPORTS_LINE_SOURCEFILE}"); + Logger.Log(Logger.LogLevel.Info, $"{nameof(BBG.AbstractShader.Preprocessor.SUPPORTS_LINE_DIRECTIVE_SOURCEFILE)} = {BBG.AbstractShader.Preprocessor.SUPPORTS_LINE_DIRECTIVE_SOURCEFILE}"); if (glContextInfo.GLVersion < 4.6) { @@ -444,7 +444,7 @@ protected override void OnStart() gpuPerFrameDataBuffer = new BBG.TypedBuffer(); gpuPerFrameDataBuffer.AllocateElements(BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, 1); - gpuPerFrameDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 0); + gpuPerFrameDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 1); SkyBoxManager.Initialize(); SkyBoxManager.SkyBoxImagePaths = [ @@ -497,6 +497,7 @@ protected override void OnStart() //ModelLoader.Model bistro = ModelLoader.LoadGltfFromFile(@"C:\Users\Julian\Downloads\Models\Bistro\BistroCompressed\Bistro.glb").Value; //ModelLoader.Model test = ModelLoader.LoadGltfFromFile(@"C:\Users\Julian\Downloads\Models\SponzaMerged\SponzaMerged.gltf", new Transformation().WithTranslation(2.0f, 0.0f, 1.3f).GetMatrix()).Value; + //ModelLoader.Model plane = ModelLoader.LoadGltfFromFile(@"C:\Users\Julian\Downloads\Models\glTF-Sample-Assets\Models\NodePerformanceTest\glTF-Binary\NodePerformanceTest.glb").Value; ModelManager.Add(sponza, lucy, helmet); @@ -508,11 +509,11 @@ protected override void OnStart() for (int i = 0; i < 3; i++) { - if (LightManager.TryGetLight(i, out CpuLight light)) - { - CpuPointShadow pointShadow = new CpuPointShadow(512, WindowFramebufferSize, new Vector2(light.GpuLight.Radius, 60.0f)); - LightManager.CreatePointShadowForLight(pointShadow, i); - } + if (LightManager.TryGetLight(i, out CpuLight light)) + { + CpuPointShadow pointShadow = new CpuPointShadow(512, WindowFramebufferSize, new Vector2(light.GpuLight.Radius, 60.0f)); + LightManager.CreatePointShadowForLight(pointShadow, i); + } } } else @@ -542,11 +543,14 @@ protected override void OnWindowResize() { gui.SetSize(WindowFramebufferSize); - // If GUI is used it will handle render resizing - if (!RenderGui) + // If GUI is used it calculates and sets the new (viewport-)resolution + // If GUI is not used the new resolution is simply window size, that case is handled here + if (!RenderImGui) { RequestPresentationResolution = WindowFramebufferSize; } + + OnRender(gpuPerFrameData.DeltaRenderTime); } protected override void OnKeyPress(uint key) diff --git a/IDKEngine/Source/Bvh/BLAS.cs b/IDKEngine/Source/Bvh/BLAS.cs index 8962868f..9824ce3b 100644 --- a/IDKEngine/Source/Bvh/BLAS.cs +++ b/IDKEngine/Source/Bvh/BLAS.cs @@ -447,8 +447,8 @@ public static GpuBlasNode[] AllocateUpperBoundNodes(int triangleCount) for (int axis = 0; axis < 3; axis++) { Box rightBoxAccum = Box.Empty(); - int firstRightTri = triStart; - for (int i = triEnd - 1; i > triStart; i--) + int firstRightTri = triStart + 1; + for (int i = triEnd - 1; i >= firstRightTri; i--) { rightBoxAccum.GrowToFit(buildData.TriBounds[buildData.TrisAxesSorted[axis][i]]); @@ -462,23 +462,24 @@ public static GpuBlasNode[] AllocateUpperBoundNodes(int triangleCount) if (rightCost >= splitCost) { // Don't need to consider split positions beyond this point as cost is already greater and will only get more - firstRightTri = i; + firstRightTri = i + 1; break; } } Box leftBoxAccum = Box.Empty(); - for (int i = triStart; i < firstRightTri; i++) + for (int i = triStart; i < firstRightTri - 1; i++) { leftBoxAccum.GrowToFit(buildData.TriBounds[buildData.TrisAxesSorted[axis][i]]); } - for (int i = firstRightTri; i < triEnd - 1; i++) + for (int i = firstRightTri - 1; i < triEnd - 1; i++) { leftBoxAccum.GrowToFit(buildData.TriBounds[buildData.TrisAxesSorted[axis][i]]); // Implementation of "Surface Area Heuristic" described in "Spatial Splits in Bounding Volume Hierarchies" // https://www.nvidia.in/docs/IO/77714/sbvh.pdf 2.1 BVH Construction - int triCount = (i + 1) - triStart; + int triIndex = i + 1; + int triCount = triIndex - triStart; float areaParent = parentNode.HalfArea(); float probHitLeftChild = leftBoxAccum.HalfArea() / areaParent; @@ -486,13 +487,13 @@ public static GpuBlasNode[] AllocateUpperBoundNodes(int triangleCount) float rightCost = buildData.RightCostsAccum[i + 1]; // Estimates cost of hitting parentNode if it was split at the evaluated split position. - // The full "Surface Area Heuristic" is recursive, but in practice assume + // The full "Surface Area Heuristic" is recursive, but in practice we assume // the resulting child nodes are leafs. This the greedy SAH approach float surfaceAreaHeuristic = CostInternalNode(leftCost, rightCost, settings.TraversalCost); if (surfaceAreaHeuristic < splitCost) { - triPivot = i + 1; + triPivot = triIndex; splitAxis = axis; splitCost = surfaceAreaHeuristic; } diff --git a/IDKEngine/Source/Bvh/BVH.cs b/IDKEngine/Source/Bvh/BVH.cs index 84a380f1..59f939be 100644 --- a/IDKEngine/Source/Bvh/BVH.cs +++ b/IDKEngine/Source/Bvh/BVH.cs @@ -304,11 +304,15 @@ public void TlasBuild(bool force = false) TLAS.Build(TlasNodes, GetPrimitive, meshInstances.Length, new TLAS.BuildSettings()); BBG.Buffer.Recreate(ref tlasBuffer, BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, TlasNodes); - void GetPrimitive(int primId, out BLAS.BuildResult blas, out Matrix4 worldTransform) + Box GetPrimitive(int primId) { ref readonly GpuMeshInstance meshInstance = ref meshInstances[primId]; - blas = GetBlas(BlasesDesc[meshInstance.MeshId]); - worldTransform = meshInstance.ModelMatrix; + + BLAS.BuildResult blas = GetBlas(BlasesDesc[meshInstance.MeshId]); + Box localBounds = Conversions.ToBox(blas.Root); + Box worldSpaceBounds = Box.Transformed(localBounds, meshInstance.ModelMatrix); + + return worldSpaceBounds; } } } @@ -428,7 +432,7 @@ public void GpuBlasesRefit(int start, int count) { BlasDesc blasDesc = BlasesDesc[i]; - blasRefitLockBuffer.Clear(0, blasRefitLockBuffer.Size, 0); + blasRefitLockBuffer.Fill(0, blasRefitLockBuffer.Size, 0); BBG.Computing.Compute("Refit BLAS", () => { refitBlasProgram.Upload(0, (uint)blasDesc.RootNodeOffset); @@ -437,7 +441,7 @@ public void GpuBlasesRefit(int start, int count) refitBlasProgram.Upload(3, (uint)blasDesc.LeafIndicesCount); BBG.Cmd.UseShaderProgram(refitBlasProgram); - BBG.Computing.Dispatch((blasDesc.LeafIndicesCount + 64 - 1) / 64, 1, 1); + BBG.Computing.Dispatch(MyMath.DivUp(blasDesc.LeafIndicesCount, 64), 1, 1); }); } diff --git a/IDKEngine/Source/Bvh/TLAS.cs b/IDKEngine/Source/Bvh/TLAS.cs index 1208da80..e31a19fc 100644 --- a/IDKEngine/Source/Bvh/TLAS.cs +++ b/IDKEngine/Source/Bvh/TLAS.cs @@ -17,10 +17,10 @@ public BuildSettings() } } - public delegate void FuncGetBlas(int instanceId, out BLAS.BuildResult blas, out Matrix4 worldTransform); + public delegate Box FuncGetPrimitive(int primId); public delegate void FuncGetBlasAndGeometry(int instanceId, out BLAS.BuildResult blas, out BLAS.Geometry geometry, out Matrix4 invWorldTransform); - public static void Build(Span nodes, FuncGetBlas funcGetBlas, int primitiveCount, BuildSettings buildSettings) + public static void Build(Span nodes, FuncGetPrimitive funcGetLeaf, int primitiveCount, BuildSettings buildSettings) { if (nodes.Length == 0) return; @@ -30,10 +30,7 @@ public static void Build(Span nodes, FuncGetBlas funcGetBlas, int p Box globalBounds = Box.Empty(); for (int i = 0; i < initialChildNodes.Length; i++) { - funcGetBlas(i, out BLAS.BuildResult blas, out Matrix4 worldTransform); - - Box localBounds = Conversions.ToBox(blas.Root); - Box worldSpaceBounds = Box.Transformed(localBounds, worldTransform); + Box worldSpaceBounds = funcGetLeaf(i); globalBounds.GrowToFit(worldSpaceBounds); GpuTlasNode newNode = new GpuTlasNode(); @@ -49,10 +46,10 @@ public static void Build(Span nodes, FuncGetBlas funcGetBlas, int p MemoryExtensions.Sort(initialChildNodes, (GpuTlasNode a, GpuTlasNode b) => { Vector3 mappedA = MyMath.MapToZeroOne((a.Max + a.Min) * 0.5f, globalBounds.Min, globalBounds.Max); - ulong mortonCodeA = MyMath.GetMorton(mappedA); + uint mortonCodeA = MyMath.GetMorton(mappedA); Vector3 mappedB = MyMath.MapToZeroOne((b.Max + b.Min) * 0.5f, globalBounds.Min, globalBounds.Max); - ulong mortonCodeB = MyMath.GetMorton(mappedB); + uint mortonCodeB = MyMath.GetMorton(mappedB); if (mortonCodeA > mortonCodeB) return 1; if (mortonCodeA == mortonCodeB) return 0; diff --git a/IDKEngine/Source/ModelManager.cs b/IDKEngine/Source/ModelManager.cs index 854d56d6..7a108eb3 100644 --- a/IDKEngine/Source/ModelManager.cs +++ b/IDKEngine/Source/ModelManager.cs @@ -334,7 +334,7 @@ public void RemoveMesh(Range rmMeshRange) } } Helper.ArrayRemove(ref Meshes, rmMeshRange.Start, rmMeshRange.Count); - + foreach (int rmMaterialId in freeListMaterials.Reverse()) { GpuMaterial rmGpuMaterial = GpuMaterials[rmMaterialId]; @@ -602,7 +602,7 @@ public void ComputeSkinnedPositions(bool anyAnimatedNodeMoved) skinningShaderProgram.Upload(3, (uint)vertexCount); BBG.Cmd.UseShaderProgram(skinningShaderProgram); - BBG.Computing.Dispatch((vertexCount + 64 - 1) / 64, 1, 1); + BBG.Computing.Dispatch(MyMath.DivUp(vertexCount, 64), 1, 1); }); vertexPositionsBuffer.CopyElementsTo(vertexPositionsHostBuffer, outputVertexOffset, outputVertexOffset, vertexCount); } diff --git a/IDKEngine/Source/Render/AtmosphericScatterer.cs b/IDKEngine/Source/Render/AtmosphericScatterer.cs index 16360297..179e025e 100644 --- a/IDKEngine/Source/Render/AtmosphericScatterer.cs +++ b/IDKEngine/Source/Render/AtmosphericScatterer.cs @@ -1,5 +1,6 @@ using System; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -42,7 +43,7 @@ public void Compute() BBG.Cmd.BindImageUnit(Result, 0, 0, true); BBG.Cmd.UseShaderProgram(shaderProgram); - BBG.Computing.Dispatch((Result.Width + 8 - 1) / 8, (Result.Width + 8 - 1) / 8, 6); + BBG.Computing.Dispatch(MyMath.DivUp(Result.Width, 8), MyMath.DivUp(Result.Width, 8), 6); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } diff --git a/IDKEngine/Source/Render/Bloom.cs b/IDKEngine/Source/Render/Bloom.cs index 9cbc7b83..17c77222 100644 --- a/IDKEngine/Source/Render/Bloom.cs +++ b/IDKEngine/Source/Render/Bloom.cs @@ -1,6 +1,7 @@ using System; using OpenTK.Mathematics; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -71,12 +72,11 @@ public void Compute(BBG.Texture src) BBG.Cmd.BindImageUnit(downscaleTexture, 0); Vector3i mipLevelSize = BBG.Texture.GetMipmapLevelSize(downscaleTexture.Width, downscaleTexture.Height, 1, currentWriteLod); - BBG.Computing.Dispatch((mipLevelSize.X + 8 - 1) / 8, (mipLevelSize.Y + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(mipLevelSize.X, 8), MyMath.DivUp(mipLevelSize.Y, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); currentWriteLod++; }); - BBG.Cmd.BindTextureUnit(downscaleTexture, 0); for (; currentWriteLod < downscaleTexture.Levels; currentWriteLod++) { @@ -86,7 +86,7 @@ public void Compute(BBG.Texture src) BBG.Cmd.BindImageUnit(downscaleTexture, 0, currentWriteLod); Vector3i mipLevelSize = BBG.Texture.GetMipmapLevelSize(downscaleTexture.Width, downscaleTexture.Height, 1, currentWriteLod); - BBG.Computing.Dispatch((mipLevelSize.X + 8 - 1) / 8, (mipLevelSize.Y + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(mipLevelSize.X, 8), MyMath.DivUp(mipLevelSize.Y, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } @@ -104,7 +104,7 @@ public void Compute(BBG.Texture src) BBG.Cmd.BindImageUnit(upsampleTexture, 0, currentWriteLod); Vector3i mipLevelSize = BBG.Texture.GetMipmapLevelSize(upsampleTexture.Width, upsampleTexture.Height, 1, currentWriteLod); - BBG.Computing.Dispatch((mipLevelSize.X + 8 - 1) / 8, (mipLevelSize.Y + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(mipLevelSize.X, 8), MyMath.DivUp(mipLevelSize.Y, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); currentWriteLod--; @@ -119,7 +119,7 @@ public void Compute(BBG.Texture src) BBG.Cmd.BindImageUnit(upsampleTexture, 0, currentWriteLod); Vector3i mipLevelSize = BBG.Texture.GetMipmapLevelSize(upsampleTexture.Width, upsampleTexture.Height, 1, currentWriteLod); - BBG.Computing.Dispatch((mipLevelSize.X + 8 - 1) / 8, (mipLevelSize.Y + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(mipLevelSize.X, 8), MyMath.DivUp(mipLevelSize.Y, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } diff --git a/IDKEngine/Source/Render/CpuPointShadow.cs b/IDKEngine/Source/Render/CpuPointShadow.cs index da707874..b743f33c 100644 --- a/IDKEngine/Source/Render/CpuPointShadow.cs +++ b/IDKEngine/Source/Render/CpuPointShadow.cs @@ -149,7 +149,7 @@ public void RenderShadowMap(ModelManager modelManager, Camera camera, int gpuPoi cullingProgram.Upload(2, visibleFaces); BBG.Cmd.UseShaderProgram(cullingProgram); - BBG.Computing.Dispatch((modelManager.MeshInstances.Length + 64 - 1) / 64, 1, 1); + BBG.Computing.Dispatch(MyMath.DivUp(modelManager.MeshInstances.Length, 64), 1, 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.CommandBarrierBit); }); diff --git a/IDKEngine/Source/Render/Gui.cs b/IDKEngine/Source/Render/Gui.cs index b9a4996c..9d862abf 100644 --- a/IDKEngine/Source/Render/Gui.cs +++ b/IDKEngine/Source/Render/Gui.cs @@ -401,8 +401,9 @@ private unsafe void DrawMyGui(Application app) ImGui.Checkbox("IsVXGI", ref app.RasterizerPipeline.IsVXGI); if (app.RasterizerPipeline.IsVXGI) { - ToolTipForItemAboveHovered("Controls wether the scene is re-voxelized every frame"); ImGui.Checkbox("GridReVoxelize", ref app.RasterizerPipeline.GridReVoxelize); + ToolTipForItemAboveHovered("Controls wether the scene is re-voxelized every frame"); + ImGui.Checkbox("GridFollowCamera", ref app.RasterizerPipeline.GridFollowCamera); ImGui.Checkbox("IsConfigureGrid", ref app.RasterizerPipeline.IsConfigureGridMode); @@ -1273,7 +1274,7 @@ public void Update(Application app) if (app.MouseState.CursorMode == CursorModeValue.CursorNormal && app.MouseState[MouseButton.Left] == Keyboard.InputState.Touched) { OtkVec2 clickedPixel = app.MouseState.Position; - if (app.RenderGui) + if (app.RenderImGui) { clickedPixel -= viewportHeaderSize.ToOpenTK(); } diff --git a/IDKEngine/Source/Render/LightManager.cs b/IDKEngine/Source/Render/LightManager.cs index 66e814f3..caeb52d2 100644 --- a/IDKEngine/Source/Render/LightManager.cs +++ b/IDKEngine/Source/Render/LightManager.cs @@ -79,7 +79,7 @@ public unsafe LightManager() lightBufferObject = new BBG.TypedBuffer(); lightBufferObject.Allocate(BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, lights.Length * sizeof(GpuLight) + sizeof(int)); - lightBufferObject.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 1); + lightBufferObject.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 2); const int SphereLatitudes = 12, SphereLongitudes = 12; const float SphereRadius = 1.0f; @@ -314,6 +314,11 @@ public void Update(float dT, ModelManager modelManager) } } + public void FSR2WorkaroundRebindUBO() + { + pointShadowManager.FSR2WorkaroundRebindUBO(); + } + private static void CollisionResponse(CpuLight light, CpuLight otherLight, float intersectionTime) { Vector3 newLightPosition = Vector3.Lerp(light.GpuLight.PrevPosition, light.GpuLight.Position, intersectionTime); diff --git a/IDKEngine/Source/Render/LightingShadingRateClassifier.cs b/IDKEngine/Source/Render/LightingShadingRateClassifier.cs index 459f3cd6..d47ff80f 100644 --- a/IDKEngine/Source/Render/LightingShadingRateClassifier.cs +++ b/IDKEngine/Source/Render/LightingShadingRateClassifier.cs @@ -1,6 +1,7 @@ using System; using OpenTK.Mathematics; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -68,7 +69,7 @@ public void Compute(BBG.Texture shaded) BBG.Cmd.BindTextureUnit(shaded, 0); BBG.Cmd.UseShaderProgram(shaderProgram); - BBG.Computing.Dispatch((shaded.Width + TILE_SIZE - 1) / TILE_SIZE, (shaded.Height + TILE_SIZE - 1) / TILE_SIZE, 1); + BBG.Computing.Dispatch(MyMath.DivUp(shaded.Width, TILE_SIZE), MyMath.DivUp(shaded.Height, TILE_SIZE), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } @@ -89,7 +90,7 @@ public void DebugRender(BBG.Texture dest) BBG.Cmd.BindTextureUnit(Settings.DebugValue == DebugMode.ShadingRate ? Result : debugTexture, 1); BBG.Cmd.UseShaderProgram(debugProgram); - BBG.Computing.Dispatch((dest.Width + TILE_SIZE - 1) / TILE_SIZE, (dest.Height + TILE_SIZE - 1) / TILE_SIZE, 1); + BBG.Computing.Dispatch(MyMath.DivUp(dest.Width, TILE_SIZE), MyMath.DivUp(dest.Height, TILE_SIZE), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } diff --git a/IDKEngine/Source/Render/PathTracer.cs b/IDKEngine/Source/Render/PathTracer.cs index d15555cd..6eace837 100644 --- a/IDKEngine/Source/Render/PathTracer.cs +++ b/IDKEngine/Source/Render/PathTracer.cs @@ -2,6 +2,7 @@ using System.Runtime.InteropServices; using OpenTK.Mathematics; using BBOpenGL; +using IDKEngine.Utils; using IDKEngine.GpuTypes; namespace IDKEngine.Render @@ -11,7 +12,7 @@ class PathTracer : IDisposable public Vector2i RenderResolution => new Vector2i(Result.Width, Result.Height); private int cachedRayDepth; - + public int _rayDepth; public int RayDepth { @@ -133,17 +134,17 @@ public void Compute() { BBG.Cmd.SetUniforms(settings); - BBG.Computing.Compute($"PathTrace Primary Rays", () => + BBG.Computing.Compute("PathTrace Primary Rays", () => { BBG.Cmd.BindImageUnit(Result, 0); BBG.Cmd.UseShaderProgram(firstHitProgram); - BBG.Computing.Dispatch((Result.Width + 8 - 1) / 8, (Result.Height + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(Result.Width, 8), MyMath.DivUp(Result.Height, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.ShaderStorageBarrierBit | BBG.Cmd.MemoryBarrierMask.CommandBarrierBit); }); for (int i = 1; i < RayDepth; i++) { - BBG.Computing.Compute("PathTrace Ray bounce {i}", () => + BBG.Computing.Compute($"PathTrace Ray bounce {i}", () => { int pingPongIndex = i % 2; @@ -164,10 +165,9 @@ public void Compute() BBG.Cmd.BindImageUnit(Result, 0); BBG.Cmd.UseShaderProgram(finalDrawProgram); - BBG.Computing.Dispatch((Result.Width + 8 - 1) / 8, (Result.Height + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(Result.Width, 8), MyMath.DivUp(Result.Height, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit | BBG.Cmd.MemoryBarrierMask.ShaderStorageBarrierBit | BBG.Cmd.MemoryBarrierMask.CommandBarrierBit); }); - AccumulatedSamples++; } @@ -178,11 +178,14 @@ public unsafe void SetSize(Vector2i size) Result.SetFilter(BBG.Sampler.MinFilter.Linear, BBG.Sampler.MagFilter.Linear); Result.SetWrapMode(BBG.Sampler.WrapMode.ClampToEdge, BBG.Sampler.WrapMode.ClampToEdge); Result.Allocate(size.X, size.Y, 1, BBG.Texture.InternalFormat.R32G32B32A32Float); - Result.Clear(BBG.Texture.PixelFormat.R, BBG.Texture.PixelType.Float, 0.0f); + Result.Fill(BBG.Texture.PixelFormat.RGBA, BBG.Texture.PixelType.Float, new Vector4(0.0f)); BBG.Buffer.Recreate(ref wavefrontRayBuffer, BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, size.X * size.Y); BBG.Buffer.Recreate(ref wavefrontPTBuffer, BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, sizeof(GpuWavefrontPTHeader) + (size.X * size.Y * sizeof(uint))); - wavefrontPTBuffer.Clear(0, sizeof(GpuWavefrontPTHeader), 0.0f); + wavefrontPTBuffer.UploadData(0, sizeof(GpuWavefrontPTHeader), new GpuWavefrontPTHeader() + { + DispatchCommand = new BBG.DispatchIndirectCommand() { NumGroupsX = 0, NumGroupsY = 1, NumGroupsZ = 1 } + }); ResetAccumulation(); } diff --git a/IDKEngine/Source/Render/PointShadowManager.cs b/IDKEngine/Source/Render/PointShadowManager.cs index 9cda500d..1e024109 100644 --- a/IDKEngine/Source/Render/PointShadowManager.cs +++ b/IDKEngine/Source/Render/PointShadowManager.cs @@ -2,6 +2,7 @@ using OpenTK.Mathematics; using BBLogger; using BBOpenGL; +using IDKEngine.Utils; using IDKEngine.GpuTypes; namespace IDKEngine.Render @@ -33,7 +34,12 @@ public unsafe PointShadowManager() pointShadowsBuffer = new BBG.TypedBuffer(); pointShadowsBuffer.Allocate(BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, GPU_MAX_UBO_POINT_SHADOW_COUNT * sizeof(GpuPointShadow) + sizeof(int)); - pointShadowsBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 2); + FSR2WorkaroundRebindUBO(); + } + + public void FSR2WorkaroundRebindUBO() + { + pointShadowsBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 3); } public void RenderShadowMaps(ModelManager modelManager, Camera camera) @@ -72,7 +78,7 @@ public void ComputeRayTracedShadowMaps(int samples) rayTracedShadowsProgram.Upload(0, samples); BBG.Cmd.UseShaderProgram(rayTracedShadowsProgram); - BBG.Computing.Dispatch((commonSize.X + 8 - 1) / 8, (commonSize.Y + 8 - 1) / 8, Count); + BBG.Computing.Dispatch(MyMath.DivUp(commonSize.X, 8), MyMath.DivUp(commonSize.Y, 8), Count); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.ShaderImageAccessBarrierBit); }); } diff --git a/IDKEngine/Source/Render/RasterPipeline.cs b/IDKEngine/Source/Render/RasterPipeline.cs index 78cf210b..ea600874 100644 --- a/IDKEngine/Source/Render/RasterPipeline.cs +++ b/IDKEngine/Source/Render/RasterPipeline.cs @@ -210,11 +210,11 @@ public RasterPipeline(Vector2i renderSize, Vector2i presentationSize) taaDataBuffer = new BBG.TypedBuffer(); taaDataBuffer.AllocateElements(BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, 1); - taaDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 3); + taaDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 4); bindlessGBufferBuffer = new BBG.TypedBuffer(); bindlessGBufferBuffer.AllocateElements(BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, 1); - bindlessGBufferBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 6); + bindlessGBufferBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 7); SSAO = new SSAO(renderSize, new SSAO.GpuSettings()); SSR = new SSR(renderSize, new SSR.GpuSettings()); @@ -340,7 +340,7 @@ public void Render(ModelManager modelManager, LightManager lightManager, Camera modelManager.ResetInstanceCounts(); BBG.Cmd.UseShaderProgram(cullingProgram); - BBG.Computing.Dispatch((modelManager.MeshInstances.Length + 64 - 1) / 64, 1, 1); + BBG.Computing.Dispatch(MyMath.DivUp(modelManager.MeshInstances.Length, 64), 1, 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.CommandBarrierBit); }); @@ -491,7 +491,7 @@ public void Render(ModelManager modelManager, LightManager lightManager, Camera BBG.Cmd.BindTextureUnit(SSR.Result, 1, IsSSR); BBG.Cmd.UseShaderProgram(mergeLightingProgram); - BBG.Computing.Dispatch((RenderResolution.X + 8 - 1) / 8, (RenderResolution.Y + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(RenderResolution.X, 8), MyMath.DivUp(RenderResolution.Y, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); @@ -504,10 +504,10 @@ public void Render(ModelManager modelManager, LightManager lightManager, Camera FSR2Wrapper.Run(beforeTAATexture, DepthTexture, VelocityTexture, camera, gpuTaaData.Jitter, dT * 1000.0f); // This is a hack to fix global UBO bindings modified by FSR2 - taaDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 3); - SkyBoxManager.skyBoxTextureBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 4); - Voxelizer.voxelizerDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 5); - bindlessGBufferBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 6); + lightManager.FSR2WorkaroundRebindUBO(); // binding 3 + taaDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 4); + SkyBoxManager.FSR2WorkaroundRebindUBO(); // binding 5 + Voxelizer.voxelizerDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 6); } } diff --git a/IDKEngine/Source/Render/SSAO.cs b/IDKEngine/Source/Render/SSAO.cs index bce5f5a2..4005d0a6 100644 --- a/IDKEngine/Source/Render/SSAO.cs +++ b/IDKEngine/Source/Render/SSAO.cs @@ -1,6 +1,7 @@ using System; using OpenTK.Mathematics; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -38,7 +39,7 @@ public void Compute() BBG.Cmd.BindImageUnit(Result, 0); BBG.Cmd.UseShaderProgram(shaderProgram); - BBG.Computing.Dispatch((Result.Width + 8 - 1) / 8, (Result.Height + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(Result.Width, 8), MyMath.DivUp(Result.Height, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } diff --git a/IDKEngine/Source/Render/SSR.cs b/IDKEngine/Source/Render/SSR.cs index fe86f055..9bad69bc 100644 --- a/IDKEngine/Source/Render/SSR.cs +++ b/IDKEngine/Source/Render/SSR.cs @@ -1,6 +1,7 @@ using System; using OpenTK.Mathematics; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -40,8 +41,8 @@ public void Compute(BBG.Texture colorTexture) BBG.Cmd.BindImageUnit(Result, 0); BBG.Cmd.BindTextureUnit(colorTexture, 0); BBG.Cmd.UseShaderProgram(shaderProgram); - - BBG.Computing.Dispatch((Result.Width + 8 - 1) / 8, (Result.Height + 8 - 1) / 8, 1); + + BBG.Computing.Dispatch(MyMath.DivUp(Result.Width, 8), MyMath.DivUp(Result.Height, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } diff --git a/IDKEngine/Source/Render/SkyBoxManager.cs b/IDKEngine/Source/Render/SkyBoxManager.cs index 27b9678a..1eb8e08f 100644 --- a/IDKEngine/Source/Render/SkyBoxManager.cs +++ b/IDKEngine/Source/Render/SkyBoxManager.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using BBLogger; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -20,12 +21,12 @@ public enum SkyBoxMode : int public static AtmosphericScatterer AtmosphericScatterer { get; private set; } private static BBG.Texture externalCubemapTexture; - public static BBG.TypedBuffer skyBoxTextureBuffer; + private static BBG.TypedBuffer skyBoxTextureBuffer; private static BBG.AbstractShaderProgram unprojectEquirectangularProgram; public static void Initialize() { skyBoxTextureBuffer = new BBG.TypedBuffer(); - skyBoxTextureBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 4); + FSR2WorkaroundRebindUBO(); skyBoxTextureBuffer.AllocateElements(BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, 1); unprojectEquirectangularProgram = new BBG.AbstractShaderProgram(BBG.AbstractShader.FromFile(BBG.ShaderStage.Compute, "UnprojectEquirectangular/compute.glsl")); @@ -80,6 +81,11 @@ public static SkyBoxMode GetSkyBoxMode() return _skyBoxMode; } + public static void FSR2WorkaroundRebindUBO() + { + skyBoxTextureBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 5); + } + private static unsafe bool LoadSkyBox(string[] imagePaths) { if (imagePaths == null) @@ -136,7 +142,7 @@ private static unsafe bool LoadSkyBoxEquirectangular(string hrdPath) BBG.Cmd.BindTextureUnit(equirectangularTexture, 0); BBG.Cmd.UseShaderProgram(unprojectEquirectangularProgram); - BBG.Computing.Dispatch((externalCubemapTexture.Width + 8 - 1) / 8, (externalCubemapTexture.Width + 8 - 1) / 8, 6); + BBG.Computing.Dispatch(MyMath.DivUp(externalCubemapTexture.Width, 8), MyMath.DivUp(externalCubemapTexture.Height, 8), 6); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); diff --git a/IDKEngine/Source/Render/TAAResolve.cs b/IDKEngine/Source/Render/TAAResolve.cs index ce44b20f..f5cd03b4 100644 --- a/IDKEngine/Source/Render/TAAResolve.cs +++ b/IDKEngine/Source/Render/TAAResolve.cs @@ -1,6 +1,7 @@ using System; using OpenTK.Mathematics; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -46,7 +47,7 @@ public void Compute(BBG.Texture color) BBG.Cmd.BindTextureUnit(color, 1); BBG.Cmd.UseShaderProgram(taaResolveProgram); - BBG.Computing.Dispatch((Result.Width + 8 - 1) / 8, (Result.Height + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(Result.Width, 8), MyMath.DivUp(Result.Height, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } diff --git a/IDKEngine/Source/Render/TonemapAndGammaCorrecter.cs b/IDKEngine/Source/Render/TonemapAndGammaCorrecter.cs index 0dec3980..d1f4bab3 100644 --- a/IDKEngine/Source/Render/TonemapAndGammaCorrecter.cs +++ b/IDKEngine/Source/Render/TonemapAndGammaCorrecter.cs @@ -1,6 +1,7 @@ using System; using OpenTK.Mathematics; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -45,7 +46,7 @@ public void Compute(BBG.Texture texture0 = null, BBG.Texture texture1 = null, BB BBG.Cmd.BindTextureUnit(texture2, 2, texture2 != null); BBG.Cmd.UseShaderProgram(tonemapAndGammaCorrecterProgram); - BBG.Computing.Dispatch((Result.Width + 8 - 1) / 8, (Result.Height + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(Result.Width, 8), MyMath.DivUp(Result.Height, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } diff --git a/IDKEngine/Source/Render/VXGI/ConeTracing/ConeTracer.cs b/IDKEngine/Source/Render/VXGI/ConeTracing/ConeTracer.cs index d0f0d862..3539ae16 100644 --- a/IDKEngine/Source/Render/VXGI/ConeTracing/ConeTracer.cs +++ b/IDKEngine/Source/Render/VXGI/ConeTracing/ConeTracer.cs @@ -1,6 +1,7 @@ using System; using OpenTK.Mathematics; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -43,7 +44,7 @@ public void Compute(BBG.Texture voxelsAlbedo) BBG.Cmd.BindTextureUnit(voxelsAlbedo, 0); BBG.Cmd.UseShaderProgram(shaderProgram); - BBG.Computing.Dispatch((Result.Width + 8 - 1) / 8, (Result.Height + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(Result.Width, 8), MyMath.DivUp(Result.Height, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } diff --git a/IDKEngine/Source/Render/VXGI/Voxelizer/Voxelizer.cs b/IDKEngine/Source/Render/VXGI/Voxelizer/Voxelizer.cs index 098a146d..c685b17f 100644 --- a/IDKEngine/Source/Render/VXGI/Voxelizer/Voxelizer.cs +++ b/IDKEngine/Source/Render/VXGI/Voxelizer/Voxelizer.cs @@ -3,6 +3,7 @@ using OpenTK.Mathematics; using BBLogger; using BBOpenGL; +using IDKEngine.Utils; using IDKEngine.GpuTypes; namespace IDKEngine.Render @@ -95,7 +96,7 @@ public Voxelizer(int width, int height, int depth, Vector3 gridMin, Vector3 grid voxelizerDataBuffer = new BBG.TypedBuffer(); voxelizerDataBuffer.AllocateElements(BBG.Buffer.MemLocation.DeviceLocal, BBG.Buffer.MemAccess.AutoSync, 1); - voxelizerDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 5); + voxelizerDataBuffer.BindToBufferBackedBlock(BBG.Buffer.BufferBackedBlockTarget.Uniform, 6); SetSize(width, height, depth); @@ -126,7 +127,7 @@ private void ClearTextures() } BBG.Cmd.UseShaderProgram(clearTexturesProgram); - BBG.Computing.Dispatch((ResultVoxels.Width + 4 - 1) / 4, (ResultVoxels.Height + 4 - 1) / 4, (ResultVoxels.Depth + 4 - 1) / 4); + BBG.Computing.Dispatch(MyMath.DivUp(ResultVoxels.Width, 4), MyMath.DivUp(ResultVoxels.Height, 4), MyMath.DivUp(ResultVoxels.Depth, 4)); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.ShaderImageAccessBarrierBit); }); } @@ -201,7 +202,7 @@ private void Voxelize(ModelManager modelManager) BBG.Cmd.BindTextureUnit(intermediateResultsRbg[2], 2); BBG.Cmd.UseShaderProgram(mergeIntermediatesProgram); - BBG.Computing.Dispatch((ResultVoxels.Width + 4 - 1) / 4, (ResultVoxels.Height + 4 - 1) / 4, (ResultVoxels.Depth + 4 - 1) / 4); + BBG.Computing.Dispatch(MyMath.DivUp(ResultVoxels.Width, 4), MyMath.DivUp(ResultVoxels.Height, 4), MyMath.DivUp(ResultVoxels.Depth, 4)); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.ShaderImageAccessBarrierBit | BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } @@ -221,7 +222,7 @@ private void Mipmap() Vector3i size = BBG.Texture.GetMipmapLevelSize(ResultVoxels.Width, ResultVoxels.Height, ResultVoxels.Depth, i); BBG.Cmd.BindImageUnit(ResultVoxels, 0, i, true); - BBG.Computing.Dispatch((size.X + 4 - 1) / 4, (size.Y + 4 - 1) / 4, (size.Z + 4 - 1) / 4); + BBG.Computing.Dispatch(MyMath.DivUp(size.X, 4), MyMath.DivUp(size.Y, 4), MyMath.DivUp(size.Z, 4)); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.ShaderImageAccessBarrierBit | BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } @@ -237,7 +238,8 @@ public void DebugRender(BBG.Texture debugResult) BBG.Cmd.BindImageUnit(debugResult, 0); BBG.Cmd.BindTextureUnit(ResultVoxels, 0); BBG.Cmd.UseShaderProgram(visualizeDebugProgram); - BBG.Computing.Dispatch((debugResult.Width + 8 - 1) / 8, (debugResult.Height + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(debugResult.Width, 8), MyMath.DivUp(debugResult.Height, 8), 1); + BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); diff --git a/IDKEngine/Source/Render/VolumetricLighting.cs b/IDKEngine/Source/Render/VolumetricLighting.cs index f8d19120..368f2f1c 100644 --- a/IDKEngine/Source/Render/VolumetricLighting.cs +++ b/IDKEngine/Source/Render/VolumetricLighting.cs @@ -1,6 +1,7 @@ using System; using OpenTK.Mathematics; using BBOpenGL; +using IDKEngine.Utils; namespace IDKEngine.Render { @@ -55,7 +56,6 @@ public unsafe VolumetricLighting(Vector2i size, in GpuSettings settings, float r public void Compute() { - BBG.Computing.Compute("Compute Volumetric Lighting", () => { BBG.Cmd.SetUniforms(Settings); @@ -64,7 +64,7 @@ public void Compute() BBG.Cmd.BindImageUnit(depthTexture, 1); BBG.Cmd.UseShaderProgram(volumetricLightingProgram); - BBG.Computing.Dispatch((volumetricLightingTexture.Width + 8 - 1) / 8, (volumetricLightingTexture.Height + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(volumetricLightingTexture.Width, 8), MyMath.DivUp(volumetricLightingTexture.Height, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); @@ -75,7 +75,7 @@ public void Compute() BBG.Cmd.BindTextureUnit(depthTexture, 1); BBG.Cmd.UseShaderProgram(upscaleProgram); - BBG.Computing.Dispatch((PresentationResolution.X + 8 - 1) / 8, (PresentationResolution.Y + 8 - 1) / 8, 1); + BBG.Computing.Dispatch(MyMath.DivUp(PresentationResolution.X, 8), MyMath.DivUp(PresentationResolution.Y, 8), 1); BBG.Cmd.MemoryBarrier(BBG.Cmd.MemoryBarrierMask.TextureFetchBarrierBit); }); } diff --git a/IDKEngine/Source/Utils/ModelLoader.cs b/IDKEngine/Source/Utils/ModelLoader.cs index c102f521..c29f5b49 100644 --- a/IDKEngine/Source/Utils/ModelLoader.cs +++ b/IDKEngine/Source/Utils/ModelLoader.cs @@ -2017,7 +2017,7 @@ public static BindlessSampledTexture GetWhite() { texture = new GLTexture(GLTexture.Type.Texture2D); texture.Allocate(1, 1, 1, GLTexture.InternalFormat.R16G16B16A16Float); - texture.Clear(GLTexture.PixelFormat.RGBA, GLTexture.PixelType.Float, new Vector4(1.0f)); + texture.Fill(GLTexture.PixelFormat.RGBA, GLTexture.PixelType.Float, new Vector4(1.0f)); cachedWhiteTexture.BindlessHandle = texture.GetTextureHandleARB(sampler); } diff --git a/IDKEngine/Source/Utils/MyMath.cs b/IDKEngine/Source/Utils/MyMath.cs index 0fe000be..0fe14e4a 100644 --- a/IDKEngine/Source/Utils/MyMath.cs +++ b/IDKEngine/Source/Utils/MyMath.cs @@ -5,6 +5,16 @@ namespace IDKEngine.Utils { public static class MyMath { + public static int DivUp(int numerator, int divisor) + { + return (numerator + divisor - 1) / divisor; + } + + public static uint DivUp(uint numerator, uint divisor) + { + return (numerator + divisor - 1) / divisor; + } + public static Vector2 GetHalton2D(int index, int baseA, int baseB) { float x = GetHalton(index + 1, baseA); @@ -133,28 +143,53 @@ public static int NextMultiple(int num, int multiple) return ((num / multiple) + 1) * multiple; } - /// Source: https://www.forceflow.be/2013/10/07/morton-encodingdecoding-through-bit-interleaving-implementations/#For-loop_based_method - private static ulong SplitBy3(uint a) + public static int NextPowerOfTwo(int v) + { + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; + + return v; + } + + /// Source: https://developer.nvidia.com/blog/thinking-parallel-part-iii-tree-construction-gpu/ + + // Expands a 10-bit integer into 30 bits + // by inserting 2 zeros after each bit. + private static unsafe uint ExpandBits(uint v) { - ulong x = a & 0x1fffff; // we only look at the first 21 bits - x = (x | x << 32) & 0x1f00000000ffff; // shift left 32 bits, OR with self, and 00011111000000000000000000000000000000001111111111111111 - x = (x | x << 16) & 0x1f0000ff0000ff; // shift left 32 bits, OR with self, and 00011111000000000000000011111111000000000000000011111111 - x = (x | x << 8) & 0x100f00f00f00f00f; // shift left 32 bits, OR with self, and 0001000000001111000000001111000000001111000000001111000000000000 - x = (x | x << 4) & 0x10c30c30c30c30c3; // shift left 32 bits, OR with self, and 0001000011000011000011000011000011000011000011000011000100000000 - x = (x | x << 2) & 0x1249249249249249; - return x; + unchecked + { + v = (v * 0x00010001u) & 0xFF0000FFu; + v = (v * 0x00000101u) & 0x0F00F00Fu; + v = (v * 0x00000011u) & 0xC30C30C3u; + v = (v * 0x00000005u) & 0x49249249u; + + return v; + } } - public static ulong GetMorton(Vector3 normalizedV) + // Calculates a 30-bit Morton code for the + // given 3D point located within the unit cube [0,1]. + public static uint GetMorton(Vector3 normalizedV) { - const uint max = (1 << 21) - 1; - uint x = Math.Clamp((uint)(normalizedV.X * max), 0u, max); - uint y = Math.Clamp((uint)(normalizedV.Y * max), 0u, max); - uint z = Math.Clamp((uint)(normalizedV.Z * max), 0u, max); + unchecked + { + uint x = Math.Clamp((uint)(normalizedV.X * 1024.0f), 0, 1023); + uint y = Math.Clamp((uint)(normalizedV.Y * 1024.0f), 0, 1023); + uint z = Math.Clamp((uint)(normalizedV.Z * 1024.0f), 0, 1023); - ulong result = 0; - result |= SplitBy3(x) | SplitBy3(y) << 1 | SplitBy3(z) << 2; - return result; + uint xx = ExpandBits(x); + uint yy = ExpandBits(y); + uint zz = ExpandBits(z); + uint result = xx * 4 + yy * 2 + zz; + + return result; + } } public static float DegreesToRadians(float degrees) diff --git a/IDKEngine/Source/Utils/RNG.cs b/IDKEngine/Source/Utils/RNG.cs index 6d3cd69e..61571394 100644 --- a/IDKEngine/Source/Utils/RNG.cs +++ b/IDKEngine/Source/Utils/RNG.cs @@ -5,6 +5,8 @@ namespace IDKEngine.Utils { public static class RNG { + + private static readonly Random rng = new Random(); public static Vector3 RandomVec3() { return RandomVec3(0.0f, 1.0f); @@ -26,12 +28,12 @@ public static float RandomFloat(float min, float max) public static float RandomFloat01() { - return Random.Shared.NextSingle(); + return rng.NextSingle(); } public static int RandomInt(int min, int max) { - return Random.Shared.Next(min, max); + return rng.Next(min, max); } } } diff --git a/Readme.md b/Readme.md index b917d662..1ff16a13 100644 --- a/Readme.md +++ b/Readme.md @@ -268,7 +268,7 @@ Here's an example: // 1. Allocate buffer to store pixels var flags = BufferStorageFlags.MapPersistentBit | BufferStorageFlags.MapWriteBit; GL.CreateBuffers(1, out int stagingBuffer); -GL.NamedBufferStorage(stagingBuffer, imageSize, IntPtr.Zero, flags); +GL.NamedBufferStorage(stagingBuffer, imageSize, null, flags); // 2. Upload pixels into buffer void* bufferMemory = GL.MapNamedBufferRange(stagingBuffer, 0, imageSize, flags); @@ -312,7 +312,7 @@ int imageSize = imageWidth * imageHeight * imageChannels; var flags = BufferStorageFlags.MapPersistentBit | BufferStorageFlags.MapWriteBit; GL.CreateBuffers(1, out int stagingBuffer); -GL.NamedBufferStorage(stagingBuffer, imageSize, IntPtr.Zero, flags); +GL.NamedBufferStorage(stagingBuffer, imageSize, null, flags); void* bufferMemory = GL.MapNamedBufferRange(stagingBuffer, 0, imageSize, flags); Task.Run(() => @@ -323,7 +323,7 @@ Task.Run(() => MainThreadQueue.AddToLazyQueue(() => { GL.BindBuffer(BufferTarget.PixelUnpackBuffer, stagingBuffer); - texture.Upload2D(imageWidth, imageHeight, PixelFormat, PixelType, IntPtr.Zero); + texture.Upload2D(imageWidth, imageHeight, PixelFormat, PixelType, null); GL.DeleteBuffer(stagingBuffer); }); }); @@ -594,7 +594,7 @@ public void Draw() GL.BindVertexArray(vao); // contains merged vertex and indices array + vertex format GL.BindBuffer(BufferTarget.DrawIndirectBuffer, drawCommandBuffer); // contains DrawCommand[Meshes.Length] - GL.MultiDrawElementsIndirect(PrimitiveType.Triangles, DrawElementsType.UnsignedInt, IntPtr.Zero, Meshes.Length, 0); + GL.MultiDrawElementsIndirect(PrimitiveType.Triangles, DrawElementsType.UnsignedInt, null, Meshes.Length, 0); } ``` While this renders all geometry just fine, you might be wondering how to access the entirety of materials to compute proper shading. After all scenes like Sponza come with a lot of textures and the usual method of manually declaring `sampler2D` in glsl quickly becomes insufficient as we can't do state changes between draw calls anymore (which is good) to swap out materials. This is where Bindless Textures comes in. @@ -690,7 +690,7 @@ void Render() GL.BindVertexArray(vao); GL.UseProgram(drawingProgram); GL.BindBuffer(BufferTarget.DrawIndirectBuffer, drawCommandBuffer); - GL.MultiDrawElementsIndirect(PrimitiveType.Triangles, DrawElementsType.UnsignedInt, IntPtr.Zero, Meshes.Length, 0); + GL.MultiDrawElementsIndirect(PrimitiveType.Triangles, DrawElementsType.UnsignedInt, null, Meshes.Length, 0); } ``` A compute shader is dispatched to do the culling and adjust the content of `drawCommandBuffer` accordingly.