2016-04-28 02:29:54 +00:00
|
|
|
//
|
|
|
|
// OutputShader.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 27/04/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "OutputShader.hpp"
|
|
|
|
|
2017-11-25 00:27:49 +00:00
|
|
|
#include <cassert>
|
2017-11-16 02:29:22 +00:00
|
|
|
#include <cmath>
|
2017-11-16 02:25:01 +00:00
|
|
|
#include <sstream>
|
2016-04-28 02:29:54 +00:00
|
|
|
|
|
|
|
using namespace OpenGL;
|
|
|
|
|
2017-11-24 23:45:24 +00:00
|
|
|
std::string OutputShader::get_input_name(Input input) {
|
|
|
|
switch(input) {
|
|
|
|
case Input::Horizontal: return "horizontal";
|
|
|
|
case Input::Vertical: return "vertical";
|
2017-11-25 00:27:49 +00:00
|
|
|
|
|
|
|
// Intended to be unreachable.
|
|
|
|
default: assert(false);
|
2017-11-24 23:45:24 +00:00
|
|
|
}
|
2016-04-28 02:29:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::unique_ptr<OutputShader> OutputShader::make_shader(const char *fragment_methods, const char *colour_expression, bool use_usampler) {
|
2017-11-16 02:25:01 +00:00
|
|
|
const std::string sampler_type = use_usampler ? "usampler2D" : "sampler2D";
|
2016-04-28 02:41:32 +00:00
|
|
|
|
2017-11-16 02:25:01 +00:00
|
|
|
std::ostringstream vertex_shader;
|
|
|
|
vertex_shader <<
|
2016-04-28 02:29:54 +00:00
|
|
|
"#version 150\n"
|
|
|
|
|
2017-11-24 23:45:24 +00:00
|
|
|
"in vec2 " << get_input_name(Input::Horizontal) << ";"
|
|
|
|
"in vec2 " << get_input_name(Input::Vertical) << ";"
|
2016-04-28 02:29:54 +00:00
|
|
|
|
|
|
|
"uniform vec2 boundsOrigin;"
|
|
|
|
"uniform vec2 boundsSize;"
|
|
|
|
"uniform vec2 positionConversion;"
|
|
|
|
"uniform vec2 scanNormal;"
|
2017-11-16 02:25:01 +00:00
|
|
|
"uniform " << sampler_type << " texID;"
|
2017-01-04 03:16:52 +00:00
|
|
|
"uniform float inputScaler;"
|
2017-01-08 19:47:16 +00:00
|
|
|
"uniform int textureHeightDivisor;"
|
2016-04-28 02:29:54 +00:00
|
|
|
|
|
|
|
"out float lateralVarying;"
|
|
|
|
"out vec2 srcCoordinatesVarying;"
|
|
|
|
"out vec2 iSrcCoordinatesVarying;"
|
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2016-05-10 11:23:47 +00:00
|
|
|
"float lateral = float(gl_VertexID & 1);"
|
|
|
|
"float longitudinal = float((gl_VertexID & 2) >> 1);"
|
2016-05-10 11:47:47 +00:00
|
|
|
"float x = mix(horizontal.x, horizontal.y, longitudinal);"
|
2016-05-10 11:23:47 +00:00
|
|
|
|
2016-04-28 02:29:54 +00:00
|
|
|
"lateralVarying = lateral - 0.5;"
|
|
|
|
|
2016-05-10 11:47:47 +00:00
|
|
|
"vec2 vSrcCoordinates = vec2(x, vertical.y);"
|
2017-01-08 19:47:16 +00:00
|
|
|
"ivec2 textureSize = textureSize(texID, 0) * ivec2(1, textureHeightDivisor);"
|
2016-05-10 11:23:47 +00:00
|
|
|
"iSrcCoordinatesVarying = vSrcCoordinates;"
|
2017-01-04 03:16:52 +00:00
|
|
|
"srcCoordinatesVarying = vec2(inputScaler * vSrcCoordinates.x / textureSize.x, (vSrcCoordinates.y + 0.5) / textureSize.y);"
|
2017-01-08 19:47:16 +00:00
|
|
|
"srcCoordinatesVarying.x = srcCoordinatesVarying.x - mod(srcCoordinatesVarying.x, 1.0 / textureSize.x);"
|
2016-04-28 02:29:54 +00:00
|
|
|
|
2016-05-10 11:47:47 +00:00
|
|
|
"vec2 vPosition = vec2(x, vertical.x);"
|
2016-05-10 11:23:47 +00:00
|
|
|
"vec2 floatingPosition = (vPosition / positionConversion) + lateral * scanNormal;"
|
2016-04-28 02:29:54 +00:00
|
|
|
"vec2 mappedPosition = (floatingPosition - boundsOrigin) / boundsSize;"
|
|
|
|
"gl_Position = vec4(mappedPosition.x * 2.0 - 1.0, 1.0 - mappedPosition.y * 2.0, 0.0, 1.0);"
|
2017-11-16 02:25:01 +00:00
|
|
|
"}";
|
2016-04-28 02:41:32 +00:00
|
|
|
|
2017-11-16 02:25:01 +00:00
|
|
|
std::ostringstream fragment_shader;
|
|
|
|
fragment_shader <<
|
2016-04-28 02:41:32 +00:00
|
|
|
"#version 150\n"
|
|
|
|
|
|
|
|
"in float lateralVarying;"
|
|
|
|
"in vec2 srcCoordinatesVarying;"
|
|
|
|
"in vec2 iSrcCoordinatesVarying;"
|
|
|
|
|
|
|
|
"out vec4 fragColour;"
|
|
|
|
|
2017-11-16 02:25:01 +00:00
|
|
|
"uniform " << sampler_type << " texID;"
|
2017-08-10 19:17:08 +00:00
|
|
|
"uniform float gamma;"
|
2016-04-28 02:41:32 +00:00
|
|
|
|
2017-11-16 02:25:01 +00:00
|
|
|
<< fragment_methods <<
|
2016-04-28 02:41:32 +00:00
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2017-11-16 02:25:01 +00:00
|
|
|
"fragColour = vec4(pow(" << colour_expression << ", vec3(gamma)), 0.5);"//*cos(lateralVarying)
|
|
|
|
"}";
|
2016-04-28 02:29:54 +00:00
|
|
|
|
2017-11-24 23:45:24 +00:00
|
|
|
return std::unique_ptr<OutputShader>(new OutputShader(vertex_shader.str(), fragment_shader.str(), {
|
|
|
|
{get_input_name(Input::Horizontal), 0},
|
|
|
|
{get_input_name(Input::Vertical), 1}
|
|
|
|
}));
|
2016-04-28 02:29:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void OutputShader::set_output_size(unsigned int output_width, unsigned int output_height, Outputs::CRT::Rect visible_area) {
|
2017-10-21 23:49:04 +00:00
|
|
|
GLfloat outputAspectRatioMultiplier = (static_cast<float>(output_width) / static_cast<float>(output_height)) / (4.0f / 3.0f);
|
2016-04-28 02:29:54 +00:00
|
|
|
|
|
|
|
GLfloat bonusWidth = (outputAspectRatioMultiplier - 1.0f) * visible_area.size.width;
|
|
|
|
visible_area.origin.x -= bonusWidth * 0.5f * visible_area.size.width;
|
|
|
|
visible_area.size.width *= outputAspectRatioMultiplier;
|
|
|
|
|
2016-05-14 02:08:32 +00:00
|
|
|
set_uniform("boundsOrigin", (GLfloat)visible_area.origin.x, (GLfloat)visible_area.origin.y);
|
|
|
|
set_uniform("boundsSize", (GLfloat)visible_area.size.width, (GLfloat)visible_area.size.height);
|
2016-04-28 02:29:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void OutputShader::set_source_texture_unit(GLenum unit) {
|
2016-05-14 02:08:32 +00:00
|
|
|
set_uniform("texID", (GLint)(unit - GL_TEXTURE0));
|
2016-04-28 02:29:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void OutputShader::set_timing(unsigned int height_of_display, unsigned int cycles_per_line, unsigned int horizontal_scan_period, unsigned int vertical_scan_period, unsigned int vertical_period_divider) {
|
2017-10-21 23:49:04 +00:00
|
|
|
GLfloat scan_angle = atan2f(1.0f / static_cast<float>(height_of_display), 1.0f);
|
2016-05-14 02:08:32 +00:00
|
|
|
GLfloat scan_normal[] = { -sinf(scan_angle), cosf(scan_angle)};
|
2017-10-21 23:49:04 +00:00
|
|
|
GLfloat multiplier = static_cast<float>(cycles_per_line) / (static_cast<float>(height_of_display) * static_cast<float>(horizontal_scan_period));
|
2016-04-28 02:29:54 +00:00
|
|
|
scan_normal[0] *= multiplier;
|
|
|
|
scan_normal[1] *= multiplier;
|
|
|
|
|
2016-05-14 02:08:32 +00:00
|
|
|
set_uniform("scanNormal", scan_normal[0], scan_normal[1]);
|
|
|
|
set_uniform("positionConversion", (GLfloat)horizontal_scan_period, (GLfloat)vertical_scan_period / (GLfloat)vertical_period_divider);
|
2016-04-28 02:29:54 +00:00
|
|
|
}
|
2017-01-04 03:16:52 +00:00
|
|
|
|
2017-08-10 19:17:08 +00:00
|
|
|
void OutputShader::set_gamma_ratio(float ratio) {
|
|
|
|
set_uniform("gamma", ratio);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void OutputShader::set_input_width_scaler(float input_scaler) {
|
2017-01-04 03:16:52 +00:00
|
|
|
set_uniform("inputScaler", input_scaler);
|
|
|
|
}
|
2017-01-08 19:47:16 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void OutputShader::set_origin_is_double_height(bool is_double_height) {
|
2017-01-08 19:47:16 +00:00
|
|
|
set_uniform("textureHeightDivisor", is_double_height ? 2 : 1);
|
|
|
|
}
|