Array/pointer/method setters
This commit is contained in:
parent
50eca7e379
commit
36172ed649
2 changed files with 118 additions and 21 deletions
|
@ -103,6 +103,7 @@ struct ComputeApplication {
|
|||
void ImportVaapiImage(VulkanTexture &texture0, VulkanTexture &texture1, int fd, uint64_t mod, uint32_t size, uint32_t offset, uint32_t pitch1, uint32_t pitch2, bool p010)
|
||||
{
|
||||
StructureChain layout{$M(VkSubresourceLayout{0},$(rowPitch) = pitch1)};
|
||||
|
||||
StructureChain iinfo{
|
||||
$M(VkImageCreateInfo{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO},
|
||||
$(imageType) = VK_IMAGE_TYPE_2D,
|
||||
|
@ -121,9 +122,10 @@ struct ComputeApplication {
|
|||
$M(VkImageDrmFormatModifierExplicitCreateInfoEXT{VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT},
|
||||
$(drmFormatModifierPlaneCount) = 1,
|
||||
$(drmFormatModifier) = mod,
|
||||
$(pPlaneLayouts) = &layout
|
||||
$(pPlaneLayouts) &= layout
|
||||
)
|
||||
};
|
||||
|
||||
VK_CHECK_RESULT(vkCreateImage(dev.device, &iinfo, NULL, &texture0.image)); // create image.
|
||||
$F(iinfo,$(format) = p010?VK_FORMAT_R16G16_UNORM:VK_FORMAT_R8G8_UNORM, $(extent) = VkExtent3D{ WIDTH/2, HEIGHT/2, 1 });
|
||||
$F(layout,$(offset) = 0, $(rowPitch) = pitch2 );
|
||||
|
|
135
vulkan_utl.h
135
vulkan_utl.h
|
@ -69,36 +69,125 @@ struct StructureChain<T> : T
|
|||
|
||||
// Positional arguments? Im my C++? It's more likely than you think!
|
||||
template<typename T>
|
||||
struct SetterFunc;
|
||||
struct Setter;
|
||||
template<typename T, typename V>
|
||||
struct SetterVal // keeps val reference and SetterFunc wrapper
|
||||
{
|
||||
const SetterFunc<T> &func;
|
||||
// todo: enum?
|
||||
constexpr static bool isarray = false;
|
||||
constexpr static bool isfunc = false;
|
||||
const Setter<T> &member;
|
||||
const V &val;
|
||||
SetterVal(const SetterFunc<T> &f, const V &v) : func(f), val(v){}
|
||||
SetterVal(const Setter<T> &m, const V &v) : member(m), val(v){}
|
||||
};
|
||||
template<typename T, typename V>
|
||||
struct SetterValPtr // keeps val reference and SetterFunc wrapper
|
||||
{
|
||||
constexpr static bool isarray = false;
|
||||
constexpr static bool isfunc = false;
|
||||
const Setter<T> &member;
|
||||
const V *val;
|
||||
SetterValPtr(const Setter<T> &m, const V *v) : member(m), val(v){}
|
||||
};
|
||||
/* Delayed function call (maybe useless)
|
||||
* struct aaa{
|
||||
void Func(int a, int b, int c){printf("%d %d %d\n",a,b,c);}
|
||||
} s;
|
||||
$F(s, $(Func) (10, 20, 30));
|
||||
*/
|
||||
template <typename T, typename... Ts>
|
||||
struct ArgumentList : ArgumentList<Ts...>
|
||||
{
|
||||
const T &v;
|
||||
ArgumentList(const T &t, const Ts&... a) : v(t), ArgumentList<Ts...>(a...){}
|
||||
template<typename Func, typename I, typename... Ts2> void Call(Func f, I &i, const Ts2&... ts) const
|
||||
{
|
||||
ArgumentList<Ts...>::Call(f, i,ts..., v);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct ArgumentList<T>
|
||||
{
|
||||
const T &v;
|
||||
ArgumentList(const T &t) : v(t){}
|
||||
template<typename Func, typename I, typename... Ts2> void Call(Func f, I &i, const Ts2&... ts) const
|
||||
{
|
||||
(i.*f)(ts..., v);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, typename... Vs>
|
||||
struct SetterFunc // keeps val reference and SetterFunc wrapper
|
||||
{
|
||||
constexpr static bool isfunc = true;
|
||||
constexpr static bool isarray = false;
|
||||
const Setter<T> &func;
|
||||
ArgumentList<Vs... > vals;
|
||||
SetterFunc(const Setter<T> &f, const Vs&... vs) : func(f), vals(vs...) {}
|
||||
};
|
||||
/*
|
||||
* auto indexes = Arr(1U,2U,3U,4U);
|
||||
* $(pQueueFamilyIndices)[$(queueFamilyIndexCount)] = indexes
|
||||
* for(int i = 0; i < iinfo.queueFamilyIndexCount;i++)
|
||||
* printf("%d\n", iinfo.pQueueFamilyIndices[i] );
|
||||
*/
|
||||
template<typename T, typename T1>
|
||||
struct SetterArray;
|
||||
|
||||
template<typename T, typename T1, typename V>
|
||||
struct SetterArrayVal // keeps val reference and SetterFunc wrapper
|
||||
{
|
||||
constexpr static bool isarray = true;
|
||||
const SetterArray<T,T1> &arr;
|
||||
const V &val;
|
||||
SetterArrayVal(const SetterArray<T, T1> &f, const V &v) : arr(f), val(v){}
|
||||
};
|
||||
|
||||
template<typename T, typename T1>
|
||||
struct SetterArray // keeps val reference and SetterFunc wrapper
|
||||
{
|
||||
const Setter<T> &member;
|
||||
const Setter<T1> &count;
|
||||
template <typename V>
|
||||
SetterArrayVal<T,T1,V> operator= (const V &v) {return SetterArrayVal<T,T1,V>(*this,v);}
|
||||
SetterArray(const Setter<T> &m, const Setter<T1> &c) : member(m), count(c) {}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct SetterFunc // keeps member pointer getter, wrapper for user-provided lambda
|
||||
struct Setter // keeps member pointer getter, wrapper for user-provided lambda
|
||||
{
|
||||
const T &func;
|
||||
SetterFunc(const T &data)
|
||||
: func(data)
|
||||
{}
|
||||
Setter(const T &data) :func(data){}
|
||||
template <typename V>
|
||||
SetterVal<T,V> operator() (const V &v) {return SetterVal(*this,v);}
|
||||
SetterValPtr<T,V> operator &= (const V &v) {return SetterValPtr<T,V>(*this,&v);}
|
||||
template <typename V>
|
||||
SetterVal<T,V> operator= (const V &v) {return SetterVal(*this,v);}
|
||||
SetterVal<T,V> operator= (const V &v) {return SetterVal<T,V>(*this,v);}
|
||||
template <typename... Vs>
|
||||
SetterFunc<T,Vs...> operator() (const Vs&... v) {return SetterFunc<T,Vs...>(*this,v...);}
|
||||
template <typename T1>
|
||||
SetterArray<T,T1> operator[] (const Setter<T1> &v) {return SetterArray<T,T1>(*this,v);}
|
||||
};
|
||||
|
||||
// macro wrapper for building positional argument setters
|
||||
// todo: remove extra unused copy
|
||||
#define $(k) SetterFunc([](auto a){return &decltype(a)::k;})
|
||||
template <typename T> struct Twrap{using type = T;};
|
||||
#define $(k) Setter([](const auto a){return &decltype(a)::type::k;})
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
void $F(T &t, const Ts&... ts) // fills all SetterVal's member pointers with it's values
|
||||
{
|
||||
auto filler = [](T &t, const auto &arg){
|
||||
t.*arg.func.func(t) = arg.val;
|
||||
if constexpr(arg.isarray)
|
||||
{
|
||||
t.*arg.arr.member.func(Twrap<T>{}) = arg.val.array;
|
||||
t.*arg.arr.count.func(Twrap<T>{}) = arg.val.count;
|
||||
}
|
||||
else if constexpr(arg.isfunc)
|
||||
{
|
||||
arg.vals.Call(arg.func.func(Twrap<T>{}), t);
|
||||
}
|
||||
else
|
||||
t.*arg.member.func(Twrap<T>{}) = arg.val;
|
||||
};
|
||||
(filler(t,ts),...);
|
||||
}
|
||||
|
@ -110,6 +199,19 @@ T $M(T t, const Ts&... ts)
|
|||
return t;
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
struct ArrayInitializer
|
||||
{
|
||||
T array[sizeof... (Args)];
|
||||
constexpr static uint32_t count = sizeof... (Args);
|
||||
};
|
||||
#define ARRAY_WRAPPER(x,y) template <typename... Args> ArrayInitializer<x, Args...> y(const Args&... arguments) { return {arguments...};}
|
||||
template <typename T0, typename... Ts>
|
||||
ArrayInitializer<T0, T0, Ts...> Arr(T0 arg0, Ts... arguments)
|
||||
{
|
||||
return {arg0, arguments...};
|
||||
}
|
||||
|
||||
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
|
||||
struct VulkanContext
|
||||
{
|
||||
|
@ -1127,13 +1229,6 @@ struct VulkanTexture
|
|||
VK_CHECK_RESULT(vkCreateImageView(device, &info, NULL, &view));
|
||||
}
|
||||
};
|
||||
template <typename T, typename... Args>
|
||||
struct ArrayInitializer
|
||||
{
|
||||
T array[sizeof... (Args)];
|
||||
constexpr static uint32_t count = sizeof... (Args);
|
||||
};
|
||||
#define ARRAY_WRAPPER(x,y) template <typename... Args> ArrayInitializer<x, Args...> y(const Args&... arguments) { return {arguments...};}
|
||||
template<class A, class B>
|
||||
struct CompareTypes_w {
|
||||
constexpr static bool res = false;
|
||||
|
@ -1383,7 +1478,7 @@ struct BaseVulkanPipeline
|
|||
else if constexpr(CompareTypes(decltype(arg),const VkPipelineRasterizationStateCreateInfo&))info.pRasterizationState = &arg;
|
||||
else if constexpr(CompareTypes(decltype(arg),const VkPipelineDepthStencilStateCreateInfo&))info.pDepthStencilState = &arg;
|
||||
else if constexpr(CompareTypes(decltype(arg),const VkPipelineColorBlendStateCreateInfo&))info.pColorBlendState = &arg;
|
||||
else static_assert(0, "Invalid type\n");
|
||||
else static_assert(CompareTypes(decltype(arg),const VkPipelineColorBlendStateCreateInfo&), "Invalid type\n");
|
||||
};
|
||||
(filler(info,args),...);
|
||||
info.stageCount = stages.count;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue