Make a number of key optimizations

- Changed glMapBuffer to glMapBufferRange to allow invalidation.  Using
   just glMapBuffer alone was causing some unacceptable stalls.

 - Changed dynamic buffers from GL_DYNAMIC_WRITE to GL_STREAM_WRITE
   because I had misunderstood the OpenGL specification

 - Added _OPENGL and _D3D11 builtin preprocessor macros to effects to
   allow special processing if needed

 - Added fmod support to shaders (NOTE: D3D and GL do not function
   identically with negative numbers when using this.  Positive numbers
   however function identically)

 - Created a planar conversion shader that converts from packed YUV to
   planar 420 right on the GPU without any CPU processing.  Reduces
   required GPU download size to approximately 37.5% of its normal rate
   as well.  GPU usage down by 10 entire percentage points despite the
   extra required pass.
This commit is contained in:
jp9000
2014-02-16 19:28:21 -07:00
parent fc368f663e
commit 2dbbffe4a2
21 changed files with 470 additions and 24 deletions

View File

@@ -914,11 +914,26 @@ error:
static bool ep_compile(struct effect_parser *ep);
extern const char *gs_preprocessor_name(void);
bool ep_parse(struct effect_parser *ep, effect_t effect,
const char *effect_string, const char *file)
{
bool success;
const char *graphics_preprocessor = gs_preprocessor_name();
if (graphics_preprocessor) {
struct cf_def def;
cf_def_init(&def);
def.name.str.array = graphics_preprocessor;
def.name.str.len = strlen(graphics_preprocessor);
strref_copy(&def.name.unmerged_str, &def.name.str);
cf_preprocessor_add_def(&ep->cfp.pp, &def);
}
ep->effect = effect;
if (!cf_parser_parse(&ep->cfp, effect_string, file))
return false;

View File

@@ -40,6 +40,7 @@ bool load_graphics_imports(struct gs_exports *exports, void *module,
{
bool success = true;
GRAPHICS_IMPORT(device_preprocessor_name);
GRAPHICS_IMPORT(device_create);
GRAPHICS_IMPORT(device_destroy);
GRAPHICS_IMPORT(device_entercontext);

View File

@@ -24,6 +24,7 @@
#include "matrix4.h"
struct gs_exports {
const char *(*device_preprocessor_name)(void);
device_t (*device_create)(struct gs_init_data *data);
void (*device_destroy)(device_t device);
void (*device_entercontext)(device_t device);

View File

@@ -865,6 +865,12 @@ void gs_perspective(float angle, float aspect, float near, float far)
/* ------------------------------------------------------------------------- */
const char *gs_preprocessor_name(void)
{
graphics_t graphics = thread_graphics;
return graphics->exports.device_preprocessor_name();
}
swapchain_t gs_create_swapchain(struct gs_init_data *data)
{
graphics_t graphics = thread_graphics;

View File

@@ -87,11 +87,14 @@ struct obs_core_video {
stagesurf_t copy_surfaces[NUM_TEXTURES];
texture_t render_textures[NUM_TEXTURES];
texture_t output_textures[NUM_TEXTURES];
texture_t convert_textures[NUM_TEXTURES];
bool textures_rendered[NUM_TEXTURES];
bool textures_output[NUM_TEXTURES];
bool textures_copied[NUM_TEXTURES];
bool textures_converted[NUM_TEXTURES];
struct source_frame convert_frames[NUM_TEXTURES];
effect_t default_effect;
effect_t conversion_effect;
stagesurf_t mapped_surface;
int cur_texture;
@@ -99,6 +102,15 @@ struct obs_core_video {
pthread_t video_thread;
bool thread_initialized;
bool gpu_conversion;
const char *conversion_tech;
uint32_t conversion_height;
uint32_t plane_offsets[3];
uint32_t plane_sizes[3];
uint32_t plane_linewidth[3];
uint32_t output_width;
uint32_t output_height;
uint32_t base_width;
uint32_t base_height;

View File

@@ -64,6 +64,7 @@ static inline void render_displays(void)
static inline void set_render_size(uint32_t width, uint32_t height)
{
gs_enable_depthtest(false);
gs_enable_blending(false);
gs_setcullmode(GS_NEITHER);
gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
@@ -137,15 +138,77 @@ static inline void render_output_texture(struct obs_core_video *video,
video->textures_output[cur_texture] = true;
}
static inline void stage_output_texture(struct obs_core_video *video,
static inline void set_eparam(effect_t effect, const char *name, float val)
{
eparam_t param = effect_getparambyname(effect, name);
effect_setfloat(effect, param, val);
}
static void render_convert_texture(struct obs_core_video *video,
int cur_texture, int prev_texture)
{
texture_t texture = video->output_textures[prev_texture];
stagesurf_t copy = video->copy_surfaces[cur_texture];
texture_t target = video->convert_textures[cur_texture];
float fwidth = (float)video->output_width;
float fheight = (float)video->output_height;
size_t passes, i;
effect_t effect = video->conversion_effect;
eparam_t image = effect_getparambyname(effect, "image");
technique_t tech = effect_gettechnique(effect,
video->conversion_tech);
if (!video->textures_output[prev_texture])
return;
set_eparam(effect, "u_plane_offset", (float)video->plane_offsets[1]);
set_eparam(effect, "v_plane_offset", (float)video->plane_offsets[2]);
set_eparam(effect, "width", fwidth);
set_eparam(effect, "height", fheight);
set_eparam(effect, "width_i", 1.0f / fwidth);
set_eparam(effect, "height_i", 1.0f / fheight);
set_eparam(effect, "width_d2", fwidth * 0.5f);
set_eparam(effect, "height_d2", fheight * 0.5f);
set_eparam(effect, "width_d2_i", 1.0f / (fwidth * 0.5f));
set_eparam(effect, "height_d2_i", 1.0f / (fheight * 0.5f));
set_eparam(effect, "input_width", fwidth);
set_eparam(effect, "input_height", (float)video->conversion_height);
effect_settexture(effect, image, texture);
gs_setrendertarget(target, NULL);
set_render_size(video->output_width, video->conversion_height);
passes = technique_begin(tech);
for (i = 0; i < passes; i++) {
technique_beginpass(tech, i);
gs_draw_sprite(texture, 0, video->output_width,
video->conversion_height);
technique_endpass(tech);
}
technique_end(tech);
video->textures_converted[cur_texture] = true;
}
static inline void stage_output_texture(struct obs_core_video *video,
int cur_texture, int prev_texture)
{
texture_t texture;
bool texture_ready;
stagesurf_t copy = video->copy_surfaces[cur_texture];
if (video->gpu_conversion) {
texture = video->convert_textures[prev_texture];
texture_ready = video->textures_converted[prev_texture];
} else {
texture = video->output_textures[prev_texture];
texture_ready = video->output_textures[prev_texture];
}
unmap_last_surface(video);
if (!video->textures_output[prev_texture])
if (!texture_ready)
return;
gs_stage_texture(copy, texture);
@@ -163,9 +226,13 @@ static inline void render_video(struct obs_core_video *video, int cur_texture,
render_main_texture(video, cur_texture);
render_output_texture(video, cur_texture, prev_texture);
if (video->gpu_conversion)
render_convert_texture(video, cur_texture, prev_texture);
stage_output_texture(video, cur_texture, prev_texture);
gs_setrendertarget(NULL, NULL);
gs_enable_blending(true);
gs_endscene();
}
@@ -186,6 +253,90 @@ static inline bool download_frame(struct obs_core_video *video,
return true;
}
static inline uint32_t calc_linesize(uint32_t pos, uint32_t linesize)
{
uint32_t size = pos % linesize;
return size ? size : linesize;
}
static void copy_dealign(
uint8_t *dst, uint32_t dst_pos, uint32_t dst_linesize,
const uint8_t *src, uint32_t src_pos, uint32_t src_linesize,
uint32_t remaining)
{
while (remaining) {
uint32_t src_remainder = src_pos % src_linesize;
uint32_t dst_offset = dst_linesize - src_remainder;
uint32_t src_offset = src_linesize - src_remainder;
if (remaining < dst_offset) {
memcpy(dst + dst_pos, src + src_pos, remaining);
src_pos += remaining;
dst_pos += remaining;
remaining = 0;
} else {
memcpy(dst + dst_pos, src + src_pos, dst_offset);
src_pos += src_offset;
dst_pos += dst_offset;
remaining -= dst_offset;
}
}
}
static inline uint32_t make_aligned_linesize_offset(uint32_t offset,
uint32_t dst_linesize, uint32_t src_linesize)
{
uint32_t remainder = offset % dst_linesize;
return (offset / dst_linesize) * src_linesize + remainder;
}
static void fix_gpu_converted_alignment(struct obs_core_video *video,
struct video_frame *frame, int cur_texture)
{
struct source_frame *new_frame = &video->convert_frames[cur_texture];
uint32_t src_linesize = frame->linesize[0];
uint32_t dst_linesize = video->output_width * 4;
uint32_t src_pos = 0;
for (size_t i = 0; i < 3; i++) {
if (video->plane_linewidth[i] == 0)
break;
src_pos = make_aligned_linesize_offset(video->plane_offsets[i],
dst_linesize, src_linesize);
copy_dealign(new_frame->data[i], 0, dst_linesize,
frame->data[0], src_pos, src_linesize,
video->plane_sizes[i]);
}
/* replace with cached frames */
for (size_t i = 0; i < MAX_AV_PLANES; i++) {
frame->data[i] = new_frame->data[i];
frame->linesize[i] = new_frame->linesize[i];
}
}
static bool set_gpu_converted_data(struct obs_core_video *video,
struct video_frame *frame, int cur_texture)
{
if (frame->linesize[0] == video->output_width*4) {
for (size_t i = 0; i < 3; i++) {
if (video->plane_linewidth[i] == 0)
break;
frame->linesize[i] = video->plane_linewidth[i];
frame->data[i] =
frame->data[0] + video->plane_offsets[i];
}
} else {
fix_gpu_converted_alignment(video, frame, cur_texture);
}
return true;
}
static bool convert_frame(struct obs_core_video *video,
struct video_frame *frame,
const struct video_output_info *info, int cur_texture)
@@ -223,9 +374,14 @@ static inline void output_video_data(struct obs_core_video *video,
const struct video_output_info *info;
info = video_output_getinfo(video->video);
if (format_is_yuv(info->format))
if (video->gpu_conversion) {
if (!set_gpu_converted_data(video, frame, cur_texture))
return;
} else if (format_is_yuv(info->format)) {
if (!convert_frame(video, frame, info, cur_texture))
return;
}
video_output_frame(video->video, frame);
}

View File

@@ -47,16 +47,92 @@ static inline void make_video_info(struct video_output_info *vi,
vi->height = ovi->output_height;
}
#define PIXEL_SIZE 4
#define GET_ALIGN(val, align) \
(((val) + (align-1)) & ~(align-1))
static inline void set_420p_sizes(const struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
uint32_t chroma_pixels;
uint32_t total_bytes;
chroma_pixels = (ovi->output_width * ovi->output_height / 4);
chroma_pixels = GET_ALIGN(chroma_pixels, PIXEL_SIZE);
video->plane_offsets[0] = 0;
video->plane_offsets[1] = ovi->output_width * ovi->output_height;
video->plane_offsets[2] = video->plane_offsets[1] + chroma_pixels;
video->plane_linewidth[0] = ovi->output_width;
video->plane_linewidth[1] = ovi->output_width/2;
video->plane_linewidth[2] = ovi->output_width/2;
video->plane_sizes[0] = video->plane_offsets[1];
video->plane_sizes[1] = video->plane_sizes[0]/4;
video->plane_sizes[2] = video->plane_sizes[1];
total_bytes = video->plane_offsets[2] + chroma_pixels;
video->conversion_height =
(total_bytes/PIXEL_SIZE + ovi->output_width-1) /
ovi->output_width;
video->conversion_height = GET_ALIGN(video->conversion_height, 2);
video->conversion_tech = "Planar420";
}
static inline void calc_gpu_conversion_sizes(const struct obs_video_info *ovi)
{
obs->video.conversion_height = 0;
memset(obs->video.plane_offsets, 0, sizeof(obs->video.plane_offsets));
memset(obs->video.plane_sizes, 0, sizeof(obs->video.plane_sizes));
memset(obs->video.plane_linewidth, 0,
sizeof(obs->video.plane_linewidth));
switch ((uint32_t)ovi->output_format) {
case VIDEO_FORMAT_I420:
set_420p_sizes(ovi);
}
}
static bool obs_init_gpu_conversion(struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
calc_gpu_conversion_sizes(ovi);
if (!video->conversion_height) {
blog(LOG_INFO, "GPU conversion not available for format: %u",
(unsigned int)ovi->output_format);
video->gpu_conversion = false;
return true;
}
for (size_t i = 0; i < NUM_TEXTURES; i++) {
video->convert_textures[i] = gs_create_texture(
ovi->output_width, video->conversion_height,
GS_RGBA, 1, NULL, GS_RENDERTARGET);
if (!video->convert_textures[i])
return false;
}
return true;
}
static bool obs_init_textures(struct obs_video_info *ovi)
{
struct obs_core_video *video = &obs->video;
bool yuv = format_is_yuv(ovi->output_format);
uint32_t output_height = video->gpu_conversion ?
video->conversion_height : ovi->output_height;
size_t i;
for (i = 0; i < NUM_TEXTURES; i++) {
video->copy_surfaces[i] = gs_create_stagesurface(
ovi->output_width, ovi->output_height,
GS_RGBA);
ovi->output_width, output_height, GS_RGBA);
if (!video->copy_surfaces[i])
return false;
@@ -92,8 +168,12 @@ static bool obs_init_graphics(struct obs_video_info *ovi)
int errorcode;
make_gs_init_data(&graphics_data, ovi);
video->base_width = ovi->base_width;
video->base_height = ovi->base_height;
video->base_width = ovi->base_width;
video->base_height = ovi->base_height;
video->output_width = ovi->output_width;
video->output_height = ovi->output_height;
video->gpu_conversion = ovi->gpu_conversion;
errorcode = gs_create(&video->graphics, ovi->graphics_module,
&graphics_data);
@@ -106,7 +186,9 @@ static bool obs_init_graphics(struct obs_video_info *ovi)
gs_entercontext(video->graphics);
if (!obs_init_textures(ovi))
if (ovi->gpu_conversion && !obs_init_gpu_conversion(ovi))
success = false;
if (success && !obs_init_textures(ovi))
success = false;
if (success) {
@@ -114,8 +196,16 @@ static bool obs_init_graphics(struct obs_video_info *ovi)
video->default_effect = gs_create_effect_from_file(filename,
NULL);
bfree(filename);
filename = find_libobs_data_file("format_conversion.effect");
video->conversion_effect = gs_create_effect_from_file(filename,
NULL);
bfree(filename);
if (!video->default_effect)
success = false;
if (!video->conversion_effect)
success = false;
}
gs_leavecontext();
@@ -195,15 +285,18 @@ static void obs_free_graphics(void)
for (i = 0; i < NUM_TEXTURES; i++) {
stagesurface_destroy(video->copy_surfaces[i]);
texture_destroy(video->render_textures[i]);
texture_destroy(video->convert_textures[i]);
texture_destroy(video->output_textures[i]);
source_frame_free(&video->convert_frames[i]);
video->copy_surfaces[i] = NULL;
video->render_textures[i] = NULL;
video->output_textures[i] = NULL;
video->copy_surfaces[i] = NULL;
video->render_textures[i] = NULL;
video->convert_textures[i] = NULL;
video->output_textures[i] = NULL;
}
effect_destroy(video->default_effect);
effect_destroy(video->conversion_effect);
video->default_effect = NULL;
gs_leavecontext();

View File

@@ -118,6 +118,9 @@ struct obs_video_info {
uint32_t adapter;
struct gs_window window; /**< Window to render to */
/** Use shaders to convert to different color formats */
bool gpu_conversion;
};
/**

View File

@@ -1295,7 +1295,7 @@ void cf_preprocessor_add_def(struct cf_preprocessor *pp, struct cf_def *def)
cf_def_free(existing);
memcpy(existing, def, sizeof(struct cf_def));
} else {
da_push_back(pp->defines, &def);
da_push_back(pp->defines, def);
}
}