2016-02-05 03:28:50 +00:00
|
|
|
// CRTOpenGL.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 03/02/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "CRT.hpp"
|
2017-01-08 16:13:20 +00:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cmath>
|
2016-02-05 03:28:50 +00:00
|
|
|
|
2016-02-14 01:52:23 +00:00
|
|
|
#include "CRTOpenGL.hpp"
|
2016-03-22 02:01:25 +00:00
|
|
|
#include "../../../SignalProcessing/FIRFilter.hpp"
|
2016-04-28 02:29:54 +00:00
|
|
|
#include "Shaders/OutputShader.hpp"
|
2016-02-05 03:28:50 +00:00
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
using namespace Outputs::CRT;
|
2016-02-05 03:28:50 +00:00
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
namespace {
|
2017-01-08 19:47:16 +00:00
|
|
|
static const GLenum source_data_texture_unit = GL_TEXTURE0;
|
|
|
|
static const GLenum pixel_accumulation_texture_unit = GL_TEXTURE1;
|
2017-01-08 16:13:20 +00:00
|
|
|
|
2017-01-08 19:47:16 +00:00
|
|
|
static const GLenum composite_texture_unit = GL_TEXTURE2;
|
|
|
|
static const GLenum separated_texture_unit = GL_TEXTURE3;
|
|
|
|
static const GLenum filtered_texture_unit = GL_TEXTURE4;
|
|
|
|
|
|
|
|
static const GLenum work_texture_unit = GL_TEXTURE2;
|
2016-03-09 03:40:23 +00:00
|
|
|
}
|
2016-02-06 02:35:39 +00:00
|
|
|
|
2016-11-17 04:26:04 +00:00
|
|
|
OpenGLOutputBuilder::OpenGLOutputBuilder(size_t bytes_per_pixel) :
|
2016-11-21 04:14:52 +00:00
|
|
|
visible_area_(Rect(0, 0, 1, 1)),
|
|
|
|
composite_src_output_y_(0),
|
|
|
|
composite_shader_(nullptr),
|
|
|
|
rgb_shader_(nullptr),
|
|
|
|
last_output_width_(0),
|
|
|
|
last_output_height_(0),
|
|
|
|
fence_(nullptr),
|
2016-11-17 04:26:04 +00:00
|
|
|
texture_builder(bytes_per_pixel, source_data_texture_unit),
|
2017-01-08 16:13:20 +00:00
|
|
|
array_builder(SourceVertexBufferDataSize, OutputVertexBufferDataSize)
|
2016-03-09 03:40:23 +00:00
|
|
|
{
|
2016-04-27 02:14:12 +00:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_CONSTANT_COLOR);
|
2016-05-06 01:27:13 +00:00
|
|
|
glBlendColor(0.6f, 0.6f, 0.6f, 1.0f);
|
2016-04-15 02:20:47 +00:00
|
|
|
|
2016-04-13 02:31:13 +00:00
|
|
|
// create the output vertex array
|
2016-11-21 04:14:52 +00:00
|
|
|
glGenVertexArrays(1, &output_vertex_array_);
|
2016-04-13 02:31:13 +00:00
|
|
|
|
2016-04-17 21:17:59 +00:00
|
|
|
// create the source vertex array
|
2016-11-21 04:14:52 +00:00
|
|
|
glGenVertexArrays(1, &source_vertex_array_);
|
2017-01-08 16:13:20 +00:00
|
|
|
|
|
|
|
bool supports_texture_barrier = false;
|
|
|
|
#ifdef GL_NV_texture_barrier
|
|
|
|
GLint number_of_extensions;
|
|
|
|
glGetIntegerv(GL_NUM_EXTENSIONS, &number_of_extensions);
|
|
|
|
|
|
|
|
for(GLuint c = 0; c < (GLuint)number_of_extensions; c++)
|
|
|
|
{
|
|
|
|
const char *extension_name = (const char *)glGetStringi(GL_EXTENSIONS, c);
|
|
|
|
if(!strcmp(extension_name, "GL_NV_texture_barrier"))
|
|
|
|
{
|
|
|
|
supports_texture_barrier = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-08 19:47:16 +00:00
|
|
|
// if(supports_texture_barrier)
|
|
|
|
// {
|
|
|
|
// work_texture_.reset(new OpenGL::TextureTarget(IntermediateBufferWidth, IntermediateBufferHeight*2, work_texture_unit));
|
|
|
|
// }
|
|
|
|
// else
|
2017-01-08 16:13:20 +00:00
|
|
|
{
|
2017-01-11 03:08:07 +00:00
|
|
|
composite_texture_.reset(new OpenGL::TextureTarget(IntermediateBufferWidth, IntermediateBufferHeight, composite_texture_unit, GL_NEAREST));
|
|
|
|
separated_texture_.reset(new OpenGL::TextureTarget(IntermediateBufferWidth, IntermediateBufferHeight, separated_texture_unit, GL_NEAREST));
|
|
|
|
filtered_texture_.reset(new OpenGL::TextureTarget(IntermediateBufferWidth, IntermediateBufferHeight, filtered_texture_unit, GL_LINEAR));
|
2017-01-08 16:13:20 +00:00
|
|
|
}
|
2016-03-09 01:49:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
OpenGLOutputBuilder::~OpenGLOutputBuilder()
|
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
glDeleteVertexArrays(1, &output_vertex_array_);
|
2016-03-09 01:49:07 +00:00
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
free(composite_shader_);
|
|
|
|
free(rgb_shader_);
|
2016-03-08 02:04:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 02:25:03 +00:00
|
|
|
bool OpenGLOutputBuilder::get_is_television_output()
|
|
|
|
{
|
|
|
|
return output_device_ == Television || !rgb_input_shader_program_;
|
|
|
|
}
|
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int output_height, bool only_if_dirty)
|
2016-02-05 03:57:46 +00:00
|
|
|
{
|
2016-06-03 02:29:09 +00:00
|
|
|
// lock down any other draw_frames
|
2016-11-21 04:14:52 +00:00
|
|
|
draw_mutex_.lock();
|
2016-05-03 22:45:55 +00:00
|
|
|
|
2016-02-13 03:31:05 +00:00
|
|
|
// establish essentials
|
2016-11-21 04:14:52 +00:00
|
|
|
if(!output_shader_program_)
|
2016-02-13 03:31:05 +00:00
|
|
|
{
|
2016-05-03 01:05:58 +00:00
|
|
|
prepare_composite_input_shaders();
|
|
|
|
prepare_rgb_input_shaders();
|
2016-04-17 20:17:23 +00:00
|
|
|
prepare_source_vertex_array();
|
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
prepare_output_shader();
|
2016-03-08 02:42:21 +00:00
|
|
|
prepare_output_vertex_array();
|
2016-02-14 04:50:18 +00:00
|
|
|
|
2016-04-24 22:58:31 +00:00
|
|
|
set_timing_uniforms();
|
2016-04-24 23:16:23 +00:00
|
|
|
set_colour_space_uniforms();
|
2016-02-13 03:31:05 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
if(fence_ != nullptr)
|
2016-05-08 23:45:36 +00:00
|
|
|
{
|
2016-06-30 01:32:47 +00:00
|
|
|
// if the GPU is still busy, don't wait; we'll catch it next time
|
2016-11-21 04:14:52 +00:00
|
|
|
if(glClientWaitSync(fence_, GL_SYNC_FLUSH_COMMANDS_BIT, only_if_dirty ? 0 : GL_TIMEOUT_IGNORED) == GL_TIMEOUT_EXPIRED)
|
2016-06-30 01:32:47 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
draw_mutex_.unlock();
|
2016-06-30 01:32:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
glDeleteSync(fence_);
|
2016-05-08 23:45:36 +00:00
|
|
|
}
|
2016-05-08 22:50:30 +00:00
|
|
|
|
2016-05-01 20:17:52 +00:00
|
|
|
// make sure there's a target to draw to
|
2016-11-21 04:14:52 +00:00
|
|
|
if(!framebuffer_ || framebuffer_->get_height() != output_height || framebuffer_->get_width() != output_width)
|
2016-05-01 20:17:52 +00:00
|
|
|
{
|
2017-01-11 03:08:07 +00:00
|
|
|
std::unique_ptr<OpenGL::TextureTarget> new_framebuffer(new OpenGL::TextureTarget((GLsizei)output_width, (GLsizei)output_height, pixel_accumulation_texture_unit, GL_LINEAR));
|
2016-11-21 04:14:52 +00:00
|
|
|
if(framebuffer_)
|
2016-05-01 20:17:52 +00:00
|
|
|
{
|
|
|
|
new_framebuffer->bind_framebuffer();
|
2016-05-02 02:28:33 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
glActiveTexture(pixel_accumulation_texture_unit);
|
2016-11-21 04:14:52 +00:00
|
|
|
framebuffer_->bind_texture();
|
|
|
|
framebuffer_->draw((float)output_width / (float)output_height);
|
2016-05-02 02:28:33 +00:00
|
|
|
|
|
|
|
new_framebuffer->bind_texture();
|
2016-05-01 20:17:52 +00:00
|
|
|
}
|
2016-11-21 04:14:52 +00:00
|
|
|
framebuffer_ = std::move(new_framebuffer);
|
2016-05-01 20:17:52 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 02:29:09 +00:00
|
|
|
// lock out the machine emulation until data is copied
|
2016-11-21 04:14:52 +00:00
|
|
|
output_mutex_.lock();
|
2016-06-03 02:29:09 +00:00
|
|
|
|
|
|
|
// release the mapping, giving up on trying to draw if data has been lost
|
2016-11-17 04:26:04 +00:00
|
|
|
ArrayBuilder::Submission array_submission = array_builder.submit();
|
2016-06-03 02:29:09 +00:00
|
|
|
|
2016-11-16 03:10:34 +00:00
|
|
|
// upload new source pixels, if any
|
2016-11-16 15:13:06 +00:00
|
|
|
glActiveTexture(source_data_texture_unit);
|
2016-11-17 04:26:04 +00:00
|
|
|
texture_builder.submit();
|
2016-02-14 04:50:18 +00:00
|
|
|
|
2016-11-16 03:22:12 +00:00
|
|
|
// buffer usage restart from 0 for the next time around
|
2016-11-21 04:14:52 +00:00
|
|
|
composite_src_output_y_ = 0;
|
2016-11-16 03:22:12 +00:00
|
|
|
|
2016-06-03 02:29:09 +00:00
|
|
|
// data having been grabbed, allow the machine to continue
|
2016-11-21 04:14:52 +00:00
|
|
|
output_mutex_.unlock();
|
2016-06-03 02:29:09 +00:00
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
struct RenderStage {
|
|
|
|
OpenGL::Shader *const shader;
|
2017-01-08 16:13:20 +00:00
|
|
|
OpenGL::TextureTarget *const target;
|
2016-05-03 01:05:58 +00:00
|
|
|
float clear_colour[3];
|
|
|
|
};
|
|
|
|
|
2016-11-21 03:40:13 +00:00
|
|
|
// for composite video, go through four steps to get to something that can be painted to the output
|
2016-05-03 01:05:58 +00:00
|
|
|
RenderStage composite_render_stages[] =
|
|
|
|
{
|
2017-01-08 16:13:20 +00:00
|
|
|
{composite_input_shader_program_.get(), composite_texture_.get(), {0.0, 0.0, 0.0}},
|
|
|
|
{composite_separation_filter_program_.get(), separated_texture_.get(), {0.0, 0.5, 0.5}},
|
|
|
|
{composite_chrominance_filter_shader_program_.get(), filtered_texture_.get(), {0.0, 0.0, 0.0}},
|
2016-05-03 01:05:58 +00:00
|
|
|
{nullptr}
|
|
|
|
};
|
|
|
|
|
2016-11-21 03:40:13 +00:00
|
|
|
// for RGB video, there's only two steps
|
2016-05-03 01:05:58 +00:00
|
|
|
RenderStage rgb_render_stages[] =
|
|
|
|
{
|
2017-01-08 16:13:20 +00:00
|
|
|
{rgb_input_shader_program_.get(), composite_texture_.get(), {0.0, 0.0, 0.0}},
|
|
|
|
{rgb_filter_shader_program_.get(), filtered_texture_.get(), {0.0, 0.0, 0.0}},
|
2016-05-03 01:05:58 +00:00
|
|
|
{nullptr}
|
|
|
|
};
|
|
|
|
|
2017-01-26 02:25:03 +00:00
|
|
|
RenderStage *active_pipeline = get_is_television_output() ? composite_render_stages : rgb_render_stages;
|
2016-05-03 01:05:58 +00:00
|
|
|
|
2016-11-17 04:26:04 +00:00
|
|
|
if(array_submission.input_size || array_submission.output_size)
|
2016-04-17 21:17:59 +00:00
|
|
|
{
|
2016-05-03 01:05:58 +00:00
|
|
|
// all drawing will be from the source vertex array and without blending
|
2016-11-21 04:14:52 +00:00
|
|
|
glBindVertexArray(source_vertex_array_);
|
2016-05-03 01:05:58 +00:00
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
2017-01-08 16:13:20 +00:00
|
|
|
#ifdef GL_NV_texture_barrier
|
|
|
|
if(work_texture_)
|
|
|
|
{
|
|
|
|
work_texture_->bind_framebuffer();
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while(active_pipeline->shader)
|
2016-05-03 01:05:58 +00:00
|
|
|
{
|
2016-11-21 03:40:13 +00:00
|
|
|
// switch to the framebuffer and shader associated with this stage
|
|
|
|
active_pipeline->shader->bind();
|
|
|
|
|
2017-01-08 16:13:20 +00:00
|
|
|
if(!work_texture_)
|
2016-11-21 03:26:07 +00:00
|
|
|
{
|
2017-01-08 16:13:20 +00:00
|
|
|
active_pipeline->target->bind_framebuffer();
|
|
|
|
|
|
|
|
// if this is the final stage before painting to the CRT, clear the framebuffer before drawing in order to blank out
|
|
|
|
// those portions for which no input was provided
|
2017-01-09 00:50:31 +00:00
|
|
|
// if(!active_pipeline[1].shader)
|
|
|
|
// {
|
2017-01-08 16:13:20 +00:00
|
|
|
glClearColor(active_pipeline->clear_colour[0], active_pipeline->clear_colour[1], active_pipeline->clear_colour[2], 1.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2017-01-09 00:50:31 +00:00
|
|
|
// }
|
2016-11-21 03:26:07 +00:00
|
|
|
}
|
2016-04-22 01:32:36 +00:00
|
|
|
|
2016-11-21 03:40:13 +00:00
|
|
|
// draw
|
2017-01-12 02:18:41 +00:00
|
|
|
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, (GLsizei)array_submission.input_size / SourceVertexSize);
|
2016-05-03 01:05:58 +00:00
|
|
|
|
|
|
|
active_pipeline++;
|
2017-01-08 16:13:20 +00:00
|
|
|
#ifdef GL_NV_texture_barrier
|
|
|
|
glTextureBarrierNV();
|
|
|
|
#endif
|
2016-04-17 21:17:59 +00:00
|
|
|
}
|
2016-05-03 01:05:58 +00:00
|
|
|
|
2016-11-21 03:40:13 +00:00
|
|
|
// prepare to transfer to framebuffer
|
2016-11-21 04:14:52 +00:00
|
|
|
framebuffer_->bind_framebuffer();
|
2016-05-03 01:05:58 +00:00
|
|
|
|
2016-11-21 03:40:13 +00:00
|
|
|
// draw from the output array buffer, with blending
|
2016-11-21 04:14:52 +00:00
|
|
|
glBindVertexArray(output_vertex_array_);
|
2016-11-20 02:53:35 +00:00
|
|
|
glEnable(GL_BLEND);
|
2016-05-03 01:05:58 +00:00
|
|
|
|
2016-11-21 03:40:13 +00:00
|
|
|
// update uniforms, then bind the target
|
2016-11-21 04:14:52 +00:00
|
|
|
if(last_output_width_ != output_width || last_output_height_ != output_height)
|
2016-05-03 01:05:58 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
output_shader_program_->set_output_size(output_width, output_height, visible_area_);
|
|
|
|
last_output_width_ = output_width;
|
|
|
|
last_output_height_ = output_height;
|
2016-05-03 01:05:58 +00:00
|
|
|
}
|
2016-11-21 04:14:52 +00:00
|
|
|
output_shader_program_->bind();
|
2016-05-03 01:05:58 +00:00
|
|
|
|
|
|
|
// draw
|
2016-11-17 04:26:04 +00:00
|
|
|
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, (GLsizei)array_submission.output_size / OutputVertexSize);
|
2016-04-17 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-08 16:13:20 +00:00
|
|
|
#ifdef GL_NV_texture_barrier
|
|
|
|
glTextureBarrierNV();
|
|
|
|
#endif
|
|
|
|
|
2016-05-02 02:28:33 +00:00
|
|
|
// copy framebuffer to the intended place
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
glViewport(0, 0, (GLsizei)output_width, (GLsizei)output_height);
|
2016-05-07 22:37:18 +00:00
|
|
|
|
2016-05-12 02:11:01 +00:00
|
|
|
glActiveTexture(pixel_accumulation_texture_unit);
|
2016-11-21 04:14:52 +00:00
|
|
|
framebuffer_->bind_texture();
|
|
|
|
framebuffer_->draw((float)output_width / (float)output_height);
|
2016-05-02 02:28:33 +00:00
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
fence_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
|
|
draw_mutex_.unlock();
|
2016-04-17 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 02:11:01 +00:00
|
|
|
void OpenGLOutputBuilder::reset_all_OpenGL_state()
|
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
composite_input_shader_program_ = nullptr;
|
|
|
|
composite_separation_filter_program_ = nullptr;
|
|
|
|
composite_chrominance_filter_shader_program_ = nullptr;
|
|
|
|
rgb_input_shader_program_ = nullptr;
|
|
|
|
rgb_filter_shader_program_ = nullptr;
|
|
|
|
output_shader_program_ = nullptr;
|
|
|
|
framebuffer_ = nullptr;
|
|
|
|
last_output_width_ = last_output_height_ = 0;
|
2016-05-12 02:11:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
void OpenGLOutputBuilder::set_openGL_context_will_change(bool should_delete_resources)
|
2016-02-05 03:28:50 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
output_mutex_.lock();
|
2016-05-12 02:11:01 +00:00
|
|
|
reset_all_OpenGL_state();
|
2016-11-21 04:14:52 +00:00
|
|
|
output_mutex_.unlock();
|
2016-02-05 03:28:50 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
void OpenGLOutputBuilder::set_composite_sampling_function(const char *shader)
|
2016-02-05 03:28:50 +00:00
|
|
|
{
|
2017-01-09 00:50:31 +00:00
|
|
|
std::lock_guard<std::mutex> lock_guard(output_mutex_);
|
2016-11-21 04:14:52 +00:00
|
|
|
composite_shader_ = strdup(shader);
|
2016-05-12 02:11:01 +00:00
|
|
|
reset_all_OpenGL_state();
|
2016-02-05 03:28:50 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
void OpenGLOutputBuilder::set_rgb_sampling_function(const char *shader)
|
2016-02-05 03:28:50 +00:00
|
|
|
{
|
2017-01-09 00:50:31 +00:00
|
|
|
std::lock_guard<std::mutex> lock_guard(output_mutex_);
|
2016-11-21 04:14:52 +00:00
|
|
|
rgb_shader_ = strdup(shader);
|
2016-05-12 02:11:01 +00:00
|
|
|
reset_all_OpenGL_state();
|
2016-02-05 03:57:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 00:21:04 +00:00
|
|
|
#pragma mark - Program compilation
|
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
void OpenGLOutputBuilder::prepare_composite_input_shaders()
|
2016-03-08 02:04:04 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
composite_input_shader_program_ = OpenGL::IntermediateShader::make_source_conversion_shader(composite_shader_, rgb_shader_);
|
|
|
|
composite_input_shader_program_->set_source_texture_unit(source_data_texture_unit);
|
|
|
|
composite_input_shader_program_->set_output_size(IntermediateBufferWidth, IntermediateBufferHeight);
|
2016-04-22 00:21:34 +00:00
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
composite_separation_filter_program_ = OpenGL::IntermediateShader::make_chroma_luma_separation_shader();
|
2017-01-08 16:13:20 +00:00
|
|
|
composite_separation_filter_program_->set_source_texture_unit(work_texture_ ? work_texture_unit : composite_texture_unit);
|
2016-11-21 04:14:52 +00:00
|
|
|
composite_separation_filter_program_->set_output_size(IntermediateBufferWidth, IntermediateBufferHeight);
|
2016-04-22 00:21:34 +00:00
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
composite_chrominance_filter_shader_program_ = OpenGL::IntermediateShader::make_chroma_filter_shader();
|
2017-01-08 16:13:20 +00:00
|
|
|
composite_chrominance_filter_shader_program_->set_source_texture_unit(work_texture_ ? work_texture_unit : separated_texture_unit);
|
2016-11-21 04:14:52 +00:00
|
|
|
composite_chrominance_filter_shader_program_->set_output_size(IntermediateBufferWidth, IntermediateBufferHeight);
|
2017-01-08 19:47:16 +00:00
|
|
|
|
|
|
|
if(work_texture_)
|
|
|
|
{
|
|
|
|
composite_input_shader_program_->set_is_double_height(true, 0.0f, 0.0f);
|
|
|
|
composite_separation_filter_program_->set_is_double_height(true, 0.0f, 0.5f);
|
|
|
|
composite_chrominance_filter_shader_program_->set_is_double_height(true, 0.5f, 0.0f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
composite_input_shader_program_->set_is_double_height(false);
|
|
|
|
composite_separation_filter_program_->set_is_double_height(false);
|
|
|
|
composite_chrominance_filter_shader_program_->set_is_double_height(false);
|
|
|
|
}
|
2016-03-08 02:04:04 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
void OpenGLOutputBuilder::prepare_rgb_input_shaders()
|
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
if(rgb_shader_)
|
2016-05-03 01:05:58 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
rgb_input_shader_program_ = OpenGL::IntermediateShader::make_rgb_source_shader(rgb_shader_);
|
|
|
|
rgb_input_shader_program_->set_source_texture_unit(source_data_texture_unit);
|
|
|
|
rgb_input_shader_program_->set_output_size(IntermediateBufferWidth, IntermediateBufferHeight);
|
2016-05-03 01:05:58 +00:00
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
rgb_filter_shader_program_ = OpenGL::IntermediateShader::make_rgb_filter_shader();
|
|
|
|
rgb_filter_shader_program_->set_source_texture_unit(composite_texture_unit);
|
|
|
|
rgb_filter_shader_program_->set_output_size(IntermediateBufferWidth, IntermediateBufferHeight);
|
2016-05-03 01:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 20:17:23 +00:00
|
|
|
void OpenGLOutputBuilder::prepare_source_vertex_array()
|
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
if(composite_input_shader_program_)
|
2016-04-17 20:17:23 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
glBindVertexArray(source_vertex_array_);
|
2016-11-17 04:26:04 +00:00
|
|
|
array_builder.bind_input();
|
2016-05-10 23:50:12 +00:00
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
composite_input_shader_program_->enable_vertex_attribute_with_pointer("inputStart", 2, GL_UNSIGNED_SHORT, GL_FALSE, SourceVertexSize, (void *)SourceVertexOffsetOfInputStart, 1);
|
|
|
|
composite_input_shader_program_->enable_vertex_attribute_with_pointer("outputStart", 2, GL_UNSIGNED_SHORT, GL_FALSE, SourceVertexSize, (void *)SourceVertexOffsetOfOutputStart, 1);
|
|
|
|
composite_input_shader_program_->enable_vertex_attribute_with_pointer("ends", 2, GL_UNSIGNED_SHORT, GL_FALSE, SourceVertexSize, (void *)SourceVertexOffsetOfEnds, 1);
|
|
|
|
composite_input_shader_program_->enable_vertex_attribute_with_pointer("phaseTimeAndAmplitude", 3, GL_UNSIGNED_BYTE, GL_FALSE, SourceVertexSize, (void *)SourceVertexOffsetOfPhaseTimeAndAmplitude, 1);
|
2016-04-17 20:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
void OpenGLOutputBuilder::prepare_output_shader()
|
2016-04-17 22:08:05 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
output_shader_program_ = OpenGL::OutputShader::make_shader("", "texture(texID, srcCoordinatesVarying).rgb", false);
|
2017-01-08 16:13:20 +00:00
|
|
|
output_shader_program_->set_source_texture_unit(work_texture_ ? work_texture_unit : filtered_texture_unit);
|
2017-01-11 03:08:07 +00:00
|
|
|
// output_shader_program_->set_source_texture_unit(composite_texture_unit);
|
2017-01-08 19:47:16 +00:00
|
|
|
output_shader_program_->set_origin_is_double_height(!!work_texture_);
|
2016-02-14 04:50:18 +00:00
|
|
|
}
|
2016-02-05 03:57:46 +00:00
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
void OpenGLOutputBuilder::prepare_output_vertex_array()
|
2016-02-14 04:50:18 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
if(output_shader_program_)
|
2016-03-08 02:42:21 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
glBindVertexArray(output_vertex_array_);
|
2016-11-17 04:26:04 +00:00
|
|
|
array_builder.bind_output();
|
2016-11-21 04:14:52 +00:00
|
|
|
output_shader_program_->enable_vertex_attribute_with_pointer("horizontal", 2, GL_UNSIGNED_SHORT, GL_FALSE, OutputVertexSize, (void *)OutputVertexOffsetOfHorizontal, 1);
|
|
|
|
output_shader_program_->enable_vertex_attribute_with_pointer("vertical", 2, GL_UNSIGNED_SHORT, GL_FALSE, OutputVertexSize, (void *)OutputVertexOffsetOfVertical, 1);
|
2016-03-08 02:42:21 +00:00
|
|
|
}
|
2016-02-05 03:28:50 +00:00
|
|
|
}
|
2016-02-08 00:21:22 +00:00
|
|
|
|
2016-04-24 22:58:31 +00:00
|
|
|
#pragma mark - Public Configuration
|
2016-03-05 19:36:12 +00:00
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
void OpenGLOutputBuilder::set_output_device(OutputDevice output_device)
|
2016-02-08 00:21:22 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
if(output_device_ != output_device)
|
2016-03-05 19:36:12 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
output_device_ = output_device;
|
|
|
|
composite_src_output_y_ = 0;
|
|
|
|
last_output_width_ = 0;
|
|
|
|
last_output_height_ = 0;
|
2017-01-25 01:48:54 +00:00
|
|
|
set_output_shader_width();
|
2016-03-05 19:36:12 +00:00
|
|
|
}
|
2016-02-08 00:21:22 +00:00
|
|
|
}
|
2016-03-08 02:22:47 +00:00
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
void OpenGLOutputBuilder::set_timing(unsigned int input_frequency, unsigned int cycles_per_line, unsigned int height_of_display, unsigned int horizontal_scan_period, unsigned int vertical_scan_period, unsigned int vertical_period_divider)
|
2016-04-24 22:58:31 +00:00
|
|
|
{
|
2016-11-21 04:14:52 +00:00
|
|
|
output_mutex_.lock();
|
|
|
|
input_frequency_ = input_frequency;
|
|
|
|
cycles_per_line_ = cycles_per_line;
|
|
|
|
height_of_display_ = height_of_display;
|
|
|
|
horizontal_scan_period_ = horizontal_scan_period;
|
|
|
|
vertical_scan_period_ = vertical_scan_period;
|
|
|
|
vertical_period_divider_ = vertical_period_divider;
|
2016-03-08 02:22:47 +00:00
|
|
|
|
2016-04-24 22:58:31 +00:00
|
|
|
set_timing_uniforms();
|
2016-11-21 04:14:52 +00:00
|
|
|
output_mutex_.unlock();
|
2016-04-24 22:58:31 +00:00
|
|
|
}
|
2016-03-08 02:22:47 +00:00
|
|
|
|
2016-04-24 22:58:31 +00:00
|
|
|
#pragma mark - Internal Configuration
|
|
|
|
|
2016-04-24 23:16:23 +00:00
|
|
|
void OpenGLOutputBuilder::set_colour_space_uniforms()
|
|
|
|
{
|
|
|
|
GLfloat rgbToYUV[] = {0.299f, -0.14713f, 0.615f, 0.587f, -0.28886f, -0.51499f, 0.114f, 0.436f, -0.10001f};
|
|
|
|
GLfloat yuvToRGB[] = {1.0f, 1.0f, 1.0f, 0.0f, -0.39465f, 2.03211f, 1.13983f, -0.58060f, 0.0f};
|
|
|
|
|
2016-04-24 23:29:30 +00:00
|
|
|
GLfloat rgbToYIQ[] = {0.299f, 0.596f, 0.211f, 0.587f, -0.274f, -0.523f, 0.114f, -0.322f, 0.312f};
|
2016-04-24 23:16:23 +00:00
|
|
|
GLfloat yiqToRGB[] = {1.0f, 1.0f, 1.0f, 0.956f, -0.272f, -1.106f, 0.621f, -0.647f, 1.703f};
|
|
|
|
|
|
|
|
GLfloat *fromRGB, *toRGB;
|
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
switch(colour_space_)
|
2016-04-24 23:16:23 +00:00
|
|
|
{
|
|
|
|
case ColourSpace::YIQ:
|
|
|
|
fromRGB = rgbToYIQ;
|
|
|
|
toRGB = yiqToRGB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ColourSpace::YUV:
|
|
|
|
fromRGB = rgbToYUV;
|
|
|
|
toRGB = yuvToRGB;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-21 04:14:52 +00:00
|
|
|
if(composite_input_shader_program_) composite_input_shader_program_->set_colour_conversion_matrices(fromRGB, toRGB);
|
2017-01-07 21:02:33 +00:00
|
|
|
if(composite_separation_filter_program_) composite_separation_filter_program_->set_colour_conversion_matrices(fromRGB, toRGB);
|
2016-11-21 04:14:52 +00:00
|
|
|
if(composite_chrominance_filter_shader_program_) composite_chrominance_filter_shader_program_->set_colour_conversion_matrices(fromRGB, toRGB);
|
2016-04-24 23:16:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 01:48:54 +00:00
|
|
|
float OpenGLOutputBuilder::get_composite_output_width() const
|
|
|
|
{
|
|
|
|
return ((float)colour_cycle_numerator_ * 4.0f) / (float)(colour_cycle_denominator_ * IntermediateBufferWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLOutputBuilder::set_output_shader_width()
|
|
|
|
{
|
|
|
|
if(output_shader_program_)
|
|
|
|
{
|
2017-01-26 02:25:03 +00:00
|
|
|
const float width = get_is_television_output() ? get_composite_output_width() : 1.0f;
|
2017-01-25 01:48:54 +00:00
|
|
|
output_shader_program_->set_input_width_scaler(width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-24 22:58:31 +00:00
|
|
|
void OpenGLOutputBuilder::set_timing_uniforms()
|
|
|
|
{
|
2017-01-07 17:38:00 +00:00
|
|
|
const float colour_subcarrier_frequency = (float)colour_cycle_numerator_ / (float)colour_cycle_denominator_;
|
2017-01-25 01:48:54 +00:00
|
|
|
const float output_width = get_composite_output_width();
|
2017-01-07 17:38:00 +00:00
|
|
|
const float sample_cycles_per_line = cycles_per_line_ / output_width;
|
|
|
|
|
2017-01-04 03:16:52 +00:00
|
|
|
if(composite_separation_filter_program_)
|
|
|
|
{
|
|
|
|
composite_separation_filter_program_->set_width_scalers(output_width, output_width);
|
2017-01-04 03:32:07 +00:00
|
|
|
composite_separation_filter_program_->set_separation_frequency(sample_cycles_per_line, colour_subcarrier_frequency);
|
2017-01-08 21:48:02 +00:00
|
|
|
composite_separation_filter_program_->set_extension(6.0f);
|
2017-01-04 03:16:52 +00:00
|
|
|
}
|
|
|
|
if(composite_chrominance_filter_shader_program_)
|
|
|
|
{
|
|
|
|
composite_chrominance_filter_shader_program_->set_width_scalers(output_width, output_width);
|
2017-01-08 21:48:02 +00:00
|
|
|
composite_chrominance_filter_shader_program_->set_extension(5.0f);
|
2017-01-04 03:16:52 +00:00
|
|
|
}
|
|
|
|
if(rgb_filter_shader_program_)
|
|
|
|
{
|
2017-01-25 01:48:54 +00:00
|
|
|
rgb_filter_shader_program_->set_width_scalers(1.0f, 1.0f);
|
2017-01-04 03:32:07 +00:00
|
|
|
rgb_filter_shader_program_->set_filter_coefficients(sample_cycles_per_line, (float)input_frequency_ * 0.5f);
|
2017-01-04 03:16:52 +00:00
|
|
|
}
|
|
|
|
if(output_shader_program_)
|
|
|
|
{
|
2017-01-25 01:48:54 +00:00
|
|
|
set_output_shader_width();
|
2017-01-04 03:16:52 +00:00
|
|
|
output_shader_program_->set_timing(height_of_display_, cycles_per_line_, horizontal_scan_period_, vertical_scan_period_, vertical_period_divider_);
|
|
|
|
}
|
|
|
|
if(composite_input_shader_program_)
|
|
|
|
{
|
|
|
|
composite_input_shader_program_->set_width_scalers(1.0f, output_width);
|
2017-01-08 21:48:02 +00:00
|
|
|
composite_input_shader_program_->set_extension(0.0f);
|
2017-01-04 03:16:52 +00:00
|
|
|
}
|
|
|
|
if(rgb_input_shader_program_)
|
|
|
|
{
|
2017-01-25 01:48:54 +00:00
|
|
|
rgb_input_shader_program_->set_width_scalers(1.0f, 1.0f);
|
2017-01-04 03:16:52 +00:00
|
|
|
}
|
2016-04-24 22:58:31 +00:00
|
|
|
}
|