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)
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();
}
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();
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"));
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;
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();
}
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 +
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()
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).
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;
}
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;
}
#include <epoxy/gl.h>
#undef Success
#include <movit/effect_chain.h>
+#include <movit/flat_input.h>
#include <functional>
#include "bmusb.h"
};
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) {
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;
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;
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.