Skip to content

Commit

Permalink
gb/cgpu/gi: add data alignment helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
pablode committed Jul 31, 2024
1 parent 58b322c commit caffd8f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
14 changes: 5 additions & 9 deletions src/cgpu/impl/Cgpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <volk.h>

#include <gtl/gb/Data.h>
#include <gtl/gb/Fmt.h>
#include <gtl/gb/Log.h>

Expand Down Expand Up @@ -1794,26 +1795,21 @@ 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,
uint32_t missShaderCount,
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;
Expand Down
1 change: 1 addition & 0 deletions src/gb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_library(
gb STATIC
gtl/gb/Data.h
gtl/gb/Fmt.h
gtl/gb/HandleStore.h
gtl/gb/LinearDataStore.h
Expand Down
32 changes: 32 additions & 0 deletions src/gb/gtl/gb/Data.h
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
//

#pragma once

namespace gtl
{
template<std::integral T>
T gbAlignUpwards(T value, T alignment)
{
if (alignment == T(0))
{
return value;
}

return (value + alignment - T(1)) / alignment * alignment;
}
}
3 changes: 2 additions & 1 deletion src/gi/impl/Gi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <gtl/mc/Frontend.h>
#include <gtl/mc/Runtime.h>
#include <gtl/gb/Log.h>
#include <gtl/gb/Data.h>

#include <MaterialXCore/Document.h>

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit caffd8f

Please sign in to comment.