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"
|
2016-02-05 03:57:46 +00:00
|
|
|
#include <stdlib.h>
|
2016-02-28 01:37:41 +00:00
|
|
|
#include <math.h>
|
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-04-13 02:31:13 +00:00
|
|
|
static const GLint internalFormatForDepth(size_t depth)
|
|
|
|
{
|
|
|
|
switch(depth)
|
|
|
|
{
|
|
|
|
default: return GL_FALSE;
|
|
|
|
case 1: return GL_R8UI;
|
|
|
|
case 2: return GL_RG8UI;
|
|
|
|
case 3: return GL_RGB8UI;
|
|
|
|
case 4: return GL_RGBA8UI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GLenum formatForDepth(size_t depth)
|
|
|
|
{
|
|
|
|
switch(depth)
|
|
|
|
{
|
|
|
|
default: return GL_FALSE;
|
|
|
|
case 1: return GL_RED_INTEGER;
|
|
|
|
case 2: return GL_RG_INTEGER;
|
|
|
|
case 3: return GL_RGB_INTEGER;
|
|
|
|
case 4: return GL_RGBA_INTEGER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 01:41:39 +00:00
|
|
|
static int getCircularRanges(GLsizei start, GLsizei end, GLsizei buffer_length, GLsizei granularity, GLsizei *ranges)
|
2016-04-21 01:05:32 +00:00
|
|
|
{
|
2016-05-05 01:08:38 +00:00
|
|
|
start -= start%granularity;
|
|
|
|
end -= end%granularity;
|
2016-04-27 01:41:39 +00:00
|
|
|
|
2016-04-21 01:05:32 +00:00
|
|
|
GLsizei length = end - start;
|
|
|
|
if(!length) return 0;
|
2016-05-05 01:08:38 +00:00
|
|
|
if(length >= buffer_length)
|
2016-04-21 01:05:32 +00:00
|
|
|
{
|
|
|
|
ranges[0] = 0;
|
|
|
|
ranges[1] = buffer_length;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ranges[0] = start % buffer_length;
|
2016-05-05 01:08:38 +00:00
|
|
|
if(ranges[0]+length <= buffer_length)
|
2016-04-21 01:05:32 +00:00
|
|
|
{
|
|
|
|
ranges[1] = length;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ranges[1] = buffer_length - ranges[0];
|
|
|
|
ranges[2] = 0;
|
|
|
|
ranges[3] = length - ranges[1];
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-05-03 11:46:40 +00:00
|
|
|
static const GLenum composite_texture_unit = GL_TEXTURE0;
|
|
|
|
static const GLenum separated_texture_unit = GL_TEXTURE1;
|
|
|
|
static const GLenum filtered_y_texture_unit = GL_TEXTURE2;
|
|
|
|
static const GLenum filtered_texture_unit = GL_TEXTURE3;
|
|
|
|
static const GLenum source_data_texture_unit = GL_TEXTURE4;
|
|
|
|
static const GLenum pixel_accumulation_texture_unit = GL_TEXTURE5;
|
2016-03-09 03:40:23 +00:00
|
|
|
}
|
2016-02-06 02:35:39 +00:00
|
|
|
|
2016-03-19 21:37:55 +00:00
|
|
|
OpenGLOutputBuilder::OpenGLOutputBuilder(unsigned int buffer_depth) :
|
2016-03-09 03:40:23 +00:00
|
|
|
_output_mutex(new std::mutex),
|
|
|
|
_visible_area(Rect(0, 0, 1, 1)),
|
|
|
|
_composite_src_output_y(0),
|
2016-04-24 11:00:22 +00:00
|
|
|
_cleared_composite_output_y(0),
|
2016-03-09 03:40:23 +00:00
|
|
|
_composite_shader(nullptr),
|
2016-03-19 01:11:09 +00:00
|
|
|
_rgb_shader(nullptr),
|
2016-03-19 01:19:11 +00:00
|
|
|
_output_buffer_data(nullptr),
|
2016-04-14 02:14:18 +00:00
|
|
|
_source_buffer_data(nullptr),
|
2016-04-17 19:51:28 +00:00
|
|
|
_output_buffer_data_pointer(0),
|
2016-04-27 01:41:39 +00:00
|
|
|
_drawn_output_buffer_data_pointer(0),
|
2016-04-24 11:00:22 +00:00
|
|
|
_source_buffer_data_pointer(0),
|
2016-04-29 02:04:47 +00:00
|
|
|
_drawn_source_buffer_data_pointer(0),
|
|
|
|
_last_output_width(0),
|
|
|
|
_last_output_height(0)
|
2016-03-09 03:40:23 +00:00
|
|
|
{
|
2016-03-19 21:37:55 +00:00
|
|
|
_buffer_builder = std::unique_ptr<CRTInputBufferBuilder>(new CRTInputBufferBuilder(buffer_depth));
|
2016-04-13 02:31:13 +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:48:47 +00:00
|
|
|
// Create intermediate textures and bind to slots 0, 1 and 2
|
2016-05-01 20:17:52 +00:00
|
|
|
compositeTexture = std::unique_ptr<OpenGL::TextureTarget>(new OpenGL::TextureTarget(IntermediateBufferWidth, IntermediateBufferHeight, composite_texture_unit));
|
2016-05-03 11:46:40 +00:00
|
|
|
separatedTexture = std::unique_ptr<OpenGL::TextureTarget>(new OpenGL::TextureTarget(IntermediateBufferWidth, IntermediateBufferHeight, separated_texture_unit));
|
2016-05-01 20:17:52 +00:00
|
|
|
filteredYTexture = std::unique_ptr<OpenGL::TextureTarget>(new OpenGL::TextureTarget(IntermediateBufferWidth, IntermediateBufferHeight, filtered_y_texture_unit));
|
|
|
|
filteredTexture = std::unique_ptr<OpenGL::TextureTarget>(new OpenGL::TextureTarget(IntermediateBufferWidth, IntermediateBufferHeight, filtered_texture_unit));
|
2016-04-13 02:48:47 +00:00
|
|
|
|
2016-04-13 02:31:13 +00:00
|
|
|
// create the surce texture
|
|
|
|
glGenTextures(1, &textureName);
|
2016-04-20 02:53:39 +00:00
|
|
|
glActiveTexture(source_data_texture_unit);
|
2016-04-13 02:31:13 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, textureName);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2016-05-04 00:56:47 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, internalFormatForDepth(_buffer_builder->get_bytes_per_pixel()), InputBufferBuilderWidth, InputBufferBuilderHeight, 0, formatForDepth(_buffer_builder->get_bytes_per_pixel()), GL_UNSIGNED_BYTE, nullptr);
|
2016-04-13 02:31:13 +00:00
|
|
|
|
|
|
|
// create the output vertex array
|
|
|
|
glGenVertexArrays(1, &output_vertex_array);
|
|
|
|
|
|
|
|
// create a buffer for output vertex attributes
|
|
|
|
glGenBuffers(1, &output_array_buffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, output_array_buffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, OutputVertexBufferDataSize, NULL, GL_STREAM_DRAW);
|
|
|
|
|
|
|
|
// map that buffer too, for any CRT activity that may occur before the first draw
|
2016-05-02 02:33:00 +00:00
|
|
|
_output_buffer_data = (uint8_t *)glMapBufferRange(GL_ARRAY_BUFFER, 0, OutputVertexBufferDataSize, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_FLUSH_EXPLICIT_BIT);
|
2016-04-14 02:14:18 +00:00
|
|
|
|
2016-04-17 21:17:59 +00:00
|
|
|
// create the source vertex array
|
|
|
|
glGenVertexArrays(1, &source_vertex_array);
|
|
|
|
|
2016-04-14 02:14:18 +00:00
|
|
|
// create a buffer for source vertex attributes
|
|
|
|
glGenBuffers(1, &source_array_buffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, source_array_buffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, SourceVertexBufferDataSize, NULL, GL_STREAM_DRAW);
|
|
|
|
|
|
|
|
// map that buffer too, for any CRT activity that may occur before the first draw
|
2016-05-02 02:33:00 +00:00
|
|
|
_source_buffer_data = (uint8_t *)glMapBufferRange(GL_ARRAY_BUFFER, 0, SourceVertexBufferDataSize, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_FLUSH_EXPLICIT_BIT);
|
2016-03-09 01:49:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
OpenGLOutputBuilder::~OpenGLOutputBuilder()
|
|
|
|
{
|
2016-03-19 21:07:05 +00:00
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
2016-03-19 21:37:55 +00:00
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
|
|
|
glDeleteTextures(1, &textureName);
|
|
|
|
glDeleteBuffers(1, &output_array_buffer);
|
2016-04-14 02:14:18 +00:00
|
|
|
glDeleteBuffers(1, &source_array_buffer);
|
2016-03-19 21:37:55 +00:00
|
|
|
glDeleteVertexArrays(1, &output_vertex_array);
|
2016-03-09 01:49:07 +00:00
|
|
|
|
2016-03-09 03:40:23 +00:00
|
|
|
free(_composite_shader);
|
|
|
|
free(_rgb_shader);
|
2016-03-08 02:04:04 +00:00
|
|
|
}
|
|
|
|
|
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-05-03 22:45:55 +00:00
|
|
|
// lock down any further work on the current frame
|
|
|
|
_output_mutex->lock();
|
|
|
|
|
2016-02-13 03:31:05 +00:00
|
|
|
// establish essentials
|
2016-05-03 01:05:58 +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-04-24 22:58:31 +00:00
|
|
|
|
2016-03-07 23:55:15 +00:00
|
|
|
// This should return either an actual framebuffer number, if this is a target with a framebuffer intended for output,
|
|
|
|
// or 0 if no framebuffer is bound, in which case 0 is also what we want to supply to bind the implied framebuffer. So
|
|
|
|
// it works either way.
|
2016-05-07 22:37:18 +00:00
|
|
|
// glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint *)&defaultFramebuffer);
|
2016-04-13 02:48:47 +00:00
|
|
|
|
|
|
|
// TODO: is this sustainable, cross-platform? If so, why store it at all?
|
2016-05-07 22:37:18 +00:00
|
|
|
// defaultFramebuffer = 0;
|
2016-02-13 03:31:05 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 00:56:47 +00:00
|
|
|
// determine how many lines are newly reclaimed; they'll need to be cleared
|
|
|
|
GLsizei clearing_zones[4], source_drawing_zones[4];
|
2016-05-05 01:27:10 +00:00
|
|
|
GLsizei output_drawing_zones[4];
|
2016-05-05 01:08:38 +00:00
|
|
|
int number_of_clearing_zones = getCircularRanges(_cleared_composite_output_y, _composite_src_output_y, IntermediateBufferHeight, 1, clearing_zones);
|
2016-05-04 00:56:47 +00:00
|
|
|
int number_of_source_drawing_zones = getCircularRanges(_drawn_source_buffer_data_pointer, _source_buffer_data_pointer, SourceVertexBufferDataSize, 2*SourceVertexSize, source_drawing_zones);
|
|
|
|
int number_of_output_drawing_zones = getCircularRanges(_drawn_output_buffer_data_pointer, _output_buffer_data_pointer, OutputVertexBufferDataSize, 6*OutputVertexSize, output_drawing_zones);
|
2016-05-08 20:43:11 +00:00
|
|
|
uint16_t completed_texture_y = _buffer_builder->get_and_finalise_current_line();
|
2016-05-04 00:56:47 +00:00
|
|
|
|
|
|
|
_composite_src_output_y %= IntermediateBufferHeight;
|
|
|
|
_source_buffer_data_pointer %= SourceVertexBufferDataSize;
|
|
|
|
_output_buffer_data_pointer %= OutputVertexBufferDataSize;
|
|
|
|
|
|
|
|
_cleared_composite_output_y = _composite_src_output_y;
|
|
|
|
_drawn_source_buffer_data_pointer = _source_buffer_data_pointer;
|
|
|
|
_drawn_output_buffer_data_pointer = _output_buffer_data_pointer;
|
|
|
|
|
2016-05-01 23:22:24 +00:00
|
|
|
// release the mapping, giving up on trying to draw if data has been lost
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, output_array_buffer);
|
2016-05-04 00:56:47 +00:00
|
|
|
for(int c = 0; c < number_of_output_drawing_zones; c++)
|
|
|
|
{
|
|
|
|
glFlushMappedBufferRange(GL_ARRAY_BUFFER, output_drawing_zones[c*2], output_drawing_zones[c*2 + 1]);
|
|
|
|
}
|
2016-05-01 23:22:24 +00:00
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
2016-05-04 00:56:47 +00:00
|
|
|
|
|
|
|
// bind and flush the source array buffer
|
2016-05-01 23:22:24 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, source_array_buffer);
|
2016-05-04 00:56:47 +00:00
|
|
|
for(int c = 0; c < number_of_source_drawing_zones; c++)
|
|
|
|
{
|
|
|
|
glFlushMappedBufferRange(GL_ARRAY_BUFFER, source_drawing_zones[c*2], source_drawing_zones[c*2 + 1]);
|
|
|
|
}
|
2016-05-01 23:22:24 +00:00
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
2016-05-04 00:56:47 +00:00
|
|
|
|
2016-05-01 20:17:52 +00:00
|
|
|
// make sure there's a target to draw to
|
|
|
|
if(!framebuffer || framebuffer->get_height() != output_height || framebuffer->get_width() != output_width)
|
|
|
|
{
|
|
|
|
std::unique_ptr<OpenGL::TextureTarget> new_framebuffer = std::unique_ptr<OpenGL::TextureTarget>(new OpenGL::TextureTarget((GLsizei)output_width, (GLsizei)output_height, pixel_accumulation_texture_unit));
|
|
|
|
if(framebuffer)
|
|
|
|
{
|
|
|
|
new_framebuffer->bind_framebuffer();
|
2016-05-02 02:28:33 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
glActiveTexture(pixel_accumulation_texture_unit);
|
|
|
|
framebuffer->bind_texture();
|
2016-05-01 20:17:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
framebuffer = std::move(new_framebuffer);
|
|
|
|
}
|
|
|
|
|
2016-05-04 00:56:47 +00:00
|
|
|
// upload new source pixels
|
2016-05-05 01:27:10 +00:00
|
|
|
if(completed_texture_y)
|
2016-04-13 02:31:13 +00:00
|
|
|
{
|
2016-05-04 02:22:12 +00:00
|
|
|
glActiveTexture(source_data_texture_unit);
|
2016-05-05 01:27:10 +00:00
|
|
|
glTexSubImage2D( GL_TEXTURE_2D, 0,
|
|
|
|
0, 0,
|
|
|
|
InputBufferBuilderWidth, completed_texture_y,
|
|
|
|
formatForDepth(_buffer_builder->get_bytes_per_pixel()), GL_UNSIGNED_BYTE,
|
|
|
|
_buffer_builder->get_image_pointer());
|
2016-04-13 02:31:13 +00:00
|
|
|
}
|
2016-02-14 04:50:18 +00:00
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
struct RenderStage {
|
|
|
|
OpenGL::TextureTarget *const target;
|
|
|
|
OpenGL::Shader *const shader;
|
|
|
|
float clear_colour[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
RenderStage composite_render_stages[] =
|
|
|
|
{
|
|
|
|
{compositeTexture.get(), composite_input_shader_program.get(), {0.0, 0.0, 0.0}},
|
2016-05-03 11:46:40 +00:00
|
|
|
{separatedTexture.get(), composite_separation_filter_program.get(), {0.0, 0.5, 0.5}},
|
2016-05-03 01:05:58 +00:00
|
|
|
{filteredYTexture.get(), composite_y_filter_shader_program.get(), {0.0, 0.5, 0.5}},
|
|
|
|
{filteredTexture.get(), composite_chrominance_filter_shader_program.get(), {0.0, 0.0, 0.0}},
|
|
|
|
{nullptr}
|
|
|
|
};
|
|
|
|
|
|
|
|
RenderStage rgb_render_stages[] =
|
|
|
|
{
|
|
|
|
{compositeTexture.get(), rgb_input_shader_program.get(), {0.0, 0.0, 0.0}},
|
|
|
|
{filteredTexture.get(), rgb_filter_shader_program.get(), {0.0, 0.0, 0.0}},
|
|
|
|
{nullptr}
|
|
|
|
};
|
|
|
|
|
|
|
|
RenderStage *active_pipeline = (_output_device == Television || !rgb_input_shader_program) ? composite_render_stages : rgb_render_stages;
|
|
|
|
|
2016-04-17 22:08:05 +00:00
|
|
|
// for television, update intermediate buffers and then draw; for a monitor, just draw
|
2016-05-04 00:56:47 +00:00
|
|
|
if(number_of_source_drawing_zones)
|
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
|
|
|
|
glBindVertexArray(source_vertex_array);
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
|
|
|
while(active_pipeline->target)
|
|
|
|
{
|
|
|
|
// switch to the initial texture
|
|
|
|
active_pipeline->target->bind_framebuffer();
|
|
|
|
active_pipeline->shader->bind();
|
2016-04-22 01:32:36 +00:00
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
// clear as desired
|
2016-05-07 22:37:18 +00:00
|
|
|
if(number_of_clearing_zones)
|
|
|
|
{
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
glClearColor(active_pipeline->clear_colour[0], active_pipeline->clear_colour[1], active_pipeline->clear_colour[2], 1.0);
|
|
|
|
for(int c = 0; c < number_of_clearing_zones; c++)
|
|
|
|
{
|
|
|
|
glScissor(0, clearing_zones[c*2], IntermediateBufferWidth, clearing_zones[c*2 + 1]);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
2016-04-21 00:44:25 +00:00
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
// draw as desired
|
2016-05-04 00:56:47 +00:00
|
|
|
for(int c = 0; c < number_of_source_drawing_zones; c++)
|
2016-05-03 01:05:58 +00:00
|
|
|
{
|
2016-05-04 00:56:47 +00:00
|
|
|
glDrawArrays(GL_LINES, source_drawing_zones[c*2] / SourceVertexSize, source_drawing_zones[c*2 + 1] / SourceVertexSize);
|
2016-04-17 21:17:59 +00:00
|
|
|
}
|
2016-05-03 01:05:58 +00:00
|
|
|
|
|
|
|
active_pipeline++;
|
2016-04-17 21:17:59 +00:00
|
|
|
}
|
2016-05-03 01:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// transfer to framebuffer
|
|
|
|
framebuffer->bind_framebuffer();
|
|
|
|
|
2016-05-07 22:37:18 +00:00
|
|
|
|
2016-05-04 00:56:47 +00:00
|
|
|
if(number_of_output_drawing_zones)
|
2016-05-03 01:05:58 +00:00
|
|
|
{
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
|
|
|
// Ensure we're back on the output framebuffer, drawing from the output array buffer
|
|
|
|
glBindVertexArray(output_vertex_array);
|
|
|
|
|
|
|
|
// update uniforms (implicitly binding the shader)
|
|
|
|
if(_last_output_width != output_width || _last_output_height != output_height)
|
|
|
|
{
|
|
|
|
output_shader_program->set_output_size(output_width, output_height, _visible_area);
|
|
|
|
_last_output_width = output_width;
|
|
|
|
_last_output_height = output_height;
|
|
|
|
}
|
|
|
|
output_shader_program->bind();
|
|
|
|
|
|
|
|
// draw
|
2016-05-04 00:56:47 +00:00
|
|
|
for(int c = 0; c < number_of_output_drawing_zones; c++)
|
2016-05-03 01:05:58 +00:00
|
|
|
{
|
2016-05-04 00:56:47 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, output_drawing_zones[c*2] / OutputVertexSize, output_drawing_zones[c*2 + 1] / OutputVertexSize);
|
2016-05-03 01:05:58 +00:00
|
|
|
}
|
2016-04-17 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2016-05-07 22:37:18 +00:00
|
|
|
|
2016-05-06 01:21:27 +00:00
|
|
|
framebuffer->draw((float)output_width / (float)output_height);
|
2016-05-02 02:28:33 +00:00
|
|
|
|
2016-04-17 22:08:05 +00:00
|
|
|
// drawing commands having been issued, reclaim the array buffer pointer
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, output_array_buffer);
|
2016-05-02 02:33:00 +00:00
|
|
|
_output_buffer_data = (uint8_t *)glMapBufferRange(GL_ARRAY_BUFFER, 0, OutputVertexBufferDataSize, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_FLUSH_EXPLICIT_BIT);
|
2016-04-17 22:08:05 +00:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, source_array_buffer);
|
2016-05-02 02:33:00 +00:00
|
|
|
_source_buffer_data = (uint8_t *)glMapBufferRange(GL_ARRAY_BUFFER, 0, SourceVertexBufferDataSize, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_FLUSH_EXPLICIT_BIT);
|
2016-04-17 22:08:05 +00:00
|
|
|
|
|
|
|
_output_mutex->unlock();
|
|
|
|
}
|
|
|
|
|
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-03-09 03:40:23 +00:00
|
|
|
void OpenGLOutputBuilder::set_composite_sampling_function(const char *shader)
|
2016-02-05 03:28:50 +00:00
|
|
|
{
|
2016-02-05 03:57:46 +00:00
|
|
|
_composite_shader = strdup(shader);
|
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
|
|
|
{
|
2016-02-05 03:57:46 +00:00
|
|
|
_rgb_shader = strdup(shader);
|
|
|
|
}
|
|
|
|
|
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-04-29 01:45:44 +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-05-03 11:46:40 +00:00
|
|
|
composite_separation_filter_program = OpenGL::IntermediateShader::make_chroma_luma_separation_shader();
|
|
|
|
composite_separation_filter_program->set_source_texture_unit(composite_texture_unit);
|
|
|
|
composite_separation_filter_program->set_output_size(IntermediateBufferWidth, IntermediateBufferHeight);
|
|
|
|
|
|
|
|
composite_y_filter_shader_program = OpenGL::IntermediateShader::make_luma_filter_shader();
|
|
|
|
composite_y_filter_shader_program->set_source_texture_unit(separated_texture_unit);
|
2016-04-29 01:45:44 +00:00
|
|
|
composite_y_filter_shader_program->set_output_size(IntermediateBufferWidth, IntermediateBufferHeight);
|
2016-04-22 00:21:34 +00:00
|
|
|
|
2016-04-29 01:45:44 +00:00
|
|
|
composite_chrominance_filter_shader_program = OpenGL::IntermediateShader::make_chroma_filter_shader();
|
|
|
|
composite_chrominance_filter_shader_program->set_source_texture_unit(filtered_y_texture_unit);
|
|
|
|
composite_chrominance_filter_shader_program->set_output_size(IntermediateBufferWidth, IntermediateBufferHeight);
|
2016-03-08 02:04:04 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
void OpenGLOutputBuilder::prepare_rgb_input_shaders()
|
|
|
|
{
|
|
|
|
if(_rgb_shader)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
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-04-17 20:17:23 +00:00
|
|
|
void OpenGLOutputBuilder::prepare_source_vertex_array()
|
|
|
|
{
|
|
|
|
if(composite_input_shader_program)
|
|
|
|
{
|
2016-05-07 22:37:18 +00:00
|
|
|
GLint inputPositionAttribute = composite_input_shader_program->get_attrib_location("inputPosition");
|
|
|
|
GLint outputPositionAttribute = composite_input_shader_program->get_attrib_location("outputPosition");
|
|
|
|
GLint phaseAndAmplitudeAttribute = composite_input_shader_program->get_attrib_location("phaseAndAmplitude");
|
|
|
|
GLint phaseTimeAttribute = composite_input_shader_program->get_attrib_location("phaseTime");
|
2016-04-17 20:17:23 +00:00
|
|
|
|
2016-04-17 21:17:59 +00:00
|
|
|
glBindVertexArray(source_vertex_array);
|
|
|
|
|
2016-04-17 20:17:23 +00:00
|
|
|
glEnableVertexAttribArray((GLuint)inputPositionAttribute);
|
|
|
|
glEnableVertexAttribArray((GLuint)outputPositionAttribute);
|
2016-05-07 22:37:18 +00:00
|
|
|
glEnableVertexAttribArray((GLuint)phaseAndAmplitudeAttribute);
|
2016-04-17 20:17:23 +00:00
|
|
|
glEnableVertexAttribArray((GLuint)phaseTimeAttribute);
|
|
|
|
|
|
|
|
const GLsizei vertexStride = SourceVertexSize;
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, source_array_buffer);
|
2016-05-07 22:37:18 +00:00
|
|
|
glVertexAttribPointer((GLuint)inputPositionAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)SourceVertexOffsetOfInputPosition);
|
|
|
|
glVertexAttribPointer((GLuint)outputPositionAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)SourceVertexOffsetOfOutputPosition);
|
|
|
|
glVertexAttribPointer((GLuint)phaseAndAmplitudeAttribute, 2, GL_UNSIGNED_BYTE, GL_TRUE, vertexStride, (void *)SourceVertexOffsetOfPhaseAndAmplitude);
|
|
|
|
glVertexAttribPointer((GLuint)phaseTimeAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)SourceVertexOffsetOfPhaseTime);
|
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-05-03 01:05:58 +00:00
|
|
|
output_shader_program = OpenGL::OutputShader::make_shader("", "texture(texID, srcCoordinatesVarying).rgb", false);
|
|
|
|
output_shader_program->set_source_texture_unit(filtered_texture_unit);
|
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-05-03 01:05:58 +00:00
|
|
|
if(output_shader_program)
|
2016-03-08 02:42:21 +00:00
|
|
|
{
|
2016-05-03 01:05:58 +00:00
|
|
|
GLint positionAttribute = output_shader_program->get_attrib_location("position");
|
|
|
|
GLint textureCoordinatesAttribute = output_shader_program->get_attrib_location("srcCoordinates");
|
2016-03-08 02:42:21 +00:00
|
|
|
|
2016-04-17 21:17:59 +00:00
|
|
|
glBindVertexArray(output_vertex_array);
|
|
|
|
|
2016-03-08 02:42:21 +00:00
|
|
|
glEnableVertexAttribArray((GLuint)positionAttribute);
|
|
|
|
glEnableVertexAttribArray((GLuint)textureCoordinatesAttribute);
|
|
|
|
|
2016-03-09 01:49:07 +00:00
|
|
|
const GLsizei vertexStride = OutputVertexSize;
|
2016-04-14 02:14:18 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, output_array_buffer);
|
2016-03-09 01:49:07 +00:00
|
|
|
glVertexAttribPointer((GLuint)positionAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)OutputVertexOffsetOfPosition);
|
|
|
|
glVertexAttribPointer((GLuint)textureCoordinatesAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)OutputVertexOffsetOfTexCoord);
|
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-04-18 23:01:15 +00:00
|
|
|
if(_output_device != output_device)
|
2016-03-05 19:36:12 +00:00
|
|
|
{
|
|
|
|
_output_device = output_device;
|
|
|
|
_composite_src_output_y = 0;
|
2016-04-29 02:04:47 +00:00
|
|
|
_last_output_width = 0;
|
|
|
|
_last_output_height = 0;
|
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-05-03 22:45:55 +00:00
|
|
|
_output_mutex->lock();
|
2016-05-03 01:05:58 +00:00
|
|
|
_input_frequency = input_frequency;
|
2016-04-24 22:58:31 +00:00
|
|
|
_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-05-03 22:45:55 +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;
|
|
|
|
|
|
|
|
switch(_colour_space)
|
|
|
|
{
|
|
|
|
case ColourSpace::YIQ:
|
|
|
|
fromRGB = rgbToYIQ;
|
|
|
|
toRGB = yiqToRGB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ColourSpace::YUV:
|
|
|
|
fromRGB = rgbToYUV;
|
|
|
|
toRGB = yuvToRGB;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-05-03 11:46:40 +00:00
|
|
|
if(composite_input_shader_program) composite_input_shader_program->set_colour_conversion_matrices(fromRGB, toRGB);
|
2016-04-29 01:45:44 +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
|
|
|
}
|
|
|
|
|
2016-04-24 22:58:31 +00:00
|
|
|
void OpenGLOutputBuilder::set_timing_uniforms()
|
|
|
|
{
|
2016-04-29 01:45:44 +00:00
|
|
|
OpenGL::IntermediateShader *intermediate_shaders[] = {
|
2016-04-24 22:58:31 +00:00
|
|
|
composite_input_shader_program.get(),
|
2016-05-03 11:46:40 +00:00
|
|
|
composite_separation_filter_program.get(),
|
2016-04-24 22:58:31 +00:00
|
|
|
composite_y_filter_shader_program.get(),
|
|
|
|
composite_chrominance_filter_shader_program.get()
|
|
|
|
};
|
|
|
|
bool extends = false;
|
2016-04-29 01:45:44 +00:00
|
|
|
float phaseCyclesPerTick = (float)_colour_cycle_numerator / (float)(_colour_cycle_denominator * _cycles_per_line);
|
2016-04-24 22:58:31 +00:00
|
|
|
for(int c = 0; c < 3; c++)
|
|
|
|
{
|
2016-04-29 01:45:44 +00:00
|
|
|
if(intermediate_shaders[c]) intermediate_shaders[c]->set_phase_cycles_per_sample(phaseCyclesPerTick, extends);
|
2016-04-24 22:58:31 +00:00
|
|
|
extends = true;
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:05:58 +00:00
|
|
|
if(output_shader_program) output_shader_program->set_timing(_height_of_display, _cycles_per_line, _horizontal_scan_period, _vertical_scan_period, _vertical_period_divider);
|
2016-04-24 22:58:31 +00:00
|
|
|
|
|
|
|
float colour_subcarrier_frequency = (float)_colour_cycle_numerator / (float)_colour_cycle_denominator;
|
2016-05-03 11:46:40 +00:00
|
|
|
if(composite_separation_filter_program) composite_separation_filter_program->set_separation_frequency(_cycles_per_line, colour_subcarrier_frequency);
|
2016-05-03 11:51:14 +00:00
|
|
|
if(composite_y_filter_shader_program) composite_y_filter_shader_program->set_filter_coefficients(_cycles_per_line, colour_subcarrier_frequency * 0.66f);
|
2016-05-03 01:05:58 +00:00
|
|
|
if(composite_chrominance_filter_shader_program) composite_chrominance_filter_shader_program->set_filter_coefficients(_cycles_per_line, colour_subcarrier_frequency * 0.5f);
|
|
|
|
if(rgb_filter_shader_program) rgb_filter_shader_program->set_filter_coefficients(_cycles_per_line, (float)_input_frequency * 0.5f);
|
2016-04-24 22:58:31 +00:00
|
|
|
}
|