Skip to content

Commit f1b2f2d

Browse files
committed
gpu: improve array to buffer and buffer to array with typing
1 parent 787a370 commit f1b2f2d

File tree

2 files changed

+91
-9
lines changed

2 files changed

+91
-9
lines changed

examples/Gpu/ArrayToBuffer.hpp

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,70 @@ struct ArrayToBuffer
2020
halp_meta(author, "ossia score")
2121
halp_meta(uuid, "3127c11b-61d3-4d5e-a896-8b56e628dfbc")
2222

23+
enum Type
24+
{
25+
Float32,
26+
Float64,
27+
SInt32,
28+
UInt32,
29+
SInt16,
30+
UInt16,
31+
SInt8,
32+
UInt8,
33+
};
34+
2335
struct
2436
{
2537
halp::val_port<"Input", std::vector<float>> main;
38+
halp::enum_t<Type, "Type"> type;
2639
} inputs;
2740

2841
struct
2942
{
3043
halp::cpu_buffer_output<"Output"> main;
3144
} outputs;
3245

46+
template <typename T>
47+
void process()
48+
{
49+
if constexpr(std::is_same_v<T, decltype(inputs.main.value)::value_type>)
50+
{
51+
// Direct reuse of the buffer
52+
outputs.main.buffer.raw_data = (unsigned char*)inputs.main.value.data();
53+
outputs.main.buffer.byte_size = inputs.main.value.size() * sizeof(float);
54+
outputs.main.buffer.byte_offset = 0;
55+
outputs.main.buffer.changed = true;
56+
}
57+
else
58+
{
59+
// Allocation and copy :'(
60+
auto span = outputs.main.create<T>(inputs.main.value.size());
61+
std::copy_n(inputs.main.value.data(), inputs.main.value.size(), span.data());
62+
outputs.main.buffer.changed = true;
63+
}
64+
}
65+
3366
void operator()() noexcept
3467
{
35-
const int num_elements = inputs.main.value.size();
36-
outputs.main.buffer.raw_data = (unsigned char*)inputs.main.value.data();
37-
outputs.main.buffer.byte_size = inputs.main.value.size() * sizeof(float);
38-
outputs.main.buffer.byte_offset = 0;
39-
outputs.main.buffer.changed = true;
68+
switch(inputs.type)
69+
{
70+
case Type::Float32:
71+
return process<float>();
72+
case Type::Float64:
73+
return process<double>();
74+
case Type::SInt32:
75+
return process<int32_t>();
76+
case Type::UInt32:
77+
return process<uint32_t>();
78+
case Type::SInt16:
79+
return process<int16_t>();
80+
case Type::UInt16:
81+
return process<uint16_t>();
82+
case Type::SInt8:
83+
return process<int8_t>();
84+
case Type::UInt8:
85+
return process<uint8_t>();
86+
}
4087
}
4188
};
4289
}

examples/Gpu/BufferToArray.hpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,58 @@ struct BufferToArray
1818
halp_meta(manual_url, "https://ossia.io/score-docs/processes/buffer_to_array.html")
1919
halp_meta(author, "ossia score")
2020
halp_meta(uuid, "5274a895-8232-4e79-bc7a-50c27274bd10")
21+
enum Type
22+
{
23+
Float32,
24+
Float64,
25+
SInt32,
26+
UInt32,
27+
SInt16,
28+
UInt16,
29+
SInt8,
30+
UInt8,
31+
};
2132

2233
struct
2334
{
2435
halp::cpu_buffer_input<"Input"> main;
36+
halp::enum_t<Type, "Type"> type;
2537
} inputs;
2638

2739
struct
2840
{
2941
halp::val_port<"Output", std::vector<float>> main;
3042
} outputs;
3143

32-
void operator()() noexcept
44+
template <typename T>
45+
void process()
3346
{
3447
auto sz = inputs.main.buffer.byte_size;
35-
if((sz % sizeof(float)) == 0) {
36-
outputs.main.value.resize(sz / sizeof(float));
37-
memcpy(outputs.main.value.data(), inputs.main.buffer.raw_data, sz);
48+
auto elements = sz / sizeof(T);
49+
auto ptr = (T*)inputs.main.buffer.raw_data;
50+
outputs.main.value.assign(ptr, ptr + elements);
51+
}
52+
53+
void operator()() noexcept
54+
{
55+
switch(inputs.type)
56+
{
57+
case Type::Float32:
58+
return process<float>();
59+
case Type::Float64:
60+
return process<double>();
61+
case Type::SInt32:
62+
return process<int32_t>();
63+
case Type::UInt32:
64+
return process<uint32_t>();
65+
case Type::SInt16:
66+
return process<int16_t>();
67+
case Type::UInt16:
68+
return process<uint16_t>();
69+
case Type::SInt8:
70+
return process<int8_t>();
71+
case Type::UInt8:
72+
return process<uint8_t>();
3873
}
3974
}
4075
};

0 commit comments

Comments
 (0)