Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
BasedOnStyle: Microsoft
Language: Cpp

IndentWidth: 4
TabWidth: 4
UseTab: Never

ColumnLimit: 100
PointerAlignment: Left

SortIncludes: true
IncludeBlocks: Preserve

NamespaceIndentation: All
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false

ReflowComments: false
BreakBeforeBraces: Attach

32 changes: 32 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Clang Format

on:
pull_request:
branches: [ main ]
push:
branches: [ main ]

jobs:
clang-format:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install clang-format
run: |
sudo apt-get update
sudo apt-get install -y clang-format

- name: Run clang-format check
run: |
git ls-files | grep -E '\.(h|hpp|c|cpp)$' | xargs clang-format -i
if ! git diff --quiet; then
echo "clang-format produced changes. Please run 'cmake --build build --target format' or the equivalent clang-format command locally and commit the results."
git diff
exit 1
fi

16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,19 @@ if(APPLE)
target_link_options(${EXEC_NAME} PRIVATE "LINKER:-no_warn_duplicate_libraries")
endif()

# ----------------------------------------------------------------------
# clang-format helper: cmake --build build --target format
# ----------------------------------------------------------------------
find_program(CLANG_FORMAT_EXECUTABLE NAMES clang-format)

if(CLANG_FORMAT_EXECUTABLE)
add_custom_target(format
COMMAND git ls-files | grep -E "\\.(h|hpp|c|cpp)$" | xargs ${CLANG_FORMAT_EXECUTABLE} -i
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Running clang-format on tracked C/C++ files"
VERBATIM
)
Comment thread
0dm marked this conversation as resolved.
else()
message(STATUS "clang-format not found; 'format' target will not be available.")
endif()

137 changes: 65 additions & 72 deletions include/app/BufferUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,70 @@

namespace sauce {

struct BufferUtils {

static void createBuffer(
const sauce::PhysicalDevice& physicalDevice,
const sauce::LogicalDevice& logicalDevice,
vk::DeviceSize size,
vk::BufferUsageFlags usage,
vk::MemoryPropertyFlags props,
vk::raii::Buffer& buffer,
vk::raii::DeviceMemory& bufferMemory
) {
vk::BufferCreateInfo bufferCreateInfo {
.size = size,
.usage = usage,
.sharingMode = vk::SharingMode::eExclusive,
};

buffer = vk::raii::Buffer { *logicalDevice, bufferCreateInfo };

vk::MemoryRequirements memoryRequirements = buffer.getMemoryRequirements();

vk::MemoryAllocateInfo memoryAllocateInfo {
.allocationSize = memoryRequirements.size,
.memoryTypeIndex = findMemoryType(physicalDevice, memoryRequirements.memoryTypeBits, props),
};

bufferMemory = vk::raii::DeviceMemory{ *logicalDevice, memoryAllocateInfo };
buffer.bindMemory(*bufferMemory, 0);
}

static uint32_t findMemoryType(
const sauce::PhysicalDevice& physicalDevice,
uint32_t typeFilter,
vk::MemoryPropertyFlags properties
) {
vk::PhysicalDeviceMemoryProperties deviceMemoryProps = physicalDevice->getMemoryProperties();

for (uint32_t i = 0; i < deviceMemoryProps.memoryTypeCount; ++i) {
if ((typeFilter & (1 << i)) && (deviceMemoryProps.memoryTypes[i].propertyFlags & properties) == properties)
return i;
}

throw std::runtime_error("Failed to find suitable memory type!");
}

static void copyBuffer(
const sauce::LogicalDevice& logicalDevice,
const vk::raii::CommandPool& commandPool,
const vk::raii::Queue& queue,
vk::raii::Buffer& src,
vk::raii::Buffer& dst,
vk::DeviceSize size
) {
vk::CommandBufferAllocateInfo copyCommandBufferAllocInfo {
.commandPool = commandPool,
.level = vk::CommandBufferLevel::ePrimary,
.commandBufferCount = 1,
struct BufferUtils {
static void createBuffer(const sauce::PhysicalDevice& physicalDevice,
const sauce::LogicalDevice& logicalDevice, vk::DeviceSize size,
vk::BufferUsageFlags usage, vk::MemoryPropertyFlags props,
vk::raii::Buffer& buffer, vk::raii::DeviceMemory& bufferMemory) {
vk::BufferCreateInfo bufferCreateInfo{
.size = size,
.usage = usage,
.sharingMode = vk::SharingMode::eExclusive,
};

buffer = vk::raii::Buffer{*logicalDevice, bufferCreateInfo};

vk::MemoryRequirements memoryRequirements = buffer.getMemoryRequirements();

vk::MemoryAllocateInfo memoryAllocateInfo{
.allocationSize = memoryRequirements.size,
.memoryTypeIndex =
findMemoryType(physicalDevice, memoryRequirements.memoryTypeBits, props),
};

bufferMemory = vk::raii::DeviceMemory{*logicalDevice, memoryAllocateInfo};
buffer.bindMemory(*bufferMemory, 0);
}

static uint32_t findMemoryType(const sauce::PhysicalDevice& physicalDevice,
uint32_t typeFilter, vk::MemoryPropertyFlags properties) {
vk::PhysicalDeviceMemoryProperties deviceMemoryProps =
physicalDevice->getMemoryProperties();

for (uint32_t i = 0; i < deviceMemoryProps.memoryTypeCount; ++i) {
if ((typeFilter & (1 << i)) &&
(deviceMemoryProps.memoryTypes[i].propertyFlags & properties) == properties)
return i;
}

throw std::runtime_error("Failed to find suitable memory type!");
}

static void copyBuffer(const sauce::LogicalDevice& logicalDevice,
const vk::raii::CommandPool& commandPool,
const vk::raii::Queue& queue, vk::raii::Buffer& src,
vk::raii::Buffer& dst, vk::DeviceSize size) {
vk::CommandBufferAllocateInfo copyCommandBufferAllocInfo{
.commandPool = commandPool,
.level = vk::CommandBufferLevel::ePrimary,
.commandBufferCount = 1,
};
vk::raii::CommandBuffer copyCommandBuffer = std::move(
logicalDevice->allocateCommandBuffers(copyCommandBufferAllocInfo).front());

copyCommandBuffer.begin(vk::CommandBufferBeginInfo{
.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
copyCommandBuffer.copyBuffer(src, dst, vk::BufferCopy(0, 0, size));
copyCommandBuffer.end();

queue.submit(
vk::SubmitInfo{
.commandBufferCount = 1,
.pCommandBuffers = &*copyCommandBuffer,
},
nullptr);
queue.waitIdle();
}
};
vk::raii::CommandBuffer copyCommandBuffer = std::move(logicalDevice->allocateCommandBuffers(copyCommandBufferAllocInfo).front());

copyCommandBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
copyCommandBuffer.copyBuffer(src, dst, vk::BufferCopy(0, 0, size));
copyCommandBuffer.end();

queue.submit(vk::SubmitInfo{
.commandBufferCount = 1,
.pCommandBuffers = &*copyCommandBuffer,
}, nullptr);
queue.waitIdle();
}

};

}
} // namespace sauce
Loading
Loading