]> git.sesse.net Git - nageru/commitdiff
Use Movit also for the display logic; a bit more setup on the mixer side, but much...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 6 Oct 2015 18:03:06 +0000 (20:03 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 6 Oct 2015 18:03:06 +0000 (20:03 +0200)
glwidget.cpp
mixer.cpp
mixer.h

index 3fb4889dc314c7ce163d389477926dfe110d51bc..9088742bdf0e7085fc19bb1e70cb1dca9b2e6090 100644 (file)
@@ -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();
 }
index 2ae8276ae7ef3780885c54ccc5b1f390778390a4..1ac5d53ee03e40484194f8f3039c10233f547f12 100644 (file)
--- 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 c4697310ec95757fecbfacea767ed887ba821e03..f2f9f7aecef7570fe1ccb47ae8adff56ef2a76be 100644 (file)
--- a/mixer.h
+++ b/mixer.h
@@ -6,6 +6,7 @@
 #include <epoxy/gl.h>
 #undef Success
 #include <movit/effect_chain.h>
+#include <movit/flat_input.h>
 #include <functional>
 
 #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 <ready_fence>, then call <setup_chain>
+               // to wire up all the inputs, and then finally call
+               // chain->render_to_screen() or similar.
+               movit::EffectChain *chain;
+               std::function<void()> 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<RefCountedFrame> 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<GLuint> 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<movit::ResourcePool> resource_pool;
        std::unique_ptr<movit::EffectChain> chain;
+       std::unique_ptr<movit::EffectChain> display_chain;
        std::unique_ptr<movit::EffectChain> preview_chain;
        GLuint cbcr_program_num;  // Owned by <resource_pool>.
        std::unique_ptr<H264Encoder> h264_encoder;
@@ -85,6 +104,9 @@ private:
        movit::Effect *resample_effect, *resample2_effect;
        movit::Effect *padding_effect, *padding2_effect;
 
+       // Effects part of <display_chain>. Owned by <display_chain>.
+       movit::FlatInput *display_input;
+
        // Effects part of <preview_chain>. Owned by <preview_chain>.
        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.