diff --git a/libobs/build/libobs.graphics+.h.c b/libobs/build/libobs.graphics+.h.c index 8c40f13883..11973ea70c 100644 --- a/libobs/build/libobs.graphics+.h.c +++ b/libobs/build/libobs.graphics+.h.c @@ -7,6 +7,7 @@ #include "libobs/graphics/effect-parser.c.h" #include "libobs/graphics/graphics-device-load-fields.c.h" #include "libobs/graphics/graphics-device-queries-fields.c.h" +#include "libobs/graphics/graphics-gpu-metadata.c.h" #include "libobs/graphics/graphics-imports.c.h" #include "libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h" #include "libobs/graphics/graphics-render-target-setters-fields.c.h" diff --git a/libobs/graphics/graphics-gpu-metadata.c.h b/libobs/graphics/graphics-gpu-metadata.c.h new file mode 100644 index 0000000000..392998f674 --- /dev/null +++ b/libobs/graphics/graphics-gpu-metadata.c.h @@ -0,0 +1,57 @@ +#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_valid(const char *f); + +const char *gs_get_driver_version(void) +{ + graphics_t *graphics = thread_graphics; + + if (!gs_valid("gs_get_driver_version")) + return NULL; + + if (graphics->exports.gpu_get_driver_version) + return (graphics->exports.gpu_get_driver_version()); + else + return NULL; +} + +const char *gs_get_renderer(void) +{ + graphics_t *graphics = thread_graphics; + + if (!gs_valid("gs_get_renderer")) + return NULL; + + if (graphics->exports.gpu_get_renderer) + return (graphics->exports.gpu_get_renderer()); + else + return NULL; +} + +uint64_t gs_get_gpu_dmem(void) +{ + graphics_t *graphics = thread_graphics; + + if (!gs_valid("gs_get_gpu_dmem")) + return 0; + + if (graphics->exports.gpu_get_dmem) + return (graphics->exports.gpu_get_dmem()); + else + return 0; +} + +uint64_t gs_get_gpu_smem(void) +{ + graphics_t *graphics = thread_graphics; + + if (!gs_valid("gs_get_gpu_smem")) + return 0; + + if (graphics->exports.gpu_get_smem) + return (graphics->exports.gpu_get_smem()); + else + return 0; +} diff --git a/libobs/graphics/graphics.c.h b/libobs/graphics/graphics.c.h index cd5a4c0ca1..6f434af7a9 100644 --- a/libobs/graphics/graphics.c.h +++ b/libobs/graphics/graphics.c.h @@ -334,58 +334,6 @@ const char *gs_get_device_name(void) return gs_valid("gs_get_device_name") ? thread_graphics->exports.device_get_name() : NULL; } -const char *gs_get_driver_version(void) -{ - graphics_t *graphics = thread_graphics; - - if (!gs_valid("gs_get_driver_version")) - return NULL; - - if (graphics->exports.gpu_get_driver_version) - return (graphics->exports.gpu_get_driver_version()); - else - return NULL; -} - -const char *gs_get_renderer(void) -{ - graphics_t *graphics = thread_graphics; - - if (!gs_valid("gs_get_renderer")) - return NULL; - - if (graphics->exports.gpu_get_renderer) - return (graphics->exports.gpu_get_renderer()); - else - return NULL; -} - -uint64_t gs_get_gpu_dmem(void) -{ - graphics_t *graphics = thread_graphics; - - if (!gs_valid("gs_get_gpu_dmem")) - return 0; - - if (graphics->exports.gpu_get_dmem) - return (graphics->exports.gpu_get_dmem()); - else - return 0; -} - -uint64_t gs_get_gpu_smem(void) -{ - graphics_t *graphics = thread_graphics; - - if (!gs_valid("gs_get_gpu_smem")) - return 0; - - if (graphics->exports.gpu_get_smem) - return (graphics->exports.gpu_get_smem()); - else - return 0; -} - static inline void reset_immediate_arrays(graphics_t *graphics) { da_init(graphics->verts); diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 7b5dd440da..a3185aed95 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -5284,6 +5284,19 @@ "SIMDe::SIMDe" ] }, + "test_graphics_gpu_metadata": { + "sources": [ + "test/cmocka/test_graphics_gpu_metadata.c" + ], + "libraries": [ + "OBS::libobs", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ] + }, "test_graphics_host_exports": { "sources": [ "test/cmocka/test_graphics_host_exports.c" @@ -24137,6 +24150,12 @@ "${target}" ] }, + "test_graphics_gpu_metadata": { + "target": "test_graphics_gpu_metadata", + "command": [ + "${target}" + ] + }, "test_graphics_host_exports": { "target": "test_graphics_host_exports", "requires": "OS_LINUX" diff --git a/test/cmocka/test_graphics_gpu_metadata.c b/test/cmocka/test_graphics_gpu_metadata.c new file mode 100644 index 0000000000..a2e52b9f08 --- /dev/null +++ b/test/cmocka/test_graphics_gpu_metadata.c @@ -0,0 +1,243 @@ +/* Characterize the public GPU metadata queries against actual libobs without a GPU. */ +#include +#include +#include +#include +#include +#include +#include +#include "graphics/graphics-internal.h" +#include "util/base.h" +#include "util/bmem.h" + +/* The characterization lands with the extraction, before the Rust swap. */ +#if !__has_include("graphics/graphics-gpu-metadata.c.h") +#error "graphics GPU-metadata extraction seam is missing" +#endif + +static struct graphics_subsystem graphics; +static int device_token; +static const char *driver_version, *renderer; +static uint64_t dmem, smem; +static unsigned driver_calls, renderer_calls, dmem_calls, smem_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 const char *query_driver_version(void) +{ + assert_ptr_equal(gs_get_context(), &graphics); + driver_calls++; + return driver_version; +} + +static const char *query_renderer(void) +{ + assert_ptr_equal(gs_get_context(), &graphics); + renderer_calls++; + return renderer; +} + +static uint64_t query_dmem(void) +{ + assert_ptr_equal(gs_get_context(), &graphics); + dmem_calls++; + return dmem; +} + +static uint64_t query_smem(void) +{ + assert_ptr_equal(gs_get_context(), &graphics); + smem_calls++; + return smem; +} + +static void install_callbacks(void) +{ + graphics.exports.gpu_get_driver_version = query_driver_version; + graphics.exports.gpu_get_renderer = query_renderer; + graphics.exports.gpu_get_dmem = query_dmem; + graphics.exports.gpu_get_smem = query_smem; +} + +static void clear_callbacks(void) +{ + graphics.exports.gpu_get_driver_version = NULL; + graphics.exports.gpu_get_renderer = NULL; + graphics.exports.gpu_get_dmem = NULL; + graphics.exports.gpu_get_smem = NULL; +} + +static void assert_calls(unsigned driver, unsigned render, unsigned d, unsigned s) +{ + assert_int_equal(driver_calls, driver); + assert_int_equal(renderer_calls, render); + assert_int_equal(dmem_calls, d); + assert_int_equal(smem_calls, s); +} + +static int setup(void **state) +{ + (void)state; + assert_null(gs_get_context()); + memset(&graphics, 0, sizeof(graphics)); + driver_calls = renderer_calls = dmem_calls = smem_calls = log_count = 0; + message[0] = '\0'; + driver_version = "driver 1.2.3"; + renderer = "renderer X"; + dmem = 1024; + smem = 2048; + graphics.device = (gs_device_t *)&device_token; + graphics.exports.device_enter_context = context; + graphics.exports.device_leave_context = context; + install_callbacks(); + 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 no_context(void **state) +{ + (void)state; + /* With callbacks present or absent, validate context before dispatch. */ + for (unsigned i = 0; i < 2; i++) { + assert_null(gs_get_driver_version()); + assert_string_equal(message, "gs_get_driver_version: called while not in a graphics context"); + assert_int_equal(log_count, 4 * i + 1); + assert_null(gs_get_renderer()); + assert_string_equal(message, "gs_get_renderer: called while not in a graphics context"); + assert_int_equal(log_count, 4 * i + 2); + assert_true(gs_get_gpu_dmem() == 0); + assert_string_equal(message, "gs_get_gpu_dmem: called while not in a graphics context"); + assert_int_equal(log_count, 4 * i + 3); + assert_true(gs_get_gpu_smem() == 0); + assert_string_equal(message, "gs_get_gpu_smem: called while not in a graphics context"); + assert_int_equal(log_count, 4 * i + 4); + assert_calls(0, 0, 0, 0); + clear_callbacks(); + } +} + +static void absent_callbacks(void **state) +{ + (void)state; + gs_enter_context(&graphics); + clear_callbacks(); + /* Each optional backend entry falls back silently to its default. */ + assert_null(gs_get_driver_version()); + assert_null(gs_get_renderer()); + assert_true(gs_get_gpu_dmem() == 0); + assert_true(gs_get_gpu_smem() == 0); + assert_calls(0, 0, 0, 0); + assert_int_equal(log_count, 0); + assert_string_equal(message, ""); +} + +static void each_slot_dispatches_alone(void **state) +{ + (void)state; + gs_enter_context(&graphics); + /* Distinct callbacks prove each entry reads only its own slot, once. */ + assert_ptr_equal(gs_get_driver_version(), driver_version); + assert_calls(1, 0, 0, 0); + assert_ptr_equal(gs_get_renderer(), renderer); + assert_calls(1, 1, 0, 0); + assert_true(gs_get_gpu_dmem() == 1024); + assert_calls(1, 1, 1, 0); + assert_true(gs_get_gpu_smem() == 2048); + assert_calls(1, 1, 1, 1); + + /* Removing one slot leaves the others dispatching. */ + graphics.exports.gpu_get_renderer = NULL; + graphics.exports.gpu_get_dmem = NULL; + assert_ptr_equal(gs_get_driver_version(), driver_version); + assert_null(gs_get_renderer()); + assert_true(gs_get_gpu_dmem() == 0); + assert_true(gs_get_gpu_smem() == 2048); + assert_calls(2, 1, 1, 2); + assert_int_equal(log_count, 0); +} + +static void strings_returned_unchanged(void **state) +{ + (void)state; + static const char empty[] = ""; + char owned[] = "owned by the backend"; + const char *values[] = {NULL, empty, owned, "static renderer"}; + long allocs = bnum_allocs(); + + gs_enter_context(&graphics); + for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); i++) { + driver_version = values[i]; + renderer = values[i]; + /* The very pointer comes back: no copy, no allocation. */ + assert_ptr_equal(gs_get_driver_version(), values[i]); + assert_ptr_equal(gs_get_renderer(), values[i]); + assert_calls(i + 1, i + 1, 0, 0); + } + assert_string_equal(owned, "owned by the backend"); + assert_true(bnum_allocs() == allocs); + assert_int_equal(log_count, 0); +} + +static void memory_values_bit_for_bit(void **state) +{ + (void)state; + const uint64_t values[] = {0, + 1, + UINT64_C(4294967295), + UINT64_C(4294967296), + UINT64_C(0x8000000000000000), + UINT64_C(0x0123456789abcdef), + UINT64_MAX}; + + gs_enter_context(&graphics); + for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); i++) { + dmem = values[i]; + smem = ~values[i]; + assert_true(gs_get_gpu_dmem() == values[i]); + assert_true(gs_get_gpu_smem() == ~values[i]); + assert_calls(0, 0, i + 1, i + 1); + } + assert_int_equal(log_count, 0); + assert_string_equal(message, ""); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(no_context, setup, teardown), + cmocka_unit_test_setup_teardown(absent_callbacks, setup, teardown), + cmocka_unit_test_setup_teardown(each_slot_dispatches_alone, setup, teardown), + cmocka_unit_test_setup_teardown(strings_returned_unchanged, setup, teardown), + cmocka_unit_test_setup_teardown(memory_values_bit_for_bit, setup, teardown), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_graphics_gpu_metadata.cmake b/test/cmocka/tests.d/test_graphics_gpu_metadata.cmake new file mode 100644 index 0000000000..d22c725950 --- /dev/null +++ b/test/cmocka/tests.d/test_graphics_gpu_metadata.cmake @@ -0,0 +1,2 @@ +# Recording-backend GPU metadata characterization; retain through the Rust swap. +# Both target and test are declared in ../build/test-graph.json.