From 2c84fdec878d1144b2d615dbfb901d23e838f457 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 6 Oct 2015 20:03:06 +0200 Subject: [PATCH] Use Movit also for the display logic; a bit more setup on the mixer side, but much less on the display side, and it avoids an expensive bump to RGB textures for things like the pure previews. --- glwidget.cpp | 53 +++------------------------------------- mixer.cpp | 68 ++++++++++++++++++++++++++++++++++++++-------------- mixer.h | 28 +++++++++++++++++++--- 3 files changed, 78 insertions(+), 71 deletions(-) diff --git a/glwidget.cpp b/glwidget.cpp index 3fb4889..9088742 100644 --- a/glwidget.cpp +++ b/glwidget.cpp @@ -48,50 +48,9 @@ void GLWidget::initializeGL() QMetaObject::invokeMethod(this, "update", Qt::AutoConnection); }); - // Prepare the shaders to actually get the texture shown (ick). glDisable(GL_BLEND); glDisable(GL_DEPTH_TEST); glDepthMask(GL_FALSE); - - std::string vert_shader = - "#version 130 \n" - "in vec2 position; \n" - "in vec2 texcoord; \n" - "out vec2 tc; \n" - " \n" - "void main() \n" - "{ \n" - " gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n" - " tc = texcoord; \n" - " tc.y = 1.0 - tc.y; \n" - "} \n"; - std::string frag_shader = - "#version 130 \n" - "in vec2 tc; \n" - "uniform sampler2D tex; \n" - "void main() { \n" - " gl_FragColor = texture2D(tex, tc); \n" - "} \n"; - program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader); - - static const float vertices[] = { - 0.0f, 2.0f, - 0.0f, 0.0f, - 2.0f, 0.0f - }; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - position_vbo = movit::fill_vertex_attribute(program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices); - texcoord_vbo = movit::fill_vertex_attribute(program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same as vertices. - -#if 0 - // Cleanup. - cleanup_vertex_attribute(phases[0]->glsl_program_num, "position", position_vbo); - cleanup_vertex_attribute(phases[0]->glsl_program_num, "texcoord", texcoord_vbo); - - glDeleteVertexArrays(1, &vao); - nheck_error(); -#endif } void GLWidget::resizeGL(int width, int height) @@ -109,14 +68,8 @@ void GLWidget::paintGL() return; } - glUseProgram(program_num); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, frame.texnum); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - - glBindVertexArray(vao); glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED); - glDrawArrays(GL_TRIANGLES, 0, 3); + frame.setup_chain(); + frame.chain->render_to_screen(); + check_error(); } diff --git a/mixer.cpp b/mixer.cpp index 2ae8276..1ac5d53 100644 --- a/mixer.cpp +++ b/mixer.cpp @@ -108,6 +108,15 @@ Mixer::Mixer(const QSurfaceFormat &format) chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT); chain->finalize(); + // Display chain; shows the live output produced by the main chain (its RGBA version). + display_chain.reset(new EffectChain(WIDTH, HEIGHT, resource_pool.get())); + check_error(); + display_input = new FlatInput(inout_format, FORMAT_RGB, GL_UNSIGNED_BYTE, WIDTH, HEIGHT); // FIXME: GL_UNSIGNED_BYTE is really wrong. + display_chain->add_input(display_input); + display_chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED); + display_chain->set_dither_bits(0); // Don't bother. + display_chain->finalize(); + // Preview chain (always shows just first input for now). preview_chain.reset(new EffectChain(WIDTH, HEIGHT, resource_pool.get())); check_error(); @@ -115,7 +124,6 @@ Mixer::Mixer(const QSurfaceFormat &format) preview_chain->add_input(preview_input); preview_chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED); preview_chain->set_dither_bits(0); // Don't bother. - preview_chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT); preview_chain->finalize(); h264_encoder.reset(new H264Encoder(h264_encoder_surface, WIDTH, HEIGHT, "test.mp4")); @@ -417,11 +425,6 @@ void Mixer::thread_func() input[1]->set_texture_num(0, userdata->tex_y); input[1]->set_texture_num(1, userdata->tex_cbcr); } - - if (card_index == 0) { - preview_input->set_texture_num(0, userdata->tex_y); - preview_input->set_texture_num(1, userdata->tex_cbcr); - } } GLuint y_tex, cbcr_tex; @@ -438,11 +441,12 @@ void Mixer::thread_func() subsample_chroma(cbcr_full_tex, cbcr_tex); resource_pool->release_2d_texture(cbcr_full_tex); - // Render preview chain. - GLuint preview_rgba_tex = resource_pool->create_2d_texture(GL_RGB565, output_channel[OUTPUT_PREVIEW].width, output_channel[OUTPUT_PREVIEW].height); // Saves texture bandwidth, although dithering gets messed up. - fbo = resource_pool->create_fbo(preview_rgba_tex); - preview_chain->render_to_fbo(fbo, output_channel[OUTPUT_PREVIEW].width, output_channel[OUTPUT_PREVIEW].height); - resource_pool->release_fbo(fbo); + // Set the right state for rgba_tex. + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, rgba_tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); RefCountedGLsync fence(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0); check_error(); @@ -456,8 +460,33 @@ void Mixer::thread_func() } h264_encoder->end_frame(fence, input_frames); - output_channel[OUTPUT_LIVE].output_frame(rgba_tex, fence); - output_channel[OUTPUT_PREVIEW].output_frame(preview_rgba_tex, fence); + // The live frame just shows the RGBA texture we just rendered. + // It owns rgba_tex now. + DisplayFrame live_frame; + live_frame.chain = display_chain.get(); + live_frame.setup_chain = [this, rgba_tex]{ + display_input->set_texture_num(rgba_tex); + }; + live_frame.ready_fence = fence; + live_frame.input_frames = {}; + live_frame.temp_textures = { rgba_tex }; + output_channel[OUTPUT_LIVE].output_frame(live_frame); + + // The preview frame shows the first input. Note that the textures + // are owned by the input frame, not the display frame. + const PBOFrameAllocator::Userdata *input0_userdata = (const PBOFrameAllocator::Userdata *)bmusb_current_rendering_frame[0]->userdata; + GLuint input0_y_tex = input0_userdata->tex_y; + GLuint input0_cbcr_tex = input0_userdata->tex_cbcr; + DisplayFrame preview_frame; + preview_frame.chain = preview_chain.get(); + preview_frame.setup_chain = [this, input0_y_tex, input0_cbcr_tex]{ + preview_input->set_texture_num(0, input0_y_tex); + preview_input->set_texture_num(1, input0_cbcr_tex); + }; + preview_frame.ready_fence = fence; + preview_frame.input_frames = { bmusb_current_rendering_frame[0] }; + preview_frame.temp_textures = {}; + output_channel[OUTPUT_PREVIEW].output_frame(preview_frame); clock_gettime(CLOCK_MONOTONIC, &now); double elapsed = now.tv_sec - start.tv_sec + @@ -537,9 +566,12 @@ void Mixer::subsample_chroma(GLuint src_tex, GLuint dst_tex) void Mixer::release_display_frame(DisplayFrame *frame) { - resource_pool->release_2d_texture(frame->texnum); - frame->texnum = 0; + for (GLuint texnum : frame->temp_textures) { + resource_pool->release_2d_texture(texnum); + } + frame->temp_textures.clear(); frame->ready_fence.reset(); + frame->input_frames.clear(); } void Mixer::start() @@ -558,7 +590,7 @@ void Mixer::cut(Source source) current_source = source; } -void Mixer::OutputChannel::output_frame(GLuint tex, RefCountedGLsync fence) +void Mixer::OutputChannel::output_frame(DisplayFrame frame) { // Store this frame for display. Remove the ready frame if any // (it was seemingly never used). @@ -567,8 +599,7 @@ void Mixer::OutputChannel::output_frame(GLuint tex, RefCountedGLsync fence) if (has_ready_frame) { parent->release_display_frame(&ready_frame); } - ready_frame.texnum = tex; - ready_frame.ready_fence = fence; + ready_frame = frame; has_ready_frame = true; } @@ -593,6 +624,7 @@ bool Mixer::OutputChannel::get_display_frame(DisplayFrame *frame) assert(!has_current_frame); current_frame = ready_frame; ready_frame.ready_fence.reset(); // Drop the refcount. + ready_frame.input_frames.clear(); // Drop the refcounts. has_current_frame = true; has_ready_frame = false; } diff --git a/mixer.h b/mixer.h index c469731..f2f9f7a 100644 --- a/mixer.h +++ b/mixer.h @@ -6,6 +6,7 @@ #include #undef Success #include +#include #include #include "bmusb.h" @@ -44,8 +45,25 @@ public: }; struct DisplayFrame { - GLuint texnum; - RefCountedGLsync ready_fence; // Asserted when the texture is done rendering. + // The chain for rendering this frame. To render a display frame, + // first wait for , then call + // to wire up all the inputs, and then finally call + // chain->render_to_screen() or similar. + movit::EffectChain *chain; + std::function setup_chain; + + // Asserted when all the inputs are ready; you cannot render the chain + // before this. + RefCountedGLsync ready_fence; + + // Holds on to all the input frames needed for this display frame, + // so they are not released while still rendering. + std::vector input_frames; + + // Textures that should be released back to the resource pool + // when this frame disappears, if any. + // TODO: Refcount these as well? + std::vector temp_textures; }; // Implicitly frees the previous one if there's a new frame available. bool get_display_frame(Output output, DisplayFrame *frame) { @@ -76,6 +94,7 @@ private: QSurface *mixer_surface, *h264_encoder_surface; std::unique_ptr resource_pool; std::unique_ptr chain; + std::unique_ptr display_chain; std::unique_ptr preview_chain; GLuint cbcr_program_num; // Owned by . std::unique_ptr h264_encoder; @@ -85,6 +104,9 @@ private: movit::Effect *resample_effect, *resample2_effect; movit::Effect *padding_effect, *padding2_effect; + // Effects part of . Owned by . + movit::FlatInput *display_input; + // Effects part of . Owned by . movit::YCbCrInput *preview_input; @@ -112,7 +134,7 @@ private: class OutputChannel { public: - void output_frame(GLuint tex, RefCountedGLsync fence); + void output_frame(DisplayFrame frame); bool get_display_frame(DisplayFrame *frame); void set_frame_ready_callback(new_frame_ready_callback_t callback); void set_size(int width, int height); // Ignored for OUTPUT_LIVE. -- 2.39.2