From caffd8f81da088ec51bb9517144eb326a34d2ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Delgado=20Kr=C3=A4mer?= Date: Tue, 30 Jul 2024 19:05:48 +0200 Subject: [PATCH] gb/cgpu/gi: add data alignment helper function --- src/cgpu/impl/Cgpu.cpp | 14 +++++--------- src/gb/CMakeLists.txt | 1 + src/gb/gtl/gb/Data.h | 32 ++++++++++++++++++++++++++++++++ src/gi/impl/Gi.cpp | 3 ++- 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/gb/gtl/gb/Data.h diff --git a/src/cgpu/impl/Cgpu.cpp b/src/cgpu/impl/Cgpu.cpp index 2044afc9..95efab0e 100644 --- a/src/cgpu/impl/Cgpu.cpp +++ b/src/cgpu/impl/Cgpu.cpp @@ -27,6 +27,7 @@ #include +#include #include #include @@ -1794,11 +1795,6 @@ namespace gtl return true; } - static uint32_t cgpuAlignSize(uint32_t size, uint32_t alignment) - { - return (size + (alignment - 1)) & ~(alignment - 1); - } - static bool cgpuCreateRtPipelineSbt(CgpuIDevice* idevice, CgpuIPipeline* ipipeline, uint32_t groupCount, @@ -1806,14 +1802,14 @@ namespace gtl uint32_t hitGroupCount) { uint32_t handleSize = idevice->properties.shaderGroupHandleSize; - uint32_t alignedHandleSize = cgpuAlignSize(handleSize, idevice->properties.shaderGroupHandleAlignment); + uint32_t alignedHandleSize = gbAlignUpwards(handleSize, idevice->properties.shaderGroupHandleAlignment); - ipipeline->sbtRgen.stride = cgpuAlignSize(alignedHandleSize, idevice->properties.shaderGroupBaseAlignment); + ipipeline->sbtRgen.stride = gbAlignUpwards(alignedHandleSize, idevice->properties.shaderGroupBaseAlignment); ipipeline->sbtRgen.size = ipipeline->sbtRgen.stride; // Special raygen condition: size must be equal to stride ipipeline->sbtMiss.stride = alignedHandleSize; - ipipeline->sbtMiss.size = cgpuAlignSize(missShaderCount * alignedHandleSize, idevice->properties.shaderGroupBaseAlignment); + ipipeline->sbtMiss.size = gbAlignUpwards(missShaderCount * alignedHandleSize, idevice->properties.shaderGroupBaseAlignment); ipipeline->sbtHit.stride = alignedHandleSize; - ipipeline->sbtHit.size = cgpuAlignSize(hitGroupCount * alignedHandleSize, idevice->properties.shaderGroupBaseAlignment); + ipipeline->sbtHit.size = gbAlignUpwards(hitGroupCount * alignedHandleSize, idevice->properties.shaderGroupBaseAlignment); uint32_t firstGroup = 0; uint32_t dataSize = handleSize * groupCount; diff --git a/src/gb/CMakeLists.txt b/src/gb/CMakeLists.txt index d91163a9..c535689a 100644 --- a/src/gb/CMakeLists.txt +++ b/src/gb/CMakeLists.txt @@ -1,5 +1,6 @@ add_library( gb STATIC + gtl/gb/Data.h gtl/gb/Fmt.h gtl/gb/HandleStore.h gtl/gb/LinearDataStore.h diff --git a/src/gb/gtl/gb/Data.h b/src/gb/gtl/gb/Data.h new file mode 100644 index 00000000..a9182605 --- /dev/null +++ b/src/gb/gtl/gb/Data.h @@ -0,0 +1,32 @@ +// +// Copyright (C) 2024 Pablo Delgado Krämer +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#pragma once + +namespace gtl +{ + template + T gbAlignUpwards(T value, T alignment) + { + if (alignment == T(0)) + { + return value; + } + + return (value + alignment - T(1)) / alignment * alignment; + } +} diff --git a/src/gi/impl/Gi.cpp b/src/gi/impl/Gi.cpp index 9d3f4874..cc0a1941 100644 --- a/src/gi/impl/Gi.cpp +++ b/src/gi/impl/Gi.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include @@ -505,7 +506,7 @@ namespace gtl return *totalSize; } - const uint64_t offset = ((*totalSize) + alignment - 1) / alignment * alignment; + const uint64_t offset = gbAlignUpwards(*totalSize, alignment); (*totalSize) = offset + bufferSize;