Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiler errors on newer vulkan implementations #71

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
6 changes: 4 additions & 2 deletions doc/examples/compute_transfer_overlap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ auto main()-> int {
auto t_1 = vuh::copy_async(device_begin(d_y), device_begin(d_y) + tile_size, begin(y));

// We need result to be available to init the copy-back of the second chunk. Hence blocking call.
program({tile_size, a}, vuh::array_view(d_y, tile_size, arr_size)
, vuh::array_view(d_x, tile_size, arr_size));
if(program({tile_size, a}, vuh::array_view(d_y, tile_size, arr_size),
vuh::array_view(d_x, tile_size, arr_size)).is_error()) {
return 1;
}
auto t_2 = vuh::copy_async(device_begin(d_y) + tile_size, device_end(d_y), begin(y) + tile_size);
t_1.wait(); // explicitly wait for the first chunk here (think of staging buffers and destruction order)
}
Expand Down
5 changes: 4 additions & 1 deletion doc/examples/mandelbrot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ auto main()-> int {
using Specs = vuh::typelist<uint32_t, uint32_t>; // width, height of a workgroup
struct Params{uint32_t width; uint32_t height;}; // width, height of an image
auto program = vuh::Program<Specs, Params>(device, "mandelbrot.spv");
program.grid(vuh::div_up(width, 32), vuh::div_up(height, 32))
const auto result = program.grid(vuh::div_up(width, 32), vuh::div_up(height, 32))
.spec(32, 32)({width, height}, mandel); // run the kernel

if(result.is_error())
return (int)result.error();

write_ppm("mandelebrot.ppm", mandel.data(), width, height);

return 0;
Expand Down
5 changes: 4 additions & 1 deletion doc/examples/saxpy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ auto main()-> int {
using Specs = vuh::typelist<uint32_t>;
struct Params{uint32_t size; float a;};
auto program = vuh::Program<Specs, Params>(device, "saxpy.spv"); // define the kernel by linking interface and spir-v implementation
program.grid(128/64).spec(64)({128, a}, d_y, d_x); // run once, wait for completion
const auto result = program.grid(128/64).spec(64)({128, a}, d_y, d_x); // run once, wait for completion
if(result.is_error())
return (int)result.error();

d_y.toHost(begin(y)); // copy data back to host

return 0;
Expand Down
3 changes: 1 addition & 2 deletions src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,11 @@ namespace vuh {
, vk::PipelineCache pipe_cache
, const vk::PipelineShaderStageCreateInfo& shader_stage_info
, vk::PipelineCreateFlags flags
)-> vk::Pipeline
)-> Result<vk::Pipeline>
{
auto pipelineCI = vk::ComputePipelineCreateInfo(flags
, shader_stage_info, pipe_layout);
return createComputePipeline(pipe_cache, pipelineCI, nullptr);

}

/// Detach the current compute command buffer for sync operations and create the new one.
Expand Down
1 change: 1 addition & 0 deletions src/include/vuh/delayed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vuh/resource.hpp>

#include <cassert>
#include <memory>

namespace vuh {
namespace detail{
Expand Down
5 changes: 3 additions & 2 deletions src/include/vuh/device.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "vuh/result.hpp"
#include <vulkan/vulkan.hpp>

#include <vector>
Expand Down Expand Up @@ -45,10 +46,10 @@ namespace vuh {
, vk::PipelineCache pipe_cache
, const vk::PipelineShaderStageCreateInfo& shader_stage_info
, vk::PipelineCreateFlags flags={}
)-> vk::Pipeline;
)-> Result<vk::Pipeline>;
auto instance()-> vuh::Instance& { return _instance; }
auto releaseComputeCmdBuffer()-> vk::CommandBuffer;

private: // helpers
explicit Device(vuh::Instance& instance, vk::PhysicalDevice physdevice
, const std::vector<vk::QueueFamilyProperties>& families);
Expand Down
52 changes: 32 additions & 20 deletions src/include/vuh/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "device.h"
#include "utils.h"
#include "delayed.hpp"
#include "result.hpp"

#include <vulkan/vulkan.hpp>

Expand Down Expand Up @@ -184,7 +185,7 @@ namespace vuh {
o._shader = nullptr; //
}

/// Move assignment. Releases resources allocated for current instance before taking
/// Move assignment. Releases resources allocated for current instance before taking
/// ownership over those of the other instance.
ProgramBase& operator= (ProgramBase&& o) noexcept {
release();
Expand All @@ -193,12 +194,12 @@ namespace vuh {
_dsclayout = o._dsclayout;
_dscpool = o._dscpool;
_dscset = o._dscset;
_pipecache = o._pipecache;
_pipecache = o._pipecache;
_pipelayout = o._pipelayout;
_pipeline = o._pipeline;
_device = o._device;
_batch = o._batch;
_batch = o._batch;

o._shader = nullptr;
return *this;
}
Expand Down Expand Up @@ -318,7 +319,7 @@ namespace vuh {

/// Initialize the pipeline.
/// Specialization constants interface is defined here.
auto init_pipeline()-> void {
auto init_pipeline()-> Result<void> {
auto specEntries = specs2mapentries(_specs);
auto specInfo = vk::SpecializationInfo(uint32_t(specEntries.size()), specEntries.data()
, sizeof(_specs), &_specs);
Expand All @@ -327,7 +328,11 @@ namespace vuh {
auto stageCI = vk::PipelineShaderStageCreateInfo(vk::PipelineShaderStageCreateFlags()
, vk::ShaderStageFlagBits::eCompute
, _shader, "main", &specInfo);
_pipeline = _device.createPipeline(_pipelayout, _pipecache, stageCI);

VUH_TRY(_device.createPipeline(_pipelayout, _pipecache, stageCI), pipeline);
_pipeline = std::move(pipeline);

return vk::Result::eSuccess;
}
protected:
std::tuple<Spec_Ts...> _specs; ///< hold the state of specialization constants between call to specs() and actual pipeline creation
Expand All @@ -353,12 +358,15 @@ namespace vuh {
{}

/// Initialize the pipeline with empty specialialization constants interface.
auto init_pipeline()-> void {
[[nodiscard]] auto init_pipeline()-> Result<void> {
auto stageCI = vk::PipelineShaderStageCreateInfo(vk::PipelineShaderStageCreateFlags()
, vk::ShaderStageFlagBits::eCompute
, _shader, "main", nullptr);

_pipeline = _device.createPipeline(_pipelayout, _pipecache, stageCI);
VUH_TRY(_device.createPipeline(_pipelayout, _pipecache, stageCI), pipeline);
_pipeline = std::move(pipeline);

return vk::Result::eSuccess;
}
}; // class SpecsBase
} // namespace detail
Expand Down Expand Up @@ -418,11 +426,11 @@ namespace vuh {
/// @pre Grid dimensions and specialization constants (if applicable)
/// should be specified before calling this.
template<class... Arrs>
auto bind(const Params& p, Arrs&&... args)-> const Program& {
auto bind(const Params& p, Arrs&&... args)-> Result<const Program&> {
if(!Base::_pipeline){ // handle multiple rebind
init_pipelayout(args...);
Base::alloc_descriptor_sets(args...);
Base::init_pipeline();
VUH_TRY_VOID(Base::init_pipeline())
}
create_command_buffer(p, args...);
return *this;
Expand All @@ -439,17 +447,19 @@ namespace vuh {
/// Run program with provided parameters.
/// @pre grid dimensions should be specified before calling this.
template<class... Arrs>
auto operator()(const Params& params, Arrs&&... args)-> void {
bind(params, args...);
auto operator()(const Params& params, Arrs&&... args)-> Result<void> {
VUH_TRY_VOID(bind(params, args...))
Base::run();

return vk::Result::eSuccess;
}

/// Initiate execution of the program with provided parameters and immidiately return.
/// @return Delayed<Compute> object for synchronization with host.
/// @pre grid dimensions should be specified before callind this.
template<class... Arrs>
auto run_async(const Params& params, Arrs&&... args)-> vuh::Delayed<detail::Compute> {
bind(params, args...);
auto run_async(const Params& params, Arrs&&... args)-> Result<Delayed<detail::Compute>> {
VUH_TRY_VOID(bind(params, args...));
return Base::run_async();
}
private: // helpers
Expand Down Expand Up @@ -519,11 +529,11 @@ namespace vuh {
/// @pre Grid dimensions and specialization constants (if applicable)
/// should be specified before calling this.
template<class... Arrs>
auto bind(Arrs&&... args)-> const Program& {
auto bind(Arrs&&... args)-> Result<const Program&> {
if(!Base::_pipeline){ // handle multiple rebind
Base::init_pipelayout(std::array<vk::PushConstantRange, 0>{}, args...);
Base::alloc_descriptor_sets(args...);
Base::init_pipeline();
VUH_TRY_VOID(Base::init_pipeline());
}
Base::command_buffer_begin(args...);
Base::command_buffer_end();
Expand All @@ -533,17 +543,19 @@ namespace vuh {
/// Run program with provided parameters.
/// @pre grid dimensions should be specified before calling this.
template<class... Arrs>
auto operator()(Arrs&&... args)-> void {
bind(args...);
auto operator()(Arrs&&... args)-> Result<void> {
VUH_TRY_VOID(bind(args...));
Base::run();

return vk::Result::eSuccess;
}

/// Initiate execution of the program with provided parameters and immidiately return.
/// @return Delayed<Compute> object for synchronization with host.
/// @pre grid dimensions should be specified before callind this.
template<class... Arrs>
auto run_async(Arrs&&... args)-> vuh::Delayed<detail::Compute> {
bind(args...);
auto run_async(Arrs&&... args)-> Result<vuh::Delayed<detail::Compute>> {
VUH_TRY_VOID(bind(args...));
return Base::run_async();
}
}; // class Program
Expand Down
162 changes: 162 additions & 0 deletions src/include/vuh/result.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#pragma once

#include <cassert>
#include <utility>
#include <vulkan/vulkan.hpp>
#include <vulkan/vulkan_core.h>

#define VUH_TRY(expression, identifier) \
auto maybe_##identifier = (expression); \
if((maybe_##identifier).is_error()) \
return (maybe_##identifier).error(); \
auto identifier = std::move(maybe_##identifier).value()

#define VUH_TRY_VOID(expression) \
{\
auto result = (expression); \
if(result.is_error())\
return result.error();\
}

#define VUH_TRY_ELSE(expression, identifier, return_value) \
auto maybe_##identifier = (expression); \
if((maybe_##identifier).is_error()) \
return return_value; \
auto identifier = std::move(maybe_##identifier).value();

#define VUH_TRY_TEST(expression, identifier, message) \
auto maybe_##identifier = (expression); \
REQUIRE(!(maybe_##identifier).is_error()); \
auto identifier = std::move(maybe_##identifier).value();


namespace vuh {
template<typename T>
class [[nodiscard]] Result
{
using R = vk::Result;

public:
Result(vk::Result ec)
:_error_code(ec), _no_value{'\0'}
{
//Cannot have success but no value. Use Result<void> for that
assert(ec != R::eSuccess);
}

Result(T value)
:_error_code(R::eSuccess), _maybe_value{std::move(value)}
{}

Result(vk::ResultValue<T> result)
:_error_code(result.result)
{
if(error() == R::eSuccess) {
_maybe_value = std::move(result.value);
} else {
_no_value = '\1';
}
}

Result(Result&& other) noexcept
:_error_code{other._error_code}
{
if(is_error()) {
_no_value = '\2';
} else {
_maybe_value = std::move(other._maybe_value);
}
}

Result& operator=(Result&& other) noexcept
{
_error_code = other._error_code;

if(is_error()) {
_no_value = '\3';
} else {
_maybe_value = std::move(other._maybe_value);
}

return *this;
}

Result(const Result&)=delete;
Result& operator=(const Result&)=delete;

auto error() const -> vk::Result
{
return _error_code;
}

auto is_error() const -> bool
{
return error() != R::eSuccess;
}

auto value() const& -> T {
//FIXME: assert that there is a value?
return _maybe_value;
}

auto value() && -> T {
//FIXME: assert that there is a value?
return std::move(_maybe_value);
}

~Result()
{
if(!is_error())
_maybe_value.~T();
}

private:
R _error_code;

union {
T _maybe_value;
char _no_value;
};
};

template<typename T>
class [[nodiscard]] Result<T&> : public Result<std::reference_wrapper<T>>
{
using Base = Result<std::reference_wrapper<T>>;
public:
using Base::Base;

Result(T& ref)
:Base(std::reference_wrapper<T>{ref})
{}
};

template<>
class [[nodiscard]] Result<void>
{
using R = vk::Result;

public:
Result(vk::Result ec)
:_error_code(ec)
{}

template<typename U>
Result(vk::ResultValue<U> result)
:_error_code(result.result)
{}

auto is_error() const noexcept -> bool
{
return error() != R::eSuccess;
}

auto error() const noexcept -> vk::Result
{
return _error_code;
}

private:
R _error_code;
};
}
Loading