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"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
using namespace OpenGL;
|
|
|
|
|
|
|
|
namespace {
|
2017-03-26 18:34:47 +00:00
|
|
|
const OpenGL::Shader::AttributeBinding bindings[] = {
|
2016-04-28 02:29:54 +00:00
|
|
|
{"position", 0},
|
|
|
|
{"srcCoordinates", 1},
|
|
|
|
{nullptr}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-04-28 02:41:32 +00:00
|
|
|
const char *sampler_type = use_usampler ? "usampler2D" : "sampler2D";
|
|
|
|
|
2016-04-28 02:29:54 +00:00
|
|
|
char *vertex_shader;
|
|
|
|
asprintf(&vertex_shader,
|
|
|
|
"#version 150\n"
|
|
|
|
|
2016-05-10 11:47:47 +00:00
|
|
|
"in vec2 horizontal;"
|
|
|
|
"in vec2 vertical;"
|
2016-04-28 02:29:54 +00:00
|
|
|
|
|
|
|
"uniform vec2 boundsOrigin;"
|
|
|
|
"uniform vec2 boundsSize;"
|
|
|
|
"uniform vec2 positionConversion;"
|
|
|
|
"uniform vec2 scanNormal;"
|
|
|
|
"uniform %s 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);"
|
2016-04-28 02:41:32 +00:00
|
|
|
"}", sampler_type);
|
|
|
|
|
|
|
|
char *fragment_shader;
|
|
|
|
asprintf(&fragment_shader,
|
|
|
|
"#version 150\n"
|
|
|
|
|
|
|
|
"in float lateralVarying;"
|
|
|
|
"in vec2 srcCoordinatesVarying;"
|
|
|
|
"in vec2 iSrcCoordinatesVarying;"
|
|
|
|
|
|
|
|
"out vec4 fragColour;"
|
|
|
|
|
|
|
|
"uniform %s texID;"
|
|
|
|
|
|
|
|
"\n%s\n"
|
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2016-05-16 23:55:56 +00:00
|
|
|
"fragColour = vec4(%s, 0.5);"//*cos(lateralVarying)
|
2016-04-28 02:41:32 +00:00
|
|
|
"}",
|
2016-04-28 02:58:53 +00:00
|
|
|
sampler_type, fragment_methods, colour_expression);
|
2016-04-28 02:29:54 +00:00
|
|
|
|
2016-06-24 00:52:44 +00:00
|
|
|
std::unique_ptr<OutputShader> result(new OutputShader(vertex_shader, fragment_shader, bindings));
|
2016-04-28 02:29:54 +00:00
|
|
|
free(vertex_shader);
|
2016-04-28 02:41:32 +00:00
|
|
|
free(fragment_shader);
|
2016-04-28 02:29:54 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-04-28 02:29:54 +00:00
|
|
|
GLfloat outputAspectRatioMultiplier = ((float)output_width / (float)output_height) / (4.0f / 3.0f);
|
|
|
|
|
|
|
|
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) {
|
2016-05-14 02:08:32 +00:00
|
|
|
GLfloat scan_angle = atan2f(1.0f / (float)height_of_display, 1.0f);
|
|
|
|
GLfloat scan_normal[] = { -sinf(scan_angle), cosf(scan_angle)};
|
|
|
|
GLfloat multiplier = (float)cycles_per_line / ((float)height_of_display * (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-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);
|
|
|
|
}
|