obs_init_graphics
			 2024-07-05
			  82
			 0
			
			
			
				
			
			
		
			obs_init_graphics函数实现了图形及渲染的基本初始化。其主要实现了以下几个功能:
- 加载D3D11 DLL模块,并保存该模块DLL导出的函数指针。
- 初始化HLSL着色器effect
- 采样状态机
- 转换纹理

static int obs_init_graphics(struct obs_video_info *ovi)
{
    struct obs_core_video *video = &obs->video;
    uint8_t transparent_tex_data[2 * 2 * 4] = {0};
    const uint8_t *transparent_tex = transparent_tex_data;
    struct gs_sampler_info point_sampler = {0};
    bool success = true;
    int errorcode;
    profile_start(obs_init_graphics_name);
    //1。graphics模块,这里实际为D3D11的dll
    errorcode = gs_create(&video->graphics, ovi->graphics_module, ovi->adapter);
    if (errorcode != GS_SUCCESS)
    {
        profile_end(obs_init_graphics_name);
        switch (errorcode) {
        case GS_ERROR_MODULE_NOT_FOUND:
            return OBS_VIDEO_MODULE_NOT_FOUND;
        case GS_ERROR_NOT_SUPPORTED:
            return OBS_VIDEO_NOT_SUPPORTED;
        default:
            return OBS_VIDEO_FAIL;
        }
    }
    profile_start(shader_comp_name);
    gs_enter_context(video->graphics);
    //2。初始化effect,即HLSL
    char *filename = obs_find_data_file("default.effect");
    video->default_effect = gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    if (gs_get_device_type() == GS_DEVICE_OPENGL)
    {
        filename = obs_find_data_file("default_rect.effect");
        video->default_rect_effect =gs_effect_create_from_file(filename, NULL);
        bfree(filename);
    }
    filename = obs_find_data_file("opaque.effect");
    video->opaque_effect = gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    filename = obs_find_data_file("solid.effect");
    video->solid_effect = gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    filename = obs_find_data_file("repeat.effect");
    video->repeat_effect = gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    filename = obs_find_data_file("format_conversion.effect");
    video->conversion_effect = gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    filename = obs_find_data_file("bicubic_scale.effect");
    video->bicubic_effect = gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    filename = obs_find_data_file("lanczos_scale.effect");
    video->lanczos_effect = gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    filename = obs_find_data_file("area.effect");
    video->area_effect = gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    filename = obs_find_data_file("bilinear_lowres_scale.effect");
    video->bilinear_lowres_effect =    gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    filename = obs_find_data_file("premultiplied_alpha.effect");
    video->premultiplied_alpha_effect =gs_effect_create_from_file(filename, NULL);
    bfree(filename);
    //3.采样器
    point_sampler.max_anisotropy = 1;
    video->point_sampler = gs_samplerstate_create(&point_sampler);
    //4.转换后的纹理
    obs->video.transparent_texture = gs_texture_create(2, 2, GS_RGBA, 1, &transparent_tex, 0);
    if (!video->default_effect)
        success = false;
    if (gs_get_device_type() == GS_DEVICE_OPENGL)
    {
        if (!video->default_rect_effect)
            success = false;
    }
    if (!video->opaque_effect)
        success = false;
    if (!video->solid_effect)
        success = false;
    if (!video->conversion_effect)
        success = false;
    if (!video->premultiplied_alpha_effect)
        success = false;
    if (!video->transparent_texture)
        success = false;
    if (!video->point_sampler)
        success = false;
    gs_leave_context();
    profile_end(shader_comp_name);
    profile_end(obs_init_graphics_name);
    return success ? OBS_VIDEO_SUCCESS : OBS_VIDEO_FAIL;
}
上面这些接口的加载及初始化在Windows下都是通过d3d11.dll实现的。
 OBS图形及渲染
			OBS图形及渲染
			




