Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libobs/build/libobs.graphics+.h.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "libobs/graphics/blend-entrypoints-fields.c.h"
#include "libobs/graphics/effect-parser-lifecycle-adapter.c.h"
#include "libobs/graphics/effect-parser.c.h"
#include "libobs/graphics/graphics-active-shader-queries.c.h"
#include "libobs/graphics/graphics-device-load-fields.c.h"
#include "libobs/graphics/graphics-device-queries-fields.c.h"
#include "libobs/graphics/graphics-imports.c.h"
Expand Down
25 changes: 25 additions & 0 deletions libobs/graphics/graphics-active-shader-queries.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#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);

gs_shader_t *gs_get_vertex_shader(void)
{
graphics_t *graphics = thread_graphics;

if (!gs_valid("gs_get_vertex_shader"))
return NULL;

return graphics->exports.device_get_vertex_shader(graphics->device);
}

gs_shader_t *gs_get_pixel_shader(void)
{
graphics_t *graphics = thread_graphics;

if (!gs_valid("gs_get_pixel_shader"))
return NULL;

return graphics->exports.device_get_pixel_shader(graphics->device);
}
20 changes: 0 additions & 20 deletions libobs/graphics/graphics.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1026,26 +1026,6 @@ void gs_load_pixelshader(gs_shader_t *pixelshader)
graphics->exports.device_load_pixelshader(graphics->device, pixelshader);
}

gs_shader_t *gs_get_vertex_shader(void)
{
graphics_t *graphics = thread_graphics;

if (!gs_valid("gs_get_vertex_shader"))
return NULL;

return graphics->exports.device_get_vertex_shader(graphics->device);
}

gs_shader_t *gs_get_pixel_shader(void)
{
graphics_t *graphics = thread_graphics;

if (!gs_valid("gs_get_pixel_shader"))
return NULL;

return graphics->exports.device_get_pixel_shader(graphics->device);
}

enum gs_color_space gs_get_color_space(void)
{
graphics_t *graphics = thread_graphics;
Expand Down
22 changes: 22 additions & 0 deletions test/cmocka/build/test-graph.json
Original file line number Diff line number Diff line change
Expand Up @@ -4933,6 +4933,22 @@
],
"requires": "OS_LINUX"
},
"test_graphics_active_shader_queries": {
"sources": [
"test/cmocka/test_graphics_active_shader_queries.c"
],
"libraries": [
"OBS::libobs",
"OBS::native-test-cmocka",
{
"name": "OBS::w32-pthreads",
"requires": "OS_WINDOWS"
}
],
"header_libraries": [
"SIMDe::SIMDe"
]
},
"test_graphics_alias_ordinary": {
"sources": [
"test/cmocka/test_graphics_alias_ordinary.c"
Expand Down Expand Up @@ -23767,6 +23783,12 @@
"test_glyph_atlas": {
"target": "test_glyph_atlas"
},
"test_graphics_active_shader_queries": {
"target": "test_graphics_active_shader_queries",
"command": [
"${target}"
]
},
"test_graphics_alias_ordinary": {
"target": "test_graphics_alias_ordinary",
"command": [
Expand Down
173 changes: 173 additions & 0 deletions test/cmocka/test_graphics_active_shader_queries.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/* Characterize borrowed active shader queries without a GPU. */
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>
#include <string.h>
#include "graphics/graphics-internal.h"
#include "util/base.h"

/* This characterization lands with the extraction, before the Rust swap. */
#if !__has_include("graphics/graphics-active-shader-queries.c.h")
#error "graphics active shader query extraction seam is missing"
#endif

static struct graphics_subsystem graphics;
static int device_tokens[2];
static int shader_tokens[2];
static gs_device_t *expected_device;
static gs_shader_t *vertex_result, *pixel_result;
static unsigned vertex_calls, pixel_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, expected_device);
}

static gs_shader_t *vertex(const gs_device_t *device)
{
assert_ptr_equal(gs_get_context(), &graphics);
assert_ptr_equal(device, expected_device);
vertex_calls++;
return vertex_result;
}

static gs_shader_t *pixel(const gs_device_t *device)
{
assert_ptr_equal(gs_get_context(), &graphics);
assert_ptr_equal(device, expected_device);
pixel_calls++;
return pixel_result;
}

static int setup(void **state)
{
(void)state;
assert_null(gs_get_context());
memset(&graphics, 0, sizeof(graphics));
vertex_calls = pixel_calls = log_count = 0;
vertex_result = pixel_result = NULL;
shader_tokens[0] = 17;
shader_tokens[1] = 29;
message[0] = '\0';
expected_device = (gs_device_t *)&device_tokens[0];
graphics.device = expected_device;
graphics.exports.device_enter_context = context;
graphics.exports.device_leave_context = context;
graphics.exports.device_get_vertex_shader = vertex;
graphics.exports.device_get_pixel_shader = pixel;
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;
/* A NULL graphics handle never establishes a thread context. */
gs_enter_context(NULL);
assert_string_equal(message, "gs_enter_context: Null 'graphics' parameter");
assert_int_equal(log_count, 1);
assert_null(gs_get_context());
for (unsigned i = 0; i < 2; i++) {
assert_null(gs_get_vertex_shader());
assert_string_equal(message, "gs_get_vertex_shader: called while not in a graphics context");
assert_int_equal(log_count, 2 * i + 2);
assert_null(gs_get_pixel_shader());
assert_string_equal(message, "gs_get_pixel_shader: called while not in a graphics context");
assert_int_equal(log_count, 2 * i + 3);
assert_int_equal(vertex_calls, 0);
assert_int_equal(pixel_calls, 0);
/* Rejection precedes reading either mandatory backend callback. */
graphics.exports.device_get_vertex_shader = NULL;
graphics.exports.device_get_pixel_shader = NULL;
}
}

static void callback_results_and_device_routing(void **state)
{
(void)state;
/* Opaque tokens are borrowed identities, never real host shaders/devices. */
for (unsigned device = 0; device < 2; device++) {
expected_device = (gs_device_t *)&device_tokens[device];
graphics.device = expected_device;
gs_enter_context(&graphics);
for (unsigned results = 0; results < 4; results++) {
vertex_result = (results & 1) ? (gs_shader_t *)&shader_tokens[device] : NULL;
pixel_result = (results & 2) ? (gs_shader_t *)&shader_tokens[1 - device] : NULL;
unsigned calls = device * 4 + results;
assert_ptr_equal(gs_get_vertex_shader(), vertex_result);
assert_int_equal(vertex_calls, calls + 1);
assert_int_equal(pixel_calls, calls);
assert_ptr_equal(gs_get_pixel_shader(), pixel_result);
assert_int_equal(vertex_calls, calls + 1);
assert_int_equal(pixel_calls, calls + 1);
assert_int_equal(graphics.ref, 1);
assert_int_equal(shader_tokens[0], 17);
assert_int_equal(shader_tokens[1], 29);
assert_int_equal(log_count, 0);
}
gs_leave_context();
assert_null(gs_get_context());
}
}

static void leaving_context_rejects_then_reentry_dispatches(void **state)
{
(void)state;
vertex_result = (gs_shader_t *)&shader_tokens[0];
pixel_result = (gs_shader_t *)&shader_tokens[1];
gs_enter_context(&graphics);
assert_ptr_equal(gs_get_pixel_shader(), pixel_result);
assert_ptr_equal(gs_get_vertex_shader(), vertex_result);
gs_leave_context();
assert_null(gs_get_vertex_shader());
assert_string_equal(message, "gs_get_vertex_shader: called while not in a graphics context");
assert_null(gs_get_pixel_shader());
assert_string_equal(message, "gs_get_pixel_shader: called while not in a graphics context");
assert_int_equal(log_count, 2);
assert_int_equal(vertex_calls, 1);
assert_int_equal(pixel_calls, 1);
gs_enter_context(&graphics);
assert_ptr_equal(gs_get_vertex_shader(), vertex_result);
assert_ptr_equal(gs_get_pixel_shader(), pixel_result);
assert_int_equal(vertex_calls, 2);
assert_int_equal(pixel_calls, 2);
assert_int_equal(log_count, 2);
}

int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(no_context, setup, teardown),
cmocka_unit_test_setup_teardown(callback_results_and_device_routing, setup, teardown),
cmocka_unit_test_setup_teardown(leaving_context_rejects_then_reentry_dispatches, setup, teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Both target and test are declared in ../build/test-graph.json.
Loading