1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-04 18:29:40 +00:00

It's the worst fragment shader you'll ever see. The worst. But this attempts NTSC composite video emulation, artefacts included.

This commit is contained in:
Thomas Harte 2015-09-01 22:36:45 -04:00
parent 5fe0cff692
commit cb6e6e8acb
2 changed files with 66 additions and 27 deletions

View File

@ -20,7 +20,7 @@ Machine::Machine()
_horizontalTimer = 0;
_lastOutputStateDuration = 0;
_lastOutputState = OutputState::Sync;
_crt = new Outputs::CRT(228, 262, 1, 4);
_crt = new Outputs::CRT(228, 262, 1, 1);
_piaTimerStatus = 0xff;
memset(_collisions, 0xff, sizeof(_collisions));
_rom = nullptr;
@ -46,22 +46,6 @@ void Machine::switch_region()
void Machine::get_output_pixel(uint8_t *pixel, int offset)
{
static const uint8_t palette[16][3] =
{
{255, 255, 255}, {253, 250, 115}, {236, 199, 125}, {252, 187, 151},
{252, 180, 181}, {235, 177, 223}, {211, 178, 250}, {187, 182, 250},
{164, 186, 250}, {166, 201, 250}, {164, 224, 251}, {165, 251, 213},
{185, 251, 187}, {201, 250, 168}, {225, 235, 160}, {252, 223, 145}
};
static const uint8_t alphaValues[8] =
{
// 0, 64, 108, 144, 176, 200, 220, 255
// };
//
// {
69, 134, 108, 161, 186, 210, 235, 255
};
// get the playfield pixel and hence a proposed colour
uint8_t playfieldPixel = _playfield[offset >> 2];
uint8_t playfieldColour = ((_playfieldControl&6) == 2) ? _playerColour[offset / 80] : _playfieldColour;
@ -163,10 +147,7 @@ void Machine::get_output_pixel(uint8_t *pixel, int offset)
}
// map that colour to an RGBA
pixel[0] = palette[outputColour >> 4][0];
pixel[1] = palette[outputColour >> 4][1];
pixel[2] = palette[outputColour >> 4][2];
pixel[3] = alphaValues[(outputColour >> 1)&7];
*pixel = outputColour;
}
// in imputing the knowledge that all we're dealing with is the rollover from 159 to 0,
@ -249,7 +230,7 @@ void Machine::output_pixels(unsigned int count)
if(_horizontalTimer < (_vBlankExtend ? 152 : 160))
{
if(_outputBuffer)
get_output_pixel(&_outputBuffer[_lastOutputStateDuration * 4], 159 - _horizontalTimer);
get_output_pixel(&_outputBuffer[_lastOutputStateDuration], 159 - _horizontalTimer);
// increment all graphics counters
increment_object_counter(0);

View File

@ -179,15 +179,23 @@ const char *vertexShader =
"in vec2 srcCoordinates;\n"
"in float lateral;\n"
"\n"
"out vec2 srcCoordinatesVarying;\n"
"out vec2 srcCoordinatesVarying[7];\n"
"out float lateralVarying;\n"
"out float phase;\n"
"\n"
"uniform vec2 textureSize;\n"
"\n"
"void main (void)\n"
"{\n"
"srcCoordinatesVarying = vec2(srcCoordinates.x / textureSize.x, (srcCoordinates.y + 0.5) / textureSize.y);\n"
"srcCoordinatesVarying[2] = vec2(srcCoordinates.x / textureSize.x, (srcCoordinates.y + 0.5) / textureSize.y);\n"
"srcCoordinatesVarying[0] = srcCoordinatesVarying[2] - vec2(0.4 / textureSize.x, 0.0);\n"
"srcCoordinatesVarying[1] = srcCoordinatesVarying[2] - vec2(0.2 / textureSize.x, 0.0);\n"
"srcCoordinatesVarying[4] = srcCoordinatesVarying[2] + vec2(0.4 / textureSize.x, 0.0);\n"
"srcCoordinatesVarying[3] = srcCoordinatesVarying[2] + vec2(0.2 / textureSize.x, 0.0);\n"
"srcCoordinatesVarying[5] = srcCoordinatesVarying[2] - vec2(0.1 / textureSize.x, 0.0);\n"
"srcCoordinatesVarying[6] = srcCoordinatesVarying[2] + vec2(0.1 / textureSize.x, 0.0);\n"
"lateralVarying = lateral + 1.0707963267949;\n"
"phase = srcCoordinates.x;\n"
"gl_Position = vec4(position.x * 2.0 - 1.0, 1.0 - position.y * 2.0 , 0.0, 1.0);\n" // + position.x / 131.0
"}\n";
@ -195,16 +203,66 @@ const char *vertexShader =
const char *fragmentShader =
"#version 150\n"
"\n"
"in vec2 srcCoordinatesVarying;\n"
"in float lateralVarying;"
"in vec2 srcCoordinatesVarying[7];\n"
"in float lateralVarying;\n"
"in float phase;\n"
"out vec4 fragColour;\n"
"\n"
"uniform sampler2D texID;\n"
"uniform float alpha;\n"
"\n"
"float angles[5];\n"
"\n"
"float sample(float c, float angle)\n"
"{\n"
"float y = 0.1 + (mod(c * 128.0, 8.0) / 8.0) * 0.9;\n"
"float rawCh = (c * 16.0);\n"
"float ch = mod(rawCh, 16.0);\n"
"float aOffset = 2.0 * 3.141592654 * (ch - mod(ch, 1.0)) / 17.0;\n"
"return y + step(1.0, rawCh) * (0.1 * sin(angle + aOffset));\n"
"}\n"
"\n"
"void main(void)\n"
"{\n"
"fragColour = texture(texID, srcCoordinatesVarying) * vec4(1.0, 1.0, 1.0, 1.0);\n" // .a = sin(lateralVarying)
"float samples[7];\n"
"float angles[7], i[5], q[5];\n"
"\n"
"angles[0] = mod(phase + 0.6, 1.0) * 2.0 * 3.141592654;\n"
"angles[1] = mod(phase + 0.8, 1.0) * 2.0 * 3.141592654;\n"
"angles[2] = mod(phase, 1.0) * 2.0 * 3.141592654;\n"
"angles[3] = mod(phase + 0.2, 1.0) * 2.0 * 3.141592654;\n"
"angles[4] = mod(phase + 0.4, 1.0) * 2.0 * 3.141592654;\n"
"angles[5] = mod(phase + 0.9, 1.0) * 2.0 * 3.141592654;\n"
"angles[6] = mod(phase + 0.1, 1.0) * 2.0 * 3.141592654;\n"
"\n"
"samples[0] = sample(texture(texID, srcCoordinatesVarying[0]).r, angles[0]);\n"
"samples[1] = sample(texture(texID, srcCoordinatesVarying[1]).r, angles[1]);\n"
"samples[2] = sample(texture(texID, srcCoordinatesVarying[2]).r, angles[2]);\n"
"samples[3] = sample(texture(texID, srcCoordinatesVarying[3]).r, angles[3]);\n"
"samples[4] = sample(texture(texID, srcCoordinatesVarying[4]).r, angles[4]);\n"
"samples[5] = sample(texture(texID, srcCoordinatesVarying[5]).r, angles[5]);\n"
"samples[6] = sample(texture(texID, srcCoordinatesVarying[6]).r, angles[6]);\n"
"float y = (samples[0] + samples[1] + samples[2] + samples[3] + samples[4]) / 5.0;\n"
"\n"
"i[0] = (samples[1] - y) * sin(angles[1]);\n"
"i[1] = (samples[5] - y) * sin(angles[5]);\n"
"i[2] = (samples[2] - y) * sin(angles[2]);\n"
"i[3] = (samples[6] - y) * sin(angles[6]);\n"
"i[4] = (samples[3] - y) * sin(angles[3]);\n"
"float iSum = (i[0] + i[1] + i[2] + i[3] + i[4]) * 1.1914;\n"
"\n"
"q[0] = (samples[1] - y) * cos(angles[1]);\n"
"q[1] = (samples[5] - y) * cos(angles[5]);\n"
"q[2] = (samples[2] - y) * cos(angles[2]);\n"
"q[3] = (samples[6] - y) * cos(angles[6]);\n"
"q[4] = (samples[3] - y) * cos(angles[3]);\n"
"float qSum = (q[0] + q[1] + q[2] + q[3] + q[4]) * 1.0452;\n"
"\n"
"\n"
"fragColour = vec4( y + 0.965 * iSum + 0.621 * qSum, "
"y - 0.272 * iSum - 0.647 * qSum, "
"y - 1.106 * iSum + 1.703 * qSum, "
"1.0);\n"
"}\n";
#if defined(DEBUG)