Skip to content

Commit cbf3bde

Browse files
committed
Fix minimal uniform buffer offset alignment
1 parent b6db0d7 commit cbf3bde

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

pathfinder/core/d3d9/renderer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ void RendererD3D9::update_tile_batch_storage(uint32_t new_tile_batch_count) {
8888
}
8989

9090
// Create a new uniform buffer.
91-
tile_ub_id = allocator->allocate_buffer(new_tile_batch_count * sizeof(TileUniformD3d9),
92-
BufferType::Uniform,
93-
"tile uniform buffer");
91+
tile_ub_id =
92+
allocator->allocate_buffer(new_tile_batch_count * device->get_aligned_uniform_size(sizeof(TileUniformD3d9)),
93+
BufferType::Uniform,
94+
"tile uniform buffer");
9495

9596
for (int i = tile_batch_storage_count; i < new_tile_batch_count; i++) {
9697
// Set descriptor set.
@@ -488,7 +489,7 @@ void RendererD3D9::upload_and_draw_tiles(const std::vector<DrawTileBatchD3D9> &t
488489
z_buffer_texture_id,
489490
encoder,
490491
tile_descriptor_sets[tile_batch_idx],
491-
tile_batch_idx * sizeof(TileUniformD3d9));
492+
tile_batch_idx * device->get_aligned_uniform_size(sizeof(TileUniformD3d9)));
492493

493494
tile_batch_idx++;
494495
}

pathfinder/gpu/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class Device : public std::enable_shared_from_this<Device> {
6565

6666
virtual std::shared_ptr<Fence> create_fence(const std::string &label) = 0;
6767

68+
virtual size_t get_aligned_uniform_size(size_t original_size) = 0;
69+
6870
BackendType get_backend_type() const {
6971
return backend_type;
7072
}

pathfinder/gpu/gl/device.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace Pathfinder {
1313

1414
DeviceGl::DeviceGl() {
1515
backend_type = BackendType::Opengl;
16+
17+
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &min_uniform_alignment_);
1618
}
1719

1820
std::shared_ptr<Framebuffer> DeviceGl::create_framebuffer(const std::shared_ptr<RenderPass> &render_pass,
@@ -106,4 +108,9 @@ std::shared_ptr<Fence> DeviceGl::create_fence(const std::string &label) {
106108
return fence_gl;
107109
}
108110

111+
size_t DeviceGl::get_aligned_uniform_size(size_t original_size) {
112+
GLint aligned_size = (original_size + min_uniform_alignment_ - 1) & ~(min_uniform_alignment_ - 1);
113+
return aligned_size;
114+
}
115+
109116
} // namespace Pathfinder

pathfinder/gpu/gl/device.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class DeviceGl : public Device {
5555
const std::string &label) override;
5656

5757
std::shared_ptr<Fence> create_fence(const std::string &label) override;
58+
59+
size_t get_aligned_uniform_size(size_t original_size) override;
60+
61+
private:
62+
GLint min_uniform_alignment_{};
5863
};
5964

6065
} // namespace Pathfinder

pathfinder/gpu/vk/device.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ DeviceVk::DeviceVk(VkDevice vk_device,
2626
: vk_physical_device_(vk_physical_device), vk_device_(vk_device), vk_graphics_queue_(vk_graphics_queue),
2727
vk_present_queue_(vk_present_queue), vk_command_pool_(vk_command_pool) {
2828
backend_type = BackendType::Vulkan;
29+
30+
VkPhysicalDeviceProperties props;
31+
vkGetPhysicalDeviceProperties(vk_physical_device, &props);
32+
min_uniform_alignment_ = props.limits.minUniformBufferOffsetAlignment;
2933
}
3034

3135
VkDevice DeviceVk::get_device() const {
@@ -761,4 +765,9 @@ void DeviceVk::copy_data_from_mappable_memory(void *dst, VkDeviceMemory buffer_m
761765
vkUnmapMemory(vk_device_, buffer_memory);
762766
}
763767

768+
size_t DeviceVk::get_aligned_uniform_size(size_t original_size) {
769+
VkDeviceSize aligned_size = (original_size + min_uniform_alignment_ - 1) & ~(min_uniform_alignment_ - 1);
770+
return aligned_size;
771+
}
772+
764773
} // namespace Pathfinder

pathfinder/gpu/vk/device.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class DeviceVk : public Device {
8888
VkDeviceSize src_offset = 0,
8989
VkDeviceSize dst_offset = 0) const;
9090

91+
size_t get_aligned_uniform_size(size_t original_size) override;
92+
9193
private:
9294
/// The graphics card that we'll end up selecting will be stored in a VkPhysicalDevice handle.
9395
VkPhysicalDevice vk_physical_device_{};
@@ -100,6 +102,8 @@ class DeviceVk : public Device {
100102

101103
VkCommandPool vk_command_pool_{};
102104

105+
VkDeviceSize min_uniform_alignment_{};
106+
103107
VkShaderModule create_shader_module(const std::vector<char> &code);
104108

105109
void create_vk_image(uint32_t width,

0 commit comments

Comments
 (0)