diff --git a/libobs/build/libobs.graphics+.h.c b/libobs/build/libobs.graphics+.h.c index 8c40f13883..17f38ebad4 100644 --- a/libobs/build/libobs.graphics+.h.c +++ b/libobs/build/libobs.graphics+.h.c @@ -9,6 +9,7 @@ #include "libobs/graphics/graphics-device-queries-fields.c.h" #include "libobs/graphics/graphics-imports.c.h" #include "libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h" +#include "libobs/graphics/graphics-indexbuffer-queries.c.h" #include "libobs/graphics/graphics-render-target-setters-fields.c.h" #include "libobs/graphics/graphics-samplerstate-lifecycle-fields.c.h" #include "libobs/graphics/graphics-shader-matrix-queries-fields.c.h" diff --git a/libobs/graphics/graphics-indexbuffer-queries.c.h b/libobs/graphics/graphics-indexbuffer-queries.c.h new file mode 100644 index 0000000000..dcc3783eee --- /dev/null +++ b/libobs/graphics/graphics-indexbuffer-queries.c.h @@ -0,0 +1,28 @@ +#include "libobs/build/libobs-graphics.pch.h" + +/* Private dependencies defined in graphics.c.h, later in the unity group. */ +static THREAD_LOCAL graphics_t *thread_graphics; +static inline bool gs_obj_valid(const void *obj, const char *f, const char *name); +static inline bool gs_valid(const char *f); + +#define ptr_valid(ptr, func) gs_obj_valid(ptr, func, #ptr) +#define gs_valid_p(func, param1) (gs_valid(func) && ptr_valid(param1, func)) + +void *gs_indexbuffer_get_data(const gs_indexbuffer_t *indexbuffer) +{ + if (!gs_valid_p("gs_indexbuffer_get_data", indexbuffer)) + return NULL; + + return thread_graphics->exports.gs_indexbuffer_get_data(indexbuffer); +} + +enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *indexbuffer) +{ + if (!gs_valid_p("gs_indexbuffer_get_type", indexbuffer)) + return (enum gs_index_type)0; + + return thread_graphics->exports.gs_indexbuffer_get_type(indexbuffer); +} + +#undef gs_valid_p +#undef ptr_valid diff --git a/libobs/graphics/graphics.c.h b/libobs/graphics/graphics.c.h index 44f5408fdf..48d3e9349f 100644 --- a/libobs/graphics/graphics.c.h +++ b/libobs/graphics/graphics.c.h @@ -1297,22 +1297,6 @@ void gs_vertexbuffer_flush_direct(gs_vertbuffer_t *vertbuffer, const struct gs_v thread_graphics->exports.gs_vertexbuffer_flush_direct(vertbuffer, data); } -void *gs_indexbuffer_get_data(const gs_indexbuffer_t *indexbuffer) -{ - if (!gs_valid_p("gs_indexbuffer_get_data", indexbuffer)) - return NULL; - - return thread_graphics->exports.gs_indexbuffer_get_data(indexbuffer); -} - -enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *indexbuffer) -{ - if (!gs_valid_p("gs_indexbuffer_get_type", indexbuffer)) - return (enum gs_index_type)0; - - return thread_graphics->exports.gs_indexbuffer_get_type(indexbuffer); -} - bool gs_is_monitor_hdr(void *monitor) { if (!gs_valid("gs_is_monitor_hdr")) diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 8a3dffd34e..b8d94db8aa 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -5307,6 +5307,22 @@ } ] }, + "test_graphics_indexbuffer_queries": { + "sources": [ + "test/cmocka/test_graphics_indexbuffer_queries.c" + ], + "libraries": [ + "OBS::libobs", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "header_libraries": [ + "SIMDe::SIMDe" + ] + }, "test_graphics_linear_srgb": { "sources": [ "test/cmocka/test_graphics_linear_srgb.c" @@ -23827,6 +23843,12 @@ "${target}" ] }, + "test_graphics_indexbuffer_queries": { + "target": "test_graphics_indexbuffer_queries", + "command": [ + "${target}" + ] + }, "test_graphics_linear_srgb": { "target": "test_graphics_linear_srgb", "command": [ diff --git a/test/cmocka/test_graphics_indexbuffer_queries.c b/test/cmocka/test_graphics_indexbuffer_queries.c new file mode 100644 index 0000000000..7cb35f505a --- /dev/null +++ b/test/cmocka/test_graphics_indexbuffer_queries.c @@ -0,0 +1,162 @@ +/* Characterize index-buffer data/type queries through a GPU-free backend. */ +#include +#include +#include +#include +#include +#include +#include "graphics/graphics-internal.h" +#include "util/base.h" + +/* This characterization lands with the extraction, before the Rust swap. */ +#if !__has_include("graphics/graphics-indexbuffer-queries.c.h") +#error "graphics indexbuffer query extraction seam is missing" +#endif + +static struct graphics_subsystem graphics; +static int device_token, buffer_tokens[2], data_tokens[2]; +static const gs_indexbuffer_t *expected_buffer; +static void *backend_data; +static enum gs_index_type backend_type; +static unsigned data_calls, type_calls, log_count; +static char message[160]; +static log_handler_t previous_handler; +static void *previous_param; + +#ifdef __GNUC__ +__attribute__((format(printf, 2, 0))) +#endif +static void capture(int level, const char *format, va_list args, void *param) +{ + (void)param; + assert_int_equal(level, LOG_DEBUG); + log_count++; + vsnprintf(message, sizeof(message), format, args); +} + +static void context(gs_device_t *device) +{ + assert_ptr_equal(device, &device_token); +} + +static void *get_data(const gs_indexbuffer_t *buffer) +{ + assert_ptr_equal(gs_get_context(), &graphics); + assert_ptr_equal(buffer, expected_buffer); + data_calls++; + return backend_data; +} + +static enum gs_index_type get_type(const gs_indexbuffer_t *buffer) +{ + assert_ptr_equal(gs_get_context(), &graphics); + assert_ptr_equal(buffer, expected_buffer); + type_calls++; + return backend_type; +} + +static int setup(void **state) +{ + (void)state; + assert_null(gs_get_context()); + memset(&graphics, 0, sizeof(graphics)); + data_calls = type_calls = log_count = 0; + message[0] = '\0'; + expected_buffer = NULL; + backend_data = NULL; + backend_type = GS_UNSIGNED_SHORT; + graphics.device = (gs_device_t *)&device_token; + graphics.exports.device_enter_context = context; + graphics.exports.device_leave_context = context; + graphics.exports.gs_indexbuffer_get_data = get_data; + graphics.exports.gs_indexbuffer_get_type = get_type; + assert_int_equal(pthread_mutex_init(&graphics.mutex, NULL), 0); + base_get_log_handler(&previous_handler, &previous_param); + base_set_log_handler(capture, NULL); + return 0; +} + +static int teardown(void **state) +{ + (void)state; + if (gs_get_context()) + gs_leave_context(); + base_set_log_handler(previous_handler, previous_param); + assert_int_equal(pthread_mutex_destroy(&graphics.mutex), 0); + return 0; +} + +static void diagnostic(const char *name, const char *reason, unsigned expected_logs) +{ + char expected[160]; + snprintf(expected, sizeof(expected), "%s: %s", name, reason); + assert_string_equal(message, expected); + assert_int_equal(log_count, expected_logs); + assert_int_equal(data_calls, 0); + assert_int_equal(type_calls, 0); +} + +static void context_before_pointers(void **state) +{ + (void)state; + const gs_indexbuffer_t *buffer = (const gs_indexbuffer_t *)&buffer_tokens[0]; + for (unsigned nonnull = 0; nonnull < 2; nonnull++) { + assert_null(gs_indexbuffer_get_data(nonnull ? buffer : NULL)); + diagnostic("gs_indexbuffer_get_data", "called while not in a graphics context", nonnull * 2 + 1); + /* Characterized, not endorsed: the fallback is the C enum cast zero. */ + assert_int_equal(gs_indexbuffer_get_type(nonnull ? buffer : NULL), (enum gs_index_type)0); + diagnostic("gs_indexbuffer_get_type", "called while not in a graphics context", nonnull * 2 + 2); + } +} + +static void pointer_guards(void **state) +{ + (void)state; + gs_enter_context(&graphics); + assert_null(gs_indexbuffer_get_data(NULL)); + diagnostic("gs_indexbuffer_get_data", "Null 'indexbuffer' parameter", 1); + assert_int_equal(gs_indexbuffer_get_type(NULL), (enum gs_index_type)0); + diagnostic("gs_indexbuffer_get_type", "Null 'indexbuffer' parameter", 2); +} + +static void backend_results_and_buffer_routing(void **state) +{ + (void)state; + const enum gs_index_type types[] = {GS_UNSIGNED_SHORT, GS_UNSIGNED_LONG}; + void *const data[] = {NULL, &data_tokens[0], &data_tokens[1]}; + gs_enter_context(&graphics); + for (unsigned buffer = 0; buffer < 2; buffer++) { + expected_buffer = (const gs_indexbuffer_t *)&buffer_tokens[buffer]; + for (unsigned i = 0; i < sizeof(data) / sizeof(data[0]); i++) { + backend_data = data[i]; + unsigned previous_types = type_calls; + unsigned previous_data = data_calls; + /* A NULL backend result is successful forwarding, without a diagnostic. */ + assert_ptr_equal(gs_indexbuffer_get_data(expected_buffer), data[i]); + assert_int_equal(data_calls, previous_data + 1); + assert_int_equal(type_calls, previous_types); + assert_int_equal(log_count, 0); + } + for (unsigned i = 0; i < sizeof(types) / sizeof(types[0]); i++) { + backend_type = types[i]; + unsigned previous_data = data_calls; + unsigned previous_types = type_calls; + assert_int_equal(gs_indexbuffer_get_type(expected_buffer), types[i]); + assert_int_equal(type_calls, previous_types + 1); + assert_int_equal(data_calls, previous_data); + assert_int_equal(log_count, 0); + } + } + assert_int_equal(data_calls, 6); + assert_int_equal(type_calls, 4); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(context_before_pointers, setup, teardown), + cmocka_unit_test_setup_teardown(pointer_guards, setup, teardown), + cmocka_unit_test_setup_teardown(backend_results_and_buffer_routing, setup, teardown), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_graphics_indexbuffer_queries.cmake b/test/cmocka/tests.d/test_graphics_indexbuffer_queries.cmake new file mode 100644 index 0000000000..097d1362a9 --- /dev/null +++ b/test/cmocka/tests.d/test_graphics_indexbuffer_queries.cmake @@ -0,0 +1 @@ +# Both target and test are declared in ../build/test-graph.json.