Skip to content

Commit 7c7233f

Browse files
committed
textures: work on gpu texture ports
1 parent 19e8c48 commit 7c7233f

File tree

6 files changed

+249
-8
lines changed

6 files changed

+249
-8
lines changed

include/avnd/concepts/gfx.hpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,46 @@ template <typename T>
6666
concept buffer_port = cpu_buffer_port<T> || gpu_buffer_port<T>; // FIXME SSBO
6767

6868
template <typename T>
69-
concept cpu_fixed_format_texture = requires(T t) {
69+
concept cpu_texture_base = requires(T t) {
7070
t.bytes;
7171
t.width;
7272
t.height;
7373
t.changed;
74+
};
75+
template <typename T>
76+
concept cpu_fixed_format_texture = cpu_texture_base<T> && requires(T t) {
7477
typename T::format;
7578
};
7679
template <typename T>
77-
concept cpu_dynamic_format_texture = requires(T t) {
78-
t.bytes;
79-
t.width;
80-
t.height;
81-
t.changed;
82-
t.format = {};
80+
concept cpu_dynamic_format_texture = cpu_texture_base<T> && (requires(T t) {
81+
t.format = {};
82+
} || requires(T t) {
83+
t.request_format;
84+
});
85+
86+
template <typename T>
87+
concept gpu_dynamic_format_texture = requires(T t) {
88+
t.handle;
8389
};
90+
8491
template <typename T>
8592
concept cpu_texture
8693
= cpu_formatted_buffer<T> || cpu_fixed_format_texture<T> || cpu_dynamic_format_texture<T>;
8794

95+
template <typename T>
96+
concept gpu_texture = gpu_dynamic_format_texture<T>;
97+
8898
template <typename T>
8999
concept cpu_texture_port = requires(T t) {
90100
t.texture;
91101
} && (cpu_texture<std::decay_t<decltype(std::declval<T>().texture)>>);
92102

103+
template <typename T>
104+
concept gpu_texture_port = requires(T t) {
105+
t.texture;
106+
} && (gpu_texture<std::decay_t<decltype(std::declval<T>().texture)>>);
107+
108+
93109

94110
template <typename T>
95111
concept sampler_port = requires(T t) { T::sampler(); };
@@ -102,7 +118,7 @@ concept attachment_port = requires { T::attachment(); };
102118

103119
template <typename T>
104120
concept texture_port
105-
= cpu_texture_port<T> || sampler_port<T> || attachment_port<T> || image_port<T>;
121+
= cpu_texture_port<T> || gpu_texture_port<T> || sampler_port<T> || attachment_port<T> || image_port<T>;
106122

107123
template <typename T>
108124
concept matrix_port = buffer_port<T> || texture_port<T>;

include/avnd/introspection/input.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ struct cpu_texture_input_introspection
117117
{
118118
};
119119

120+
template <typename T>
121+
struct gpu_texture_input_introspection
122+
: gpu_texture_port_introspection<typename inputs_type<T>::type>
123+
{
124+
};
125+
120126
template <typename T>
121127
struct matrix_input_introspection
122128
: matrix_port_introspection<typename inputs_type<T>::type>

include/avnd/introspection/output.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ struct cpu_texture_output_introspection
100100
{
101101
};
102102

103+
template <typename T>
104+
struct gpu_texture_output_introspection
105+
: gpu_texture_port_introspection<typename outputs_type<T>::type>
106+
{
107+
};
108+
103109
template <typename T>
104110
struct matrix_output_introspection
105111
: matrix_port_introspection<typename outputs_type<T>::type>

include/avnd/introspection/port.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ using is_cpu_texture_port_t = boost::mp11::mp_bool<cpu_texture_port<Field>>;
134134
template <typename T>
135135
using cpu_texture_port_introspection = predicate_introspection<T, is_cpu_texture_port_t>;
136136

137+
template <typename Field>
138+
using is_gpu_texture_port_t = boost::mp11::mp_bool<gpu_texture_port<Field>>;
139+
template <typename T>
140+
using gpu_texture_port_introspection = predicate_introspection<T, is_gpu_texture_port_t>;
141+
137142
template <typename Field>
138143
using is_matrix_port_t = boost::mp11::mp_bool<matrix_port<Field>>;
139144
template <typename T>

include/halp/texture.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ struct texture_input<lit, r32f_texture>
126126
r32f_texture texture;
127127
};
128128

129+
template <static_string lit>
130+
struct texture_input<lit, custom_texture>
131+
{
132+
static clang_buggy_consteval auto name() { return std::string_view{lit.value}; }
133+
134+
custom_texture texture;
135+
};
136+
137+
template <static_string lit>
138+
struct texture_input<lit, custom_variable_texture>
139+
{
140+
static clang_buggy_consteval auto name() { return std::string_view{lit.value}; }
141+
142+
custom_variable_texture texture;
143+
};
144+
129145
template <static_string lit, typename TextureType = rgba_texture>
130146
struct fixed_texture_input : texture_input<lit, TextureType>
131147
{
@@ -182,4 +198,32 @@ struct texture_output
182198
template <static_string lit>
183199
using rgb_texture_output = texture_output<lit, rgb_texture>;
184200

201+
202+
struct gpu_texture
203+
{
204+
enum format { RGBA8, RGBA16F, RFGBA32F, R8, R16, R16F, R32F } format{RGBA8};
205+
void* handle{};
206+
};
207+
208+
template <static_string lit>
209+
struct gpu_texture_input
210+
{
211+
static clang_buggy_consteval auto name() { return std::string_view{lit.value}; }
212+
213+
operator const halp::gpu_texture&() const noexcept { return texture; }
214+
operator halp::gpu_texture&() noexcept { return texture; }
215+
216+
halp::gpu_texture texture{};
217+
};
218+
219+
template <static_string lit>
220+
struct gpu_texture_output
221+
{
222+
static clang_buggy_consteval auto name() { return std::string_view{lit.value}; }
223+
224+
operator const halp::gpu_texture&() const noexcept { return texture; }
225+
operator halp::gpu_texture&() noexcept { return texture; }
226+
227+
halp::gpu_texture texture{};
228+
};
185229
}

include/halp/texture_formats.hpp

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,168 @@ struct rgb_texture
289289
}
290290
};
291291

292+
293+
struct custom_texture_base
294+
{
295+
using uninitialized_bytes = boost::container::vector<unsigned char>;
296+
unsigned char* bytes;
297+
int width;
298+
int height;
299+
bool changed;
300+
301+
enum texture_format : uint8_t
302+
{
303+
RGBA8,
304+
BGRA8,
305+
R8,
306+
RG8,
307+
R16,
308+
RG16,
309+
RED_OR_ALPHA8,
310+
311+
RGBA16F,
312+
RGBA32F,
313+
R16F,
314+
R32F,
315+
316+
R8UI,
317+
R32UI,
318+
RG32UI,
319+
RGBA32UI,
320+
};
321+
322+
static constexpr int component_size(texture_format format) noexcept
323+
{
324+
switch(format)
325+
{
326+
case RGBA8:
327+
case BGRA8:
328+
return 1;
329+
case R8:
330+
return 1;
331+
case RG8:
332+
return 1;
333+
case R16:
334+
return 2;
335+
case RG16:
336+
return 2;
337+
case RED_OR_ALPHA8:
338+
return 1;
339+
case RGBA16F:
340+
return 2;
341+
case RGBA32F:
342+
return 4;
343+
case R16F:
344+
return 2;
345+
case R32F:
346+
return 4;
347+
case R8UI:
348+
return 1;
349+
case R32UI:
350+
return 4;
351+
case RG32UI:
352+
return 4;
353+
case RGBA32UI:
354+
return 4;
355+
default:
356+
return 1;
357+
}
358+
}
359+
static constexpr int components(texture_format format) noexcept
360+
{
361+
switch(format)
362+
{
363+
case RGBA8:
364+
case BGRA8:
365+
return 4;
366+
case R8:
367+
return 1;
368+
case RG8:
369+
return 2;
370+
case R16:
371+
return 1;
372+
case RG16:
373+
return 2;
374+
case RED_OR_ALPHA8:
375+
return 1;
376+
case RGBA16F:
377+
return 4;
378+
case RGBA32F:
379+
return 4;
380+
case R16F:
381+
return 1;
382+
case R32F:
383+
return 1;
384+
case R8UI:
385+
return 1;
386+
case R32UI:
387+
return 1;
388+
case RG32UI:
389+
return 2;
390+
case RGBA32UI:
391+
return 4;
392+
default:
393+
return 1;
394+
}
395+
}
396+
397+
void update(unsigned char* data, int w, int h) noexcept
398+
{
399+
bytes = data;
400+
width = w;
401+
height = h;
402+
changed = true;
403+
}
404+
};
405+
406+
// Use when you want your plugin to request a texture format
407+
// from the host
408+
struct custom_texture : custom_texture_base
409+
{
410+
using custom_texture_base::component_size;
411+
using custom_texture_base::components;
412+
using custom_texture_base::update;
413+
414+
texture_format request_format = RGBA8;
415+
416+
auto bytesize() const noexcept { return bytes_per_pixel() * width * height; }
417+
auto component_size() const noexcept { return bytes_per_pixel() * width * height; }
418+
int bytes_per_pixel() const noexcept
419+
{
420+
return component_size(request_format) * components(request_format);
421+
}
422+
423+
/* FIXME the allocation should not be managed by the plug-in */
424+
auto allocate(int width, int height)
425+
{
426+
using namespace boost::container;
427+
return uninitialized_bytes(bytesize(), default_init);
428+
}
429+
};
430+
431+
// Use when you want your plugin to receive whatever format
432+
// the host chooses
433+
struct custom_variable_texture : custom_texture_base
434+
{
435+
using custom_texture_base::component_size;
436+
using custom_texture_base::components;
437+
using custom_texture_base::update;
438+
439+
texture_format format = RGBA8;
440+
441+
auto bytesize() const noexcept { return bytes_per_pixel() * width * height; }
442+
auto component_size() const noexcept { return bytes_per_pixel() * width * height; }
443+
int bytes_per_pixel() const noexcept
444+
{
445+
return component_size(format) * components(format);
446+
}
447+
448+
/* FIXME the allocation should not be managed by the plug-in */
449+
auto allocate(int width, int height)
450+
{
451+
using namespace boost::container;
452+
return uninitialized_bytes(bytesize(), default_init);
453+
}
454+
};
455+
292456
}

0 commit comments

Comments
 (0)