2016-04-29 01:04:59 +00:00
|
|
|
//
|
|
|
|
// IntermediateShader.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 28/04/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-04-29 01:04:59 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "IntermediateShader.hpp"
|
|
|
|
|
2017-11-25 00:27:49 +00:00
|
|
|
#include <cassert>
|
2017-11-11 02:56:53 +00:00
|
|
|
#include <cstring>
|
2017-11-16 02:25:01 +00:00
|
|
|
#include <sstream>
|
2017-11-11 02:56:53 +00:00
|
|
|
|
2018-11-07 03:23:38 +00:00
|
|
|
#include "../../../SignalProcessing/FIRFilter.hpp"
|
2016-04-29 01:04:59 +00:00
|
|
|
|
|
|
|
using namespace OpenGL;
|
|
|
|
|
2017-11-24 23:45:24 +00:00
|
|
|
std::string IntermediateShader::get_input_name(Input input) {
|
|
|
|
switch(input) {
|
|
|
|
case Input::InputStart: return "inputStart";
|
|
|
|
case Input::OutputStart: return "outputStart";
|
|
|
|
case Input::Ends: return "ends";
|
|
|
|
case Input::PhaseTimeAndAmplitude: return "phaseTimeAndAmplitude";
|
2017-11-25 00:27:49 +00:00
|
|
|
|
|
|
|
// Intended to be unreachable.
|
2018-05-12 22:02:16 +00:00
|
|
|
default: assert(false); return "";
|
2017-11-24 23:45:24 +00:00
|
|
|
}
|
2016-04-29 01:04:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::unique_ptr<IntermediateShader> IntermediateShader::make_shader(const std::string &fragment_shader, bool use_usampler, bool input_is_inputPosition) {
|
2017-11-16 02:25:01 +00:00
|
|
|
std::ostringstream vertex_shader;
|
|
|
|
vertex_shader <<
|
2016-04-29 01:04:59 +00:00
|
|
|
"#version 150\n"
|
|
|
|
|
2017-11-24 23:45:24 +00:00
|
|
|
"in vec2 " << get_input_name(Input::InputStart) << ";"
|
|
|
|
"in vec2 " << get_input_name(Input::OutputStart) << ";"
|
|
|
|
"in vec2 " << get_input_name(Input::Ends) << ";"
|
|
|
|
"in vec3 " << get_input_name(Input::PhaseTimeAndAmplitude) << ";"
|
2016-04-29 01:04:59 +00:00
|
|
|
|
|
|
|
"uniform ivec2 outputTextureSize;"
|
|
|
|
"uniform float extension;"
|
2017-11-16 02:25:01 +00:00
|
|
|
"uniform " << (use_usampler ? "usampler2D" : "sampler2D") << " texID;"
|
2016-04-29 22:37:35 +00:00
|
|
|
"uniform float offsets[5];"
|
2017-01-04 03:16:52 +00:00
|
|
|
"uniform vec2 widthScalers;"
|
2017-01-08 16:13:20 +00:00
|
|
|
"uniform float inputVerticalOffset;"
|
|
|
|
"uniform float outputVerticalOffset;"
|
2017-01-08 19:47:16 +00:00
|
|
|
"uniform float textureHeightDivisor;"
|
2016-04-29 01:04:59 +00:00
|
|
|
|
2017-07-07 01:25:38 +00:00
|
|
|
"out vec3 phaseAndAmplitudeVarying;"
|
2016-04-29 01:04:59 +00:00
|
|
|
"out vec2 inputPositionsVarying[11];"
|
|
|
|
"out vec2 delayLinePositionVarying;"
|
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2017-01-04 02:16:38 +00:00
|
|
|
// odd vertices are on the left, even on the right
|
2016-05-10 23:50:12 +00:00
|
|
|
"float extent = float(gl_VertexID & 1);"
|
2017-01-12 02:18:41 +00:00
|
|
|
"float longitudinal = float((gl_VertexID & 2) >> 1);"
|
2016-05-10 23:50:12 +00:00
|
|
|
|
2017-01-04 02:16:38 +00:00
|
|
|
// inputPosition.x is either inputStart.x or ends.x, depending on whether it is on the left or the right;
|
|
|
|
// outputPosition.x is either outputStart.x or ends.y;
|
|
|
|
// .ys are inputStart.y and outputStart.y respectively
|
2017-01-08 16:13:20 +00:00
|
|
|
"vec2 inputPosition = vec2(mix(inputStart.x, ends.x, extent)*widthScalers[0], inputStart.y + inputVerticalOffset);"
|
|
|
|
"vec2 outputPosition = vec2(mix(outputStart.x, ends.y, extent)*widthScalers[1], outputStart.y + outputVerticalOffset);"
|
2017-01-04 02:16:38 +00:00
|
|
|
|
2017-01-12 02:18:41 +00:00
|
|
|
"inputPosition.y += longitudinal;"
|
|
|
|
"outputPosition.y += longitudinal;"
|
|
|
|
|
2017-01-04 02:16:38 +00:00
|
|
|
// extension is the amount to extend both the input and output by to add a full colour cycle at each end
|
2016-05-10 23:50:12 +00:00
|
|
|
"vec2 extensionVector = vec2(extension, 0.0) * 2.0 * (extent - 0.5);"
|
|
|
|
|
2017-01-04 02:16:38 +00:00
|
|
|
// extended[Input/Output]Position are [input/output]Position with the necessary applied extension
|
2017-11-16 02:25:01 +00:00
|
|
|
"vec2 extendedInputPosition = " << (input_is_inputPosition ? "inputPosition" : "outputPosition") << " + extensionVector;"
|
2016-04-29 01:04:59 +00:00
|
|
|
"vec2 extendedOutputPosition = outputPosition + extensionVector;"
|
|
|
|
|
2018-09-13 00:05:39 +00:00
|
|
|
// scale mappedInputPosition to the ordinary normalised range
|
2016-04-29 01:04:59 +00:00
|
|
|
"vec2 textureSize = vec2(textureSize(texID, 0));"
|
2018-04-18 23:29:03 +00:00
|
|
|
"vec2 mappedInputPosition = extendedInputPosition / textureSize;"
|
2016-04-29 01:04:59 +00:00
|
|
|
|
2017-01-04 02:16:38 +00:00
|
|
|
// setup input positions spaced as per the supplied offsets; these are for filtering where required
|
2017-01-08 16:13:20 +00:00
|
|
|
"inputPositionsVarying[0] = mappedInputPosition - (vec2(5.0, 0.0) / textureSize);"
|
|
|
|
"inputPositionsVarying[1] = mappedInputPosition - (vec2(4.0, 0.0) / textureSize);"
|
|
|
|
"inputPositionsVarying[2] = mappedInputPosition - (vec2(3.0, 0.0) / textureSize);"
|
|
|
|
"inputPositionsVarying[3] = mappedInputPosition - (vec2(2.0, 0.0) / textureSize);"
|
|
|
|
"inputPositionsVarying[4] = mappedInputPosition - (vec2(1.0, 0.0) / textureSize);"
|
2016-04-29 01:04:59 +00:00
|
|
|
"inputPositionsVarying[5] = mappedInputPosition;"
|
2017-01-08 16:13:20 +00:00
|
|
|
"inputPositionsVarying[6] = mappedInputPosition + (vec2(1.0, 0.0) / textureSize);"
|
|
|
|
"inputPositionsVarying[7] = mappedInputPosition + (vec2(2.0, 0.0) / textureSize);"
|
|
|
|
"inputPositionsVarying[8] = mappedInputPosition + (vec2(3.0, 0.0) / textureSize);"
|
|
|
|
"inputPositionsVarying[9] = mappedInputPosition + (vec2(4.0, 0.0) / textureSize);"
|
|
|
|
"inputPositionsVarying[10] = mappedInputPosition + (vec2(5.0, 0.0) / textureSize);"
|
2016-04-29 01:04:59 +00:00
|
|
|
"delayLinePositionVarying = mappedInputPosition - vec2(0.0, 1.0);"
|
|
|
|
|
2017-01-04 02:16:38 +00:00
|
|
|
// setup phaseAndAmplitudeVarying.x as colour burst subcarrier phase, in radians;
|
2017-07-07 01:25:38 +00:00
|
|
|
// setup phaseAndAmplitudeVarying.y as colour burst amplitude;
|
2018-06-01 01:40:46 +00:00
|
|
|
// setup phaseAndAmplitudeVarying.z as 1 / abs(colour burst amplitude), or 0.0 if amplitude is 0.0;
|
2017-01-11 13:18:00 +00:00
|
|
|
"phaseAndAmplitudeVarying.x = (extendedOutputPosition.x + (phaseTimeAndAmplitude.x / 64.0)) * 0.5 * 3.141592654;"
|
2018-06-01 01:40:46 +00:00
|
|
|
"phaseAndAmplitudeVarying.y = (phaseTimeAndAmplitude.y - 128) / 127.0;"
|
|
|
|
"phaseAndAmplitudeVarying.z = (abs(phaseAndAmplitudeVarying.y) > 0.05) ? 1.0 / abs(phaseAndAmplitudeVarying.y) : 0.0;"
|
2016-04-29 01:04:59 +00:00
|
|
|
|
2017-01-04 02:16:38 +00:00
|
|
|
// determine output position by scaling the output position according to the texture size
|
2017-05-14 01:50:48 +00:00
|
|
|
"vec2 eyePosition = 2.0*(extendedOutputPosition / outputTextureSize) - vec2(1.0);"
|
2016-04-29 01:04:59 +00:00
|
|
|
"gl_Position = vec4(eyePosition, 0.0, 1.0);"
|
2017-11-16 02:25:01 +00:00
|
|
|
"}";
|
2017-11-12 22:29:20 +00:00
|
|
|
|
2017-11-24 23:45:24 +00:00
|
|
|
return std::unique_ptr<IntermediateShader>(new IntermediateShader(vertex_shader.str(), fragment_shader, {
|
|
|
|
{get_input_name(Input::InputStart), 0},
|
|
|
|
{get_input_name(Input::OutputStart), 1},
|
|
|
|
{get_input_name(Input::Ends), 2},
|
|
|
|
{get_input_name(Input::PhaseTimeAndAmplitude), 3}
|
|
|
|
}));
|
2016-04-29 01:04:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:41:20 +00:00
|
|
|
std::unique_ptr<IntermediateShader> IntermediateShader::make_composite_source_shader(const std::string &composite_shader, const std::string &svideo_shader, const std::string &rgb_shader) {
|
2017-11-16 02:25:01 +00:00
|
|
|
std::ostringstream fragment_shader;
|
|
|
|
fragment_shader <<
|
2016-04-29 01:04:59 +00:00
|
|
|
"#version 150\n"
|
|
|
|
|
|
|
|
"in vec2 inputPositionsVarying[11];"
|
2017-07-07 01:25:38 +00:00
|
|
|
"in vec3 phaseAndAmplitudeVarying;"
|
2016-04-29 01:04:59 +00:00
|
|
|
|
|
|
|
"out vec4 fragColour;"
|
|
|
|
|
2018-03-30 22:24:18 +00:00
|
|
|
"uniform usampler2D texID;"
|
|
|
|
<< composite_shader;
|
2016-04-29 01:04:59 +00:00
|
|
|
|
2018-03-30 22:24:18 +00:00
|
|
|
if(composite_shader.empty()) {
|
2018-03-30 16:41:20 +00:00
|
|
|
if(!svideo_shader.empty()) {
|
2018-03-30 22:24:18 +00:00
|
|
|
fragment_shader <<
|
2018-03-30 16:41:20 +00:00
|
|
|
svideo_shader <<
|
2018-09-13 00:05:39 +00:00
|
|
|
"float composite_sample(usampler2D texID, vec2 coordinate, float phase, float amplitude)"
|
2018-03-30 16:41:20 +00:00
|
|
|
"{"
|
2018-09-13 00:05:39 +00:00
|
|
|
"vec2 svideoColour = svideo_sample(texID, coordinate, phase, amplitude);"
|
2018-06-01 01:40:46 +00:00
|
|
|
"return mix(svideoColour.x, svideoColour.y, abs(amplitude));"
|
2018-03-30 16:41:20 +00:00
|
|
|
"}";
|
|
|
|
} else {
|
2018-03-30 22:24:18 +00:00
|
|
|
fragment_shader <<
|
2018-03-30 16:41:20 +00:00
|
|
|
rgb_shader <<
|
|
|
|
"uniform mat3 rgbToLumaChroma;"
|
2018-09-13 00:05:39 +00:00
|
|
|
"float composite_sample(usampler2D texID, vec2 coordinate, float phase, float amplitude)"
|
2018-03-30 16:41:20 +00:00
|
|
|
"{"
|
2018-09-13 00:05:39 +00:00
|
|
|
"vec3 rgbColour = clamp(rgb_sample(texID, coordinate), vec3(0.0), vec3(1.0));"
|
2018-03-30 16:41:20 +00:00
|
|
|
"vec3 lumaChromaColour = rgbToLumaChroma * rgbColour;"
|
2018-06-01 01:40:46 +00:00
|
|
|
"vec2 quadrature = vec2(cos(phase), sin(phase)) * vec2(abs(amplitude), amplitude);"
|
2018-06-14 22:25:48 +00:00
|
|
|
"return dot(lumaChromaColour, vec3(1.0 - abs(amplitude), quadrature));"
|
2018-03-30 16:41:20 +00:00
|
|
|
"}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment_shader <<
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2018-09-13 00:05:39 +00:00
|
|
|
"fragColour = vec4(composite_sample(texID, inputPositionsVarying[5], phaseAndAmplitudeVarying.x, phaseAndAmplitudeVarying.y));"
|
2018-03-30 16:41:20 +00:00
|
|
|
"}";
|
|
|
|
|
|
|
|
return make_shader(fragment_shader.str(), true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IntermediateShader> IntermediateShader::make_svideo_source_shader(const std::string &svideo_shader, const std::string &rgb_shader) {
|
|
|
|
std::ostringstream fragment_shader;
|
|
|
|
fragment_shader <<
|
|
|
|
"#version 150\n"
|
|
|
|
|
|
|
|
"in vec2 inputPositionsVarying[11];"
|
|
|
|
"in vec3 phaseAndAmplitudeVarying;"
|
|
|
|
|
|
|
|
"out vec3 fragColour;"
|
|
|
|
|
|
|
|
"uniform usampler2D texID;"
|
|
|
|
<< svideo_shader;
|
|
|
|
|
|
|
|
if(svideo_shader.empty()) {
|
|
|
|
fragment_shader
|
|
|
|
<< rgb_shader <<
|
2017-11-16 02:25:01 +00:00
|
|
|
"uniform mat3 rgbToLumaChroma;"
|
2018-09-13 00:05:39 +00:00
|
|
|
"vec2 svideo_sample(usampler2D texID, vec2 coordinate, float phase, float amplitude)"
|
2017-11-16 02:25:01 +00:00
|
|
|
"{"
|
2018-09-13 00:05:39 +00:00
|
|
|
"vec3 rgbColour = clamp(rgb_sample(texID, coordinate), vec3(0.0), vec3(1.0));"
|
2017-11-16 02:25:01 +00:00
|
|
|
"vec3 lumaChromaColour = rgbToLumaChroma * rgbColour;"
|
2018-06-01 01:40:46 +00:00
|
|
|
"vec2 quadrature = vec2(cos(phase), sin(phase)) * vec2(1.0, sign(amplitude));"
|
2018-03-30 22:24:18 +00:00
|
|
|
"return vec2(lumaChromaColour.x, 0.5 + dot(quadrature, lumaChromaColour.yz) * 0.5);"
|
2017-11-16 02:25:01 +00:00
|
|
|
"}";
|
|
|
|
}
|
2016-04-29 01:04:59 +00:00
|
|
|
|
2017-11-16 02:25:01 +00:00
|
|
|
fragment_shader <<
|
2016-04-29 01:04:59 +00:00
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2018-09-13 00:05:39 +00:00
|
|
|
"vec2 sample = svideo_sample(texID, inputPositionsVarying[5], phaseAndAmplitudeVarying.x, phaseAndAmplitudeVarying.y);"
|
2018-06-01 01:40:46 +00:00
|
|
|
"vec2 quadrature = vec2(cos(phaseAndAmplitudeVarying.x), sin(phaseAndAmplitudeVarying.x)) * vec2(1.0, sign(phaseAndAmplitudeVarying.y)) * 0.5 * phaseAndAmplitudeVarying.z;"
|
2018-03-30 17:16:18 +00:00
|
|
|
"fragColour = vec3(sample.x, vec2(0.5) + (sample.y * quadrature));"
|
2017-11-16 02:25:01 +00:00
|
|
|
"}";
|
2017-11-12 22:29:20 +00:00
|
|
|
|
2017-11-16 02:48:10 +00:00
|
|
|
return make_shader(fragment_shader.str(), true, true);
|
2016-04-29 01:04:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::unique_ptr<IntermediateShader> IntermediateShader::make_rgb_source_shader(const std::string &rgb_shader) {
|
2017-11-16 02:25:01 +00:00
|
|
|
std::ostringstream fragment_shader;
|
|
|
|
fragment_shader <<
|
2016-05-03 01:05:58 +00:00
|
|
|
"#version 150\n"
|
|
|
|
|
|
|
|
"in vec2 inputPositionsVarying[11];"
|
2017-07-07 01:25:38 +00:00
|
|
|
"in vec3 phaseAndAmplitudeVarying;"
|
2016-05-03 01:05:58 +00:00
|
|
|
|
|
|
|
"out vec3 fragColour;"
|
|
|
|
|
|
|
|
"uniform usampler2D texID;"
|
|
|
|
|
2017-11-16 02:25:01 +00:00
|
|
|
<< rgb_shader <<
|
2016-05-03 01:05:58 +00:00
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2018-09-13 00:05:39 +00:00
|
|
|
"fragColour = rgb_sample(texID, inputPositionsVarying[5]);"
|
2017-11-16 02:25:01 +00:00
|
|
|
"}";
|
2017-11-12 22:29:20 +00:00
|
|
|
|
2017-11-16 02:48:10 +00:00
|
|
|
return make_shader(fragment_shader.str(), true, true);
|
2016-05-03 01:05:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::unique_ptr<IntermediateShader> IntermediateShader::make_chroma_luma_separation_shader() {
|
2016-04-29 01:04:59 +00:00
|
|
|
return make_shader(
|
|
|
|
"#version 150\n"
|
|
|
|
|
2017-07-07 01:25:38 +00:00
|
|
|
"in vec3 phaseAndAmplitudeVarying;"
|
2016-04-29 01:04:59 +00:00
|
|
|
"in vec2 inputPositionsVarying[11];"
|
|
|
|
|
|
|
|
"out vec3 fragColour;"
|
|
|
|
|
|
|
|
"uniform sampler2D texID;"
|
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2017-01-11 03:08:07 +00:00
|
|
|
"vec4 samples = vec4("
|
|
|
|
"texture(texID, inputPositionsVarying[3]).r,"
|
|
|
|
"texture(texID, inputPositionsVarying[4]).r,"
|
|
|
|
"texture(texID, inputPositionsVarying[5]).r,"
|
|
|
|
"texture(texID, inputPositionsVarying[6]).r"
|
2017-01-07 21:02:33 +00:00
|
|
|
");"
|
2018-09-09 21:17:38 +00:00
|
|
|
// calculate luminance as either the straight average of the samples, if a colour subcarrier
|
|
|
|
// was present, or else a weighted sample around the third sample if not.
|
2017-07-07 01:38:33 +00:00
|
|
|
"float luminance = mix(dot(samples, vec4(0.25)), dot(samples, vec4(0.0, 0.16, 0.66, 0.16)), step(phaseAndAmplitudeVarying.z, 0.0));"
|
2017-01-07 21:02:33 +00:00
|
|
|
|
2017-01-04 13:06:18 +00:00
|
|
|
// define chroma to be whatever was here, minus luma
|
2017-07-07 01:25:38 +00:00
|
|
|
"float chrominance = 0.5 * (samples.z - luminance) * phaseAndAmplitudeVarying.z;"
|
2018-09-09 21:17:38 +00:00
|
|
|
|
|
|
|
// scale luminance up to the range [0, 1)
|
2018-06-01 01:40:46 +00:00
|
|
|
"luminance /= (1.0 - abs(phaseAndAmplitudeVarying.y));"
|
2016-04-29 01:04:59 +00:00
|
|
|
|
2017-01-04 13:06:18 +00:00
|
|
|
// split choma colours here, as the most direct place, writing out
|
|
|
|
// RGB = (luma, chroma.x, chroma.y)
|
2018-06-01 01:40:46 +00:00
|
|
|
"vec2 quadrature = vec2(cos(phaseAndAmplitudeVarying.x), sin(phaseAndAmplitudeVarying.x)) * vec2(1.0, sign(phaseAndAmplitudeVarying.y));"
|
2016-04-29 01:04:59 +00:00
|
|
|
"fragColour = vec3(luminance, vec2(0.5) + (chrominance * quadrature));"
|
|
|
|
"}",false, false);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::unique_ptr<IntermediateShader> IntermediateShader::make_chroma_filter_shader() {
|
2016-04-29 01:04:59 +00:00
|
|
|
return make_shader(
|
|
|
|
"#version 150\n"
|
|
|
|
|
|
|
|
"in vec2 inputPositionsVarying[11];"
|
|
|
|
"uniform vec4 weights[3];"
|
|
|
|
|
|
|
|
"out vec3 fragColour;"
|
|
|
|
|
|
|
|
"uniform sampler2D texID;"
|
|
|
|
"uniform mat3 lumaChromaToRGB;"
|
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2016-05-03 11:46:40 +00:00
|
|
|
"vec3 samples[] = vec3[]("
|
2017-01-11 03:08:07 +00:00
|
|
|
"texture(texID, inputPositionsVarying[3]).rgb,"
|
2016-05-03 11:46:40 +00:00
|
|
|
"texture(texID, inputPositionsVarying[4]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[5]).rgb,"
|
2017-01-11 03:08:07 +00:00
|
|
|
"texture(texID, inputPositionsVarying[6]).rgb"
|
2017-01-07 21:02:33 +00:00
|
|
|
");"
|
|
|
|
|
2017-01-11 03:08:07 +00:00
|
|
|
"vec4 chromaChannel1 = vec4(samples[0].g, samples[1].g, samples[2].g, samples[3].g);"
|
|
|
|
"vec4 chromaChannel2 = vec4(samples[0].b, samples[1].b, samples[2].b, samples[3].b);"
|
2016-04-29 01:04:59 +00:00
|
|
|
|
2017-01-11 03:08:07 +00:00
|
|
|
"vec3 lumaChromaColour = vec3(samples[2].r,"
|
|
|
|
"dot(chromaChannel1, vec4(0.25)),"
|
|
|
|
"dot(chromaChannel2, vec4(0.25))"
|
2016-04-29 01:04:59 +00:00
|
|
|
");"
|
|
|
|
|
2016-05-16 12:01:29 +00:00
|
|
|
"vec3 lumaChromaColourInRange = (lumaChromaColour - vec3(0.0, 0.5, 0.5)) * vec3(1.0, 2.0, 2.0);"
|
2016-04-29 01:04:59 +00:00
|
|
|
"fragColour = lumaChromaToRGB * lumaChromaColourInRange;"
|
|
|
|
"}", false, false);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::unique_ptr<IntermediateShader> IntermediateShader::make_rgb_filter_shader() {
|
2016-05-03 01:05:58 +00:00
|
|
|
return make_shader(
|
|
|
|
"#version 150\n"
|
|
|
|
|
|
|
|
"in vec2 inputPositionsVarying[11];"
|
|
|
|
"uniform vec4 weights[3];"
|
|
|
|
|
|
|
|
"out vec3 fragColour;"
|
|
|
|
|
|
|
|
"uniform sampler2D texID;"
|
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
|
|
|
"vec3 samples[] = vec3[]("
|
|
|
|
"texture(texID, inputPositionsVarying[0]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[1]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[2]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[3]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[4]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[5]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[6]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[7]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[8]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[9]).rgb,"
|
|
|
|
"texture(texID, inputPositionsVarying[10]).rgb"
|
|
|
|
");"
|
|
|
|
|
|
|
|
"vec4 channel1[] = vec4[]("
|
|
|
|
"vec4(samples[0].r, samples[1].r, samples[2].r, samples[3].r),"
|
|
|
|
"vec4(samples[4].r, samples[5].r, samples[6].r, samples[7].r),"
|
|
|
|
"vec4(samples[8].r, samples[9].r, samples[10].r, 0.0)"
|
|
|
|
");"
|
|
|
|
"vec4 channel2[] = vec4[]("
|
|
|
|
"vec4(samples[0].g, samples[1].g, samples[2].g, samples[3].g),"
|
|
|
|
"vec4(samples[4].g, samples[5].g, samples[6].g, samples[7].g),"
|
|
|
|
"vec4(samples[8].g, samples[9].g, samples[10].g, 0.0)"
|
|
|
|
");"
|
|
|
|
"vec4 channel3[] = vec4[]("
|
|
|
|
"vec4(samples[0].b, samples[1].b, samples[2].b, samples[3].b),"
|
|
|
|
"vec4(samples[4].b, samples[5].b, samples[6].b, samples[7].b),"
|
|
|
|
"vec4(samples[8].b, samples[9].b, samples[10].b, 0.0)"
|
|
|
|
");"
|
|
|
|
|
|
|
|
"fragColour = vec3("
|
|
|
|
"dot(vec3("
|
|
|
|
"dot(channel1[0], weights[0]),"
|
|
|
|
"dot(channel1[1], weights[1]),"
|
|
|
|
"dot(channel1[2], weights[2])"
|
|
|
|
"), vec3(1.0)),"
|
|
|
|
"dot(vec3("
|
|
|
|
"dot(channel2[0], weights[0]),"
|
|
|
|
"dot(channel2[1], weights[1]),"
|
|
|
|
"dot(channel2[2], weights[2])"
|
|
|
|
"), vec3(1.0)),"
|
|
|
|
"dot(vec3("
|
|
|
|
"dot(channel3[0], weights[0]),"
|
|
|
|
"dot(channel3[1], weights[1]),"
|
|
|
|
"dot(channel3[2], weights[2])"
|
|
|
|
"), vec3(1.0))"
|
|
|
|
");"
|
|
|
|
"}", false, false);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void IntermediateShader::set_output_size(unsigned int output_width, unsigned int output_height) {
|
2016-05-14 02:08:32 +00:00
|
|
|
set_uniform("outputTextureSize", (GLint)output_width, (GLint)output_height);
|
2016-04-29 01:04:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void IntermediateShader::set_source_texture_unit(GLenum unit) {
|
2016-05-14 02:08:32 +00:00
|
|
|
set_uniform("texID", (GLint)(unit - GL_TEXTURE0));
|
2016-04-29 01:04:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void IntermediateShader::set_filter_coefficients(float sampling_rate, float cutoff_frequency) {
|
2016-04-29 22:37:35 +00:00
|
|
|
// The process below: the source texture will have bilinear filtering enabled; so by
|
|
|
|
// sampling at non-integral offsets from the centre the shader can get a weighted sum
|
|
|
|
// of two source pixels, then scale that once, to do two taps per sample. However
|
|
|
|
// that works only if the two coefficients being joined have the same sign. So the
|
|
|
|
// number of usable taps is between 11 and 21 depending on the values that come out.
|
|
|
|
// Perform a linear search for the highest number of taps we can use with 11 samples.
|
2016-05-14 02:08:32 +00:00
|
|
|
GLfloat weights[12];
|
|
|
|
GLfloat offsets[5];
|
2017-01-04 13:06:18 +00:00
|
|
|
unsigned int taps = 11;
|
|
|
|
// unsigned int taps = 21;
|
2017-07-22 00:58:55 +00:00
|
|
|
// while(1) {
|
2017-11-11 17:20:11 +00:00
|
|
|
// float coefficients[21];
|
2016-04-29 22:37:35 +00:00
|
|
|
SignalProcessing::FIRFilter luminance_filter(taps, sampling_rate, 0.0f, cutoff_frequency, SignalProcessing::FIRFilter::DefaultAttenuation);
|
2017-11-11 17:20:11 +00:00
|
|
|
std::vector<float> coefficients = luminance_filter.get_coefficients();
|
2016-04-29 22:37:35 +00:00
|
|
|
|
2017-01-04 13:06:18 +00:00
|
|
|
// int sample = 0;
|
|
|
|
// int c = 0;
|
2017-11-12 22:17:27 +00:00
|
|
|
std::memset(weights, 0, sizeof(float)*12);
|
|
|
|
std::memset(offsets, 0, sizeof(float)*5);
|
2016-05-01 00:48:09 +00:00
|
|
|
|
2017-07-22 00:58:55 +00:00
|
|
|
unsigned int half_size = (taps >> 1);
|
|
|
|
for(unsigned int c = 0; c < taps; c++) {
|
|
|
|
if(c < 5) offsets[c] = (half_size - c);
|
2017-01-04 13:06:18 +00:00
|
|
|
weights[c] = coefficients[c];
|
2016-04-29 22:37:35 +00:00
|
|
|
}
|
2017-07-22 00:58:55 +00:00
|
|
|
// break;
|
2017-01-04 13:06:18 +00:00
|
|
|
|
|
|
|
// int halfSize = (taps >> 1);
|
2017-03-26 18:34:47 +00:00
|
|
|
// while(c < halfSize && sample < 5) {
|
2017-10-21 23:49:04 +00:00
|
|
|
// offsets[sample] = static_cast<float>(halfSize - c);
|
2017-03-26 18:34:47 +00:00
|
|
|
// if((coefficients[c] < 0.0f) == (coefficients[c+1] < 0.0f) && c+1 < (taps >> 1)) {
|
2017-01-04 13:06:18 +00:00
|
|
|
// weights[sample] = coefficients[c] + coefficients[c+1];
|
|
|
|
// offsets[sample] -= (coefficients[c+1] / weights[sample]);
|
|
|
|
// c += 2;
|
2017-03-26 18:34:47 +00:00
|
|
|
// } else {
|
2017-01-04 13:06:18 +00:00
|
|
|
// weights[sample] = coefficients[c];
|
|
|
|
// c++;
|
|
|
|
// }
|
|
|
|
// sample ++;
|
|
|
|
// }
|
2017-03-26 18:34:47 +00:00
|
|
|
// if(c == halfSize) { // i.e. we finished combining inputs before we ran out of space
|
2017-01-04 13:06:18 +00:00
|
|
|
// weights[sample] = coefficients[c];
|
2017-03-26 18:34:47 +00:00
|
|
|
// for(int c = 0; c < sample; c++) {
|
2017-01-04 13:06:18 +00:00
|
|
|
// weights[sample+c+1] = weights[sample-c-1];
|
|
|
|
// }
|
|
|
|
// break;
|
|
|
|
// }
|
2017-07-22 00:58:55 +00:00
|
|
|
// taps -= 2;
|
|
|
|
// }
|
2016-04-29 22:37:35 +00:00
|
|
|
|
2016-05-14 02:08:32 +00:00
|
|
|
set_uniform("weights", 4, 3, weights);
|
|
|
|
set_uniform("offsets", 1, 5, offsets);
|
2016-04-29 01:04:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void IntermediateShader::set_separation_frequency(float sampling_rate, float colour_burst_frequency) {
|
2016-05-03 11:46:40 +00:00
|
|
|
set_filter_coefficients(sampling_rate, colour_burst_frequency);
|
2016-05-03 01:05:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void IntermediateShader::set_extension(float extension) {
|
2017-01-08 21:24:22 +00:00
|
|
|
set_uniform("extension", extension);
|
2016-04-29 01:04:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void IntermediateShader::set_colour_conversion_matrices(float *fromRGB, float *toRGB) {
|
2016-05-14 02:08:32 +00:00
|
|
|
set_uniform_matrix("lumaChromaToRGB", 3, false, toRGB);
|
|
|
|
set_uniform_matrix("rgbToLumaChroma", 3, false, fromRGB);
|
2016-04-29 01:04:59 +00:00
|
|
|
}
|
2017-01-04 03:16:52 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void IntermediateShader::set_width_scalers(float input_scaler, float output_scaler) {
|
2017-01-04 03:16:52 +00:00
|
|
|
set_uniform("widthScalers", input_scaler, output_scaler);
|
|
|
|
}
|
2017-01-08 16:13:20 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void IntermediateShader::set_is_double_height(bool is_double_height, float input_offset, float output_offset) {
|
2017-01-08 19:47:16 +00:00
|
|
|
set_uniform("textureHeightDivisor", is_double_height ? 2.0f : 1.0f);
|
|
|
|
set_uniform("inputVerticalOffset", input_offset);
|
|
|
|
set_uniform("outputVerticalOffset", output_offset);
|
2017-01-08 16:13:20 +00:00
|
|
|
}
|