more compute mandatory functions

This commit is contained in:
PancakeTAS 2025-06-29 18:08:04 +02:00
parent 2832020d10
commit 013950596c
No known key found for this signature in database
7 changed files with 40 additions and 19 deletions

View file

@ -54,6 +54,17 @@ namespace Vulkan::Core {
/// ///
void begin(); void begin();
///
/// Dispatch a compute command.
///
/// @param x Number of groups in the X dimension
/// @param y Number of groups in the Y dimension
/// @param z Number of groups in the Z dimension
///
/// @throws std::logic_error if the command buffer is not in Recording state
///
void dispatch(uint32_t x, uint32_t y, uint32_t z);
/// ///
/// End recording commands in the command buffer. /// End recording commands in the command buffer.
/// ///
@ -85,7 +96,7 @@ namespace Vulkan::Core {
/// Get the state of the command buffer. /// Get the state of the command buffer.
[[nodiscard]] CommandBufferState getState() const { return *this->state; } [[nodiscard]] CommandBufferState getState() const { return *this->state; }
/// Get the Vulkan handle. /// Get the Vulkan handle.
[[nodiscard]] auto handle() const { *this->commandBuffer; } [[nodiscard]] auto handle() const { return *this->commandBuffer; }
/// Check whether the object is valid. /// Check whether the object is valid.
[[nodiscard]] bool isValid() const { return static_cast<bool>(this->commandBuffer); } [[nodiscard]] bool isValid() const { return static_cast<bool>(this->commandBuffer); }

View file

@ -9,12 +9,6 @@
namespace Vulkan::Core { namespace Vulkan::Core {
/// Enumeration for different types of command pools.
enum class CommandPoolType {
/// Used for compute-type command buffers.
Compute
};
/// ///
/// C++ wrapper class for a Vulkan command pool. /// C++ wrapper class for a Vulkan command pool.
/// ///
@ -26,12 +20,11 @@ namespace Vulkan::Core {
/// Create the command pool. /// Create the command pool.
/// ///
/// @param device Vulkan device /// @param device Vulkan device
/// @param type Type of command pool to create.
/// ///
/// @throws std::invalid_argument if the device is invalid. /// @throws std::invalid_argument if the device is invalid.
/// @throws ls::vulkan_error if object creation fails. /// @throws ls::vulkan_error if object creation fails.
/// ///
CommandPool(const Device& device, CommandPoolType type); CommandPool(const Device& device);
/// Get the Vulkan handle. /// Get the Vulkan handle.
[[nodiscard]] auto handle() const { return *this->commandPool; } [[nodiscard]] auto handle() const { return *this->commandPool; }

View file

@ -1,6 +1,7 @@
#ifndef PIPELINE_HPP #ifndef PIPELINE_HPP
#define PIPELINE_HPP #define PIPELINE_HPP
#include "core/commandbuffer.hpp"
#include "core/shadermodule.hpp" #include "core/shadermodule.hpp"
#include "device.hpp" #include "device.hpp"
@ -28,6 +29,15 @@ namespace Vulkan::Core {
/// ///
Pipeline(const Device& device, const ShaderModule& shader); Pipeline(const Device& device, const ShaderModule& shader);
///
/// Bind the pipeline to a command buffer.
///
/// @param commandBuffer Command buffer to bind the pipeline to.
///
/// @throws std::invalid_argument if the command buffer is invalid.
///
void bind(const CommandBuffer& commandBuffer) const;
/// Get the Vulkan handle. /// Get the Vulkan handle.
[[nodiscard]] auto handle() const { return *this->pipeline; } [[nodiscard]] auto handle() const { return *this->pipeline; }
/// Get the pipeline layout. /// Get the pipeline layout.

View file

@ -44,6 +44,13 @@ void CommandBuffer::begin() {
*this->state = CommandBufferState::Recording; *this->state = CommandBufferState::Recording;
} }
void CommandBuffer::dispatch(uint32_t x, uint32_t y, uint32_t z) {
if (*this->state != CommandBufferState::Recording)
throw std::logic_error("Command buffer is not in Recording state");
vkCmdDispatch(*this->commandBuffer, x, y, z);
}
void CommandBuffer::end() { void CommandBuffer::end() {
if (*this->state != CommandBufferState::Recording) if (*this->state != CommandBufferState::Recording)
throw std::logic_error("Command buffer is not in Recording state"); throw std::logic_error("Command buffer is not in Recording state");

View file

@ -3,21 +3,14 @@
using namespace Vulkan::Core; using namespace Vulkan::Core;
CommandPool::CommandPool(const Device& device, CommandPoolType type) { CommandPool::CommandPool(const Device& device) {
if (!device) if (!device)
throw std::invalid_argument("Invalid Vulkan device"); throw std::invalid_argument("Invalid Vulkan device");
uint32_t familyIdx{};
switch (type) {
case CommandPoolType::Compute:
familyIdx = device.getComputeFamilyIdx();
break;
}
// create command pool // create command pool
const VkCommandPoolCreateInfo desc = { const VkCommandPoolCreateInfo desc = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.queueFamilyIndex = familyIdx .queueFamilyIndex = device.getComputeFamilyIdx()
}; };
VkCommandPool commandPoolHandle{}; VkCommandPool commandPoolHandle{};
auto res = vkCreateCommandPool(device.handle(), &desc, nullptr, &commandPoolHandle); auto res = vkCreateCommandPool(device.handle(), &desc, nullptr, &commandPoolHandle);

View file

@ -53,3 +53,10 @@ Pipeline::Pipeline(const Device& device, const ShaderModule& shader) {
} }
); );
} }
void Pipeline::bind(const CommandBuffer& commandBuffer) const {
if (!commandBuffer)
throw std::invalid_argument("Invalid command buffer");
vkCmdBindPipeline(commandBuffer.handle(), VK_PIPELINE_BIND_POINT_COMPUTE, *this->pipeline);
}

View file

@ -17,7 +17,7 @@ int main() {
const Device device(instance); const Device device(instance);
// prepare render pass // prepare render pass
const Core::CommandPool commandPool(device, Core::CommandPoolType::Compute); const Core::CommandPool commandPool(device);
// prepare shader // prepare shader
const Core::ShaderModule computeShader(device, "shaders/downsample.spv", const Core::ShaderModule computeShader(device, "shaders/downsample.spv",