obs_init_graphics之gs_create
2024-04-26
54
0
gs_create函数实现功能如下:
- 1.os_dlopen加载对应的DLL导内存,即LoadLibray
- 2.GetProcessAddress获取所有导出函数指针
- 3.调用device_create函数,模块内部初始化
- 4.graphics_init OBS渲染用到的资源初始化
int gs_create(graphics_t **pgraphics, const char *module, uint32_t adapter)
{
int errcode = GS_ERROR_FAIL;
graphics_t *graphics = bzalloc(sizeof(struct graphics_subsystem));
pthread_mutex_init_value(&graphics->mutex);
pthread_mutex_init_value(&graphics->effect_mutex);
//Loadlibray
graphics->module = os_dlopen(module);
if (!graphics->module) {
errcode = GS_ERROR_MODULE_NOT_FOUND;
goto error;
}
//2.GetProcessAddress
if (!load_graphics_imports(&graphics->exports, graphics->module, module))
goto error;
//调用d3d11的device_create函数,模块内部初始化
errcode = graphics->exports.device_create(&graphics->device, adapter);
if (errcode != GS_SUCCESS)
goto error;
//其它资源初始化
if (!graphics_init(graphics)) {
errcode = GS_ERROR_FAIL;
goto error;
}
*pgraphics = graphics;
return errcode;
error:
gs_destroy(graphics);
return errcode;
}
graphics_t结构体
typedef struct graphics_subsystem graphics_t;
重新typedef了,不要被名不同而误导。
struct graphics_subsystem {
void *module;
gs_device_t *device; //d3d11相关
struct gs_exports exports; //导出函数集
DARRAY(struct gs_rect) viewport_stack;
DARRAY(struct matrix4) matrix_stack;
size_t cur_matrix;
struct matrix4 projection;
struct gs_effect *cur_effect;
gs_vertbuffer_t *sprite_buffer;
bool using_immediate;
struct gs_vb_data *vbd;
gs_vertbuffer_t *immediate_vertbuffer;
DARRAY(struct vec3) verts;
DARRAY(struct vec3) norms;
DARRAY(uint32_t) colors;
DARRAY(struct vec2) texverts[16];
pthread_mutex_t effect_mutex;
struct gs_effect *first_effect;
pthread_mutex_t mutex;
volatile long ref;
struct blend_state cur_blend_state;
DARRAY(struct blend_state) blend_state_stack;
bool linear_srgb;
};
DLL导出函数
OBS的核心结构模块图如下:
从上图可知,其导出的DLL函数保存在graphics->exports结构体中。
其关于视频处理的参数及函数都是由struct obs_core_video video成员来保存。而对于图形处理由是由统一的graphcis.c来实现,其根据struct obs_video_info ovi;传递的模块名加载不同的dll,可以是opengl的,也可以是d3d11的。
这其中关于graphics.h导出的函数包括有:
#define GRAPHICS_IMPORT(func) \
do { \
exports->func = os_dlsym(module, #func); \
if (!exports->func) { \
success = false; \
blog(LOG_ERROR, \
"Could not load function '%s' from " \
"module '%s'", \
#func, module_name); \
} \
} while (false)
#define GRAPHICS_IMPORT_OPTIONAL(func) \
do { \
exports->func = os_dlsym(module, #func); \
} while (false)
bool load_graphics_imports(struct gs_exports *exports, void *module,
const char *module_name)
{
bool success = true;
GRAPHICS_IMPORT(device_get_name);
GRAPHICS_IMPORT(device_get_type);
GRAPHICS_IMPORT_OPTIONAL(device_enum_adapters);
GRAPHICS_IMPORT(device_preprocessor_name);
GRAPHICS_IMPORT(device_create);
GRAPHICS_IMPORT(device_destroy);
GRAPHICS_IMPORT(device_enter_context);
GRAPHICS_IMPORT(device_leave_context);
GRAPHICS_IMPORT(device_get_device_obj);
GRAPHICS_IMPORT(device_swapchain_create);
GRAPHICS_IMPORT(device_resize);
GRAPHICS_IMPORT(device_get_color_space);
GRAPHICS_IMPORT(device_update_color_space);
GRAPHICS_IMPORT(device_get_size);
GRAPHICS_IMPORT(device_get_width);
GRAPHICS_IMPORT(device_get_height);
GRAPHICS_IMPORT(device_texture_create);
GRAPHICS_IMPORT(device_cubetexture_create);
GRAPHICS_IMPORT(device_voltexture_create);
GRAPHICS_IMPORT(device_zstencil_create);
GRAPHICS_IMPORT(device_stagesurface_create);
GRAPHICS_IMPORT(device_samplerstate_create);
GRAPHICS_IMPORT(device_vertexshader_create);
GRAPHICS_IMPORT(device_pixelshader_create);
GRAPHICS_IMPORT(device_vertexbuffer_create);
GRAPHICS_IMPORT(device_indexbuffer_create);
GRAPHICS_IMPORT(device_timer_create);
GRAPHICS_IMPORT(device_timer_range_create);
GRAPHICS_IMPORT(device_get_texture_type);
GRAPHICS_IMPORT(device_load_vertexbuffer);
GRAPHICS_IMPORT(device_load_indexbuffer);
GRAPHICS_IMPORT(device_load_texture);
GRAPHICS_IMPORT(device_load_samplerstate);
GRAPHICS_IMPORT(device_load_vertexshader);
GRAPHICS_IMPORT(device_load_pixelshader);
GRAPHICS_IMPORT(device_load_default_samplerstate);
GRAPHICS_IMPORT(device_get_vertex_shader);
GRAPHICS_IMPORT(device_get_pixel_shader);
GRAPHICS_IMPORT(device_get_render_target);
GRAPHICS_IMPORT(device_get_zstencil_target);
GRAPHICS_IMPORT(device_set_render_target);
GRAPHICS_IMPORT(device_set_render_target_with_color_space);
GRAPHICS_IMPORT(device_set_cube_render_target);
GRAPHICS_IMPORT(device_enable_framebuffer_srgb);
GRAPHICS_IMPORT(device_framebuffer_srgb_enabled);
GRAPHICS_IMPORT(device_copy_texture_region);
GRAPHICS_IMPORT(device_copy_texture);
GRAPHICS_IMPORT(device_stage_texture);
GRAPHICS_IMPORT(device_begin_frame);
GRAPHICS_IMPORT(device_begin_scene);
GRAPHICS_IMPORT(device_draw);
GRAPHICS_IMPORT(device_load_swapchain);
GRAPHICS_IMPORT(device_end_scene);
GRAPHICS_IMPORT(device_clear);
GRAPHICS_IMPORT(device_is_present_ready);
GRAPHICS_IMPORT(device_present);
GRAPHICS_IMPORT(device_flush);
GRAPHICS_IMPORT(device_set_cull_mode);
GRAPHICS_IMPORT(device_get_cull_mode);
GRAPHICS_IMPORT(device_enable_blending);
GRAPHICS_IMPORT(device_enable_depth_test);
GRAPHICS_IMPORT(device_enable_stencil_test);
GRAPHICS_IMPORT(device_enable_stencil_write);
GRAPHICS_IMPORT(device_enable_color);
GRAPHICS_IMPORT(device_blend_function);
GRAPHICS_IMPORT(device_blend_function_separate);
GRAPHICS_IMPORT(device_blend_op);
GRAPHICS_IMPORT(device_depth_function);
GRAPHICS_IMPORT(device_stencil_function);
GRAPHICS_IMPORT(device_stencil_op);
GRAPHICS_IMPORT(device_set_viewport);
GRAPHICS_IMPORT(device_get_viewport);
GRAPHICS_IMPORT(device_set_scissor_rect);
GRAPHICS_IMPORT(device_ortho);
GRAPHICS_IMPORT(device_frustum);
GRAPHICS_IMPORT(device_projection_push);
GRAPHICS_IMPORT(device_projection_pop);
GRAPHICS_IMPORT(gs_swapchain_destroy);
GRAPHICS_IMPORT(gs_texture_destroy);
GRAPHICS_IMPORT(gs_texture_get_width);
GRAPHICS_IMPORT(gs_texture_get_height);
GRAPHICS_IMPORT(gs_texture_get_color_format);
GRAPHICS_IMPORT(gs_texture_map);
GRAPHICS_IMPORT(gs_texture_unmap);
GRAPHICS_IMPORT_OPTIONAL(gs_texture_is_rect);
GRAPHICS_IMPORT(gs_texture_get_obj);
GRAPHICS_IMPORT(gs_cubetexture_destroy);
GRAPHICS_IMPORT(gs_cubetexture_get_size);
GRAPHICS_IMPORT(gs_cubetexture_get_color_format);
GRAPHICS_IMPORT(gs_voltexture_destroy);
GRAPHICS_IMPORT(gs_voltexture_get_width);
GRAPHICS_IMPORT(gs_voltexture_get_height);
GRAPHICS_IMPORT(gs_voltexture_get_depth);
GRAPHICS_IMPORT(gs_voltexture_get_color_format);
GRAPHICS_IMPORT(gs_stagesurface_destroy);
GRAPHICS_IMPORT(gs_stagesurface_get_width);
GRAPHICS_IMPORT(gs_stagesurface_get_height);
GRAPHICS_IMPORT(gs_stagesurface_get_color_format);
GRAPHICS_IMPORT(gs_stagesurface_map);
GRAPHICS_IMPORT(gs_stagesurface_unmap);
GRAPHICS_IMPORT(gs_zstencil_destroy);
GRAPHICS_IMPORT(gs_samplerstate_destroy);
GRAPHICS_IMPORT(gs_vertexbuffer_destroy);
GRAPHICS_IMPORT(gs_vertexbuffer_flush);
GRAPHICS_IMPORT(gs_vertexbuffer_flush_direct);
GRAPHICS_IMPORT(gs_vertexbuffer_get_data);
GRAPHICS_IMPORT(gs_indexbuffer_destroy);
GRAPHICS_IMPORT(gs_indexbuffer_flush);
GRAPHICS_IMPORT(gs_indexbuffer_flush_direct);
GRAPHICS_IMPORT(gs_indexbuffer_get_data);
GRAPHICS_IMPORT(gs_indexbuffer_get_num_indices);
GRAPHICS_IMPORT(gs_indexbuffer_get_type);
GRAPHICS_IMPORT(gs_timer_destroy);
GRAPHICS_IMPORT(gs_timer_begin);
GRAPHICS_IMPORT(gs_timer_end);
GRAPHICS_IMPORT(gs_timer_get_data);
GRAPHICS_IMPORT(gs_timer_range_destroy);
GRAPHICS_IMPORT(gs_timer_range_begin);
GRAPHICS_IMPORT(gs_timer_range_end);
GRAPHICS_IMPORT(gs_timer_range_get_data);
GRAPHICS_IMPORT(gs_shader_destroy);
GRAPHICS_IMPORT(gs_shader_get_num_params);
GRAPHICS_IMPORT(gs_shader_get_param_by_idx);
GRAPHICS_IMPORT(gs_shader_get_param_by_name);
GRAPHICS_IMPORT(gs_shader_get_viewproj_matrix);
GRAPHICS_IMPORT(gs_shader_get_world_matrix);
GRAPHICS_IMPORT(gs_shader_get_param_info);
GRAPHICS_IMPORT(gs_shader_set_bool);
GRAPHICS_IMPORT(gs_shader_set_float);
GRAPHICS_IMPORT(gs_shader_set_int);
GRAPHICS_IMPORT(gs_shader_set_matrix3);
GRAPHICS_IMPORT(gs_shader_set_matrix4);
GRAPHICS_IMPORT(gs_shader_set_vec2);
GRAPHICS_IMPORT(gs_shader_set_vec3);
GRAPHICS_IMPORT(gs_shader_set_vec4);
GRAPHICS_IMPORT(gs_shader_set_texture);
GRAPHICS_IMPORT(gs_shader_set_val);
GRAPHICS_IMPORT(gs_shader_set_default);
GRAPHICS_IMPORT(gs_shader_set_next_sampler);
GRAPHICS_IMPORT_OPTIONAL(device_nv12_available);
GRAPHICS_IMPORT_OPTIONAL(device_p010_available);
GRAPHICS_IMPORT_OPTIONAL(device_texture_create_nv12);
GRAPHICS_IMPORT_OPTIONAL(device_texture_create_p010);
GRAPHICS_IMPORT(device_is_monitor_hdr);
GRAPHICS_IMPORT(device_debug_marker_begin);
GRAPHICS_IMPORT(device_debug_marker_end);
GRAPHICS_IMPORT_OPTIONAL(gs_get_adapter_count);
/* OSX/Cocoa specific functions */
#ifdef __APPLE__
GRAPHICS_IMPORT(device_shared_texture_available);
GRAPHICS_IMPORT(device_texture_open_shared);
GRAPHICS_IMPORT(device_texture_create_from_iosurface);
GRAPHICS_IMPORT(gs_texture_rebind_iosurface);
/* win32 specific functions */
#elif _WIN32
GRAPHICS_IMPORT(device_gdi_texture_available);
GRAPHICS_IMPORT(device_shared_texture_available);
GRAPHICS_IMPORT_OPTIONAL(device_get_duplicator_monitor_info);
GRAPHICS_IMPORT_OPTIONAL(device_duplicator_get_monitor_index);
GRAPHICS_IMPORT_OPTIONAL(device_duplicator_create);
GRAPHICS_IMPORT_OPTIONAL(gs_duplicator_destroy);
GRAPHICS_IMPORT_OPTIONAL(gs_duplicator_update_frame);
GRAPHICS_IMPORT_OPTIONAL(gs_duplicator_get_texture);
GRAPHICS_IMPORT_OPTIONAL(gs_duplicator_get_color_space);
GRAPHICS_IMPORT_OPTIONAL(gs_duplicator_get_sdr_white_level);
GRAPHICS_IMPORT_OPTIONAL(device_can_adapter_fast_clear);
GRAPHICS_IMPORT_OPTIONAL(device_texture_create_gdi);
GRAPHICS_IMPORT_OPTIONAL(gs_texture_get_dc);
GRAPHICS_IMPORT_OPTIONAL(gs_texture_release_dc);
GRAPHICS_IMPORT_OPTIONAL(device_texture_open_shared);
GRAPHICS_IMPORT_OPTIONAL(device_texture_open_nt_shared);
GRAPHICS_IMPORT_OPTIONAL(device_texture_get_shared_handle);
GRAPHICS_IMPORT_OPTIONAL(device_texture_wrap_obj);
GRAPHICS_IMPORT_OPTIONAL(device_texture_acquire_sync);
GRAPHICS_IMPORT_OPTIONAL(device_texture_release_sync);
GRAPHICS_IMPORT_OPTIONAL(device_stagesurface_create_nv12);
GRAPHICS_IMPORT_OPTIONAL(device_stagesurface_create_p010);
GRAPHICS_IMPORT_OPTIONAL(device_register_loss_callbacks);
GRAPHICS_IMPORT_OPTIONAL(device_unregister_loss_callbacks);
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
GRAPHICS_IMPORT(device_texture_create_from_dmabuf);
GRAPHICS_IMPORT(device_query_dmabuf_capabilities);
GRAPHICS_IMPORT(device_query_dmabuf_modifiers_for_format);
GRAPHICS_IMPORT(device_texture_create_from_pixmap);
#endif
return success;
}
device_create回调
d3d11的device_create函数如下:
int device_create(gs_device_t **p_device, uint32_t adapter)
{
gs_device *device = NULL;
int errorcode = GS_SUCCESS;
try {
//打印所有显卡及该显卡下的显示器信息
LogD3DAdapters();
//创建obs-studio/shader-cache 目录
CreateShaderCacheDirectory();
device = new gs_device(adapter);
} catch (const UnsupportedHWError &error) {
blog(LOG_ERROR, "device_create (D3D11): %s (%08lX)", error.str, error.hr);
errorcode = GS_ERROR_NOT_SUPPORTED;
} catch (const HRError &error) {
blog(LOG_ERROR, "device_create (D3D11): %s (%08lX)", error.str,error.hr);
errorcode = GS_ERROR_FAIL;
}
*p_device = device;
return errorcode;
}
然后最重析的就是创建gs_device结构体的new了。gs_device描述符obs对d3d11和opengl的设备对象抽象。其中使用那个显卡,由adapter即obs_video_info成员的 ovi.adapter决定。一般为0。
gs_device结构体构造函数的初始化如下:
gs_device::gs_device(uint32_t adapterIdx)
: curToplogy(D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED)
{
matrix4_identity(&curProjMatrix);
matrix4_identity(&curViewMatrix);
matrix4_identity(&curViewProjMatrix);
memset(&viewport, 0, sizeof(viewport));
for (size_t i = 0; i < GS_MAX_TEXTURES; i++)
{
curTextures[i] = NULL;
curSamplers[i] = NULL;
}
InitFactory();
InitAdapter(adapterIdx);
//初始化D3D11Device和D3D11DeviceContext
InitDevice(adapterIdx);
//渲染目标相关,如到窗口选择交换链
device_set_render_target(this, NULL, NULL);
}
graphics_init
graphics_init是OBS资源初始化。
static bool graphics_init(struct graphics_subsystem *graphics)
{
struct matrix4 top_mat;
matrix4_identity(&top_mat);
da_push_back(graphics->matrix_stack, &top_mat);
//do nothing
graphics->exports.device_enter_context(graphics->device);
//初始 gs_vertbuffer_t *immediate_vertbuffer
if (!graphics_init_immediate_vb(graphics))
return false;
//初始化 gs_vertbuffer_t *sprite_buffer
if (!graphics_init_sprite_vb(graphics))
return false;
if (pthread_mutex_init(&graphics->mutex, NULL) != 0)
return false;
if (pthread_mutex_init(&graphics->effect_mutex, NULL) != 0)
return false;
//好像只有OPENGL用到
graphics->exports.device_blend_function_separate(graphics->device, GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA,GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
//当前的混合状态
graphics->cur_blend_state.enabled = true;
graphics->cur_blend_state.src_c = GS_BLEND_SRCALPHA;
graphics->cur_blend_state.dest_c = GS_BLEND_INVSRCALPHA;
graphics->cur_blend_state.src_a = GS_BLEND_ONE;
graphics->cur_blend_state.dest_a = GS_BLEND_INVSRCALPHA;
graphics->cur_blend_state.op = GS_BLEND_OP_ADD;
graphics->exports.device_blend_op(graphics->device, graphics->cur_blend_state.op);
graphics->exports.device_leave_context(graphics->device);
//空
gs_init_image_deps();
return true;
}
而graphics_init_immediate_vb和graphics_init_sprite_vb则分别创建其对应的顶点坐标和纹理,不过数量等有所差异。
static bool graphics_init_immediate_vb(struct graphics_subsystem *graphics)
{
struct gs_vb_data *vbd;
vbd = gs_vbdata_create();
//512个顶 点、法线、颜色
vbd->num = IMMEDIATE_COUNT;//512
vbd->points = bmalloc(sizeof(struct vec3) * IMMEDIATE_COUNT);
vbd->normals = bmalloc(sizeof(struct vec3) * IMMEDIATE_COUNT);
vbd->colors = bmalloc(sizeof(uint32_t) * IMMEDIATE_COUNT);
//1个纹理
vbd->num_tex = 1;
vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
vbd->tvarray[0].width = 2;
vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * IMMEDIATE_COUNT);
graphics->immediate_vertbuffer =graphics->exports.device_vertexbuffer_create(graphics->device, vbd, GS_DYNAMIC);
if (!graphics->immediate_vertbuffer)
return false;
return true;
}
static bool graphics_init_sprite_vb(struct graphics_subsystem *graphics)
{
struct gs_vb_data *vbd;
vbd = gs_vbdata_create();
//4个顶点
vbd->num = 4;
vbd->points = bmalloc(sizeof(struct vec3) * 4);
//1个纹理
vbd->num_tex = 1;
vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
vbd->tvarray[0].width = 2;
vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * 4);
memset(vbd->points, 0, sizeof(struct vec3) * 4);
memset(vbd->tvarray[0].array, 0, sizeof(struct vec2) * 4);
graphics->sprite_buffer = graphics->exports.device_vertexbuffer_create(graphics->device, vbd, GS_DYNAMIC);
if (!graphics->sprite_buffer)
return false;
return true;
}