Skip to content

Commit

Permalink
[gpu.resource] slight api modifications involving errors
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Oct 30, 2024
1 parent f2e97fa commit 82ec49f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
11 changes: 6 additions & 5 deletions include/tz/gpu/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ namespace tz::gpu
* @brief Write some new data to a resource. The thread will block until the changes are resident GPU-side.
* @param res Resource whose data should be changed.
* @param new_data Region containing new memory for the provided resource.
* @pre new_data.size_bytes() < X, where X is the number of bytes comprising the image's underlying data. Otherwise, the behaviour is undefined.
* @return @ref tz::error_code::invalid_value If the write is too large. The size of the new data + offset must be less than or equal to the resource's size.
*
* If the region of new data is smaller than the total size of the resource's underlying data, then all bytes beyond the new data region will be unchanged and keep their previous state.
*
Expand All @@ -204,7 +204,8 @@ namespace tz::gpu
* - If the image was created with @ref image_type::rgba, then each pixel should be 4 bytes - one for each component (0-255).
* - If the image was created with @ref image_type::depth or @ref image_type::floats, then each pixel should be 4 bytes - a single signed 32-bit float.
*/
void resource_write(resource_handle res, std::span<const std::byte> new_data, std::size_t offset = 0);
tz::error_code resource_write(resource_handle res, std::span<const std::byte> new_data, std::size_t offset = 0);
std::size_t resource_size(resource_handle res);
/**
* @ingroup tz_gpu_resource
* @brief Retrieves the current data within a resource.
Expand All @@ -220,21 +221,21 @@ namespace tz::gpu
*
* This is a helper function which will call @ref resource_write under-the-hood.
*/
void index_buffer_write(resource_handle index_buffer, std::span<const index_t> indices);
tz::error_code index_buffer_write(resource_handle index_buffer, std::span<const index_t> indices);
/**
* @ingroup tz_gpu_resource
* @brief Write draw-indirect-count + commands into a buffer resource.
*
* This is a helper function which will call @ref resource_write under-the-hood.
*/
void draw_buffer_write(resource_handle draw_buffer, std::uint32_t count, std::span<const draw_t> draws);
tz::error_code draw_buffer_write(resource_handle draw_buffer, std::uint32_t count, std::span<const draw_t> draws);
/**
* @ingroup tz_gpu_resource
* @brief Write draw-indirect-count + indexed commands into a buffer resource.
*
* This is a helper function which will call @ref resource_write under-the-hood.
*/
void draw_buffer_indexed_write(resource_handle draw_buffer, std::uint32_t count, std::span<const draw_indexed_t> draws);
tz::error_code draw_buffer_indexed_write(resource_handle draw_buffer, std::uint32_t count, std::span<const draw_indexed_t> draws);
}

#endif // TOPAZ_GPU_RESOURCE_HPP
24 changes: 17 additions & 7 deletions src/tz/gpu/rhi_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,15 +907,25 @@ namespace tz::gpu
return tz::error_code::success;
}

void resource_write(resource_handle resh, std::span<const std::byte> new_data, std::size_t offset)
tz::error_code resource_write(resource_handle resh, std::span<const std::byte> new_data, std::size_t offset)
{
auto& res = resources[resh.peek()];
// update the resource data cpu-side.
if(offset + new_data.size_bytes() > res.data.size())
{
RETERR(tz::error_code::invalid_value, "resource write at offset {} of size {} (total {}) is too large - the resource data is only of size {}", offset, new_data.size_bytes(), (offset + new_data.size_bytes()), res.data.size());
}
tz_assert(offset + new_data.size_bytes() <= res.data.size(), "resource write data span is of wrong size.");
std::copy(new_data.begin(), new_data.end(), res.data.begin() + offset);
// make sure the change is resident GPU-side.
// definitely could cause gpu sync issues if commands are currently in-flight that are reading from it.
impl_write_single_resource(resh);
return tz::error_code::success;
}

std::size_t resource_size(resource_handle resh)
{
return resources[resh.peek()].data.size();
}

std::span<const std::byte> resource_read(resource_handle resh)
Expand All @@ -938,27 +948,27 @@ namespace tz::gpu
tz_error("image resize NYI");
}

void index_buffer_write(resource_handle index_buffer, std::span<const index_t> indices)
tz::error_code index_buffer_write(resource_handle index_buffer, std::span<const index_t> indices)
{
resource_write(index_buffer, std::as_bytes(indices));
return resource_write(index_buffer, std::as_bytes(indices));
}

void draw_buffer_write(resource_handle draw_buffer, std::uint32_t count, std::span<const draw_t> draws)
tz::error_code draw_buffer_write(resource_handle draw_buffer, std::uint32_t count, std::span<const draw_t> draws)
{
std::vector<std::byte> mem(sizeof(std::uint32_t) + draws.size_bytes());
auto cursor = mem.data();
*reinterpret_cast<std::uint32_t*>(cursor) = count;
std::memcpy(cursor + sizeof(std::uint32_t), draws.data(), draws.size_bytes());
resource_write(draw_buffer, mem);
return resource_write(draw_buffer, mem);
}

void draw_buffer_indexed_write(resource_handle draw_buffer, std::uint32_t count, std::span<const draw_indexed_t> draws)
tz::error_code draw_buffer_indexed_write(resource_handle draw_buffer, std::uint32_t count, std::span<const draw_indexed_t> draws)
{
std::vector<std::byte> mem(sizeof(std::uint32_t) + draws.size_bytes());
auto cursor = mem.data();
*reinterpret_cast<std::uint32_t*>(cursor) = count;
std::memcpy(cursor + sizeof(std::uint32_t), draws.data(), draws.size_bytes());
resource_write(draw_buffer, mem);
return resource_write(draw_buffer, mem);
}

std::expected<shader_handle, tz::error_code> create_graphics_shader(std::string_view vertex_source, std::string_view fragment_source)
Expand Down

0 comments on commit 82ec49f

Please sign in to comment.