mirror of
https://github.com/TomHarte/CLK.git
synced 2025-01-11 08:30:55 +00:00
Merge pull request #15 from TomHarte/2600PAL
Adds an implementation of the PAL-region colour phase shift formula
This commit is contained in:
commit
f487062c62
@ -30,18 +30,37 @@ Machine::Machine() :
|
|||||||
void Machine::setup_output(float aspect_ratio)
|
void Machine::setup_output(float aspect_ratio)
|
||||||
{
|
{
|
||||||
_crt = new Outputs::CRT::CRT(228, 1, 263, Outputs::CRT::ColourSpace::YIQ, 228, 1, 1);
|
_crt = new Outputs::CRT::CRT(228, 1, 263, Outputs::CRT::ColourSpace::YIQ, 228, 1, 1);
|
||||||
|
|
||||||
|
// this is the NTSC phase offset function; see below for PAL
|
||||||
_crt->set_composite_sampling_function(
|
_crt->set_composite_sampling_function(
|
||||||
"float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)\n"
|
"float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)"
|
||||||
"{"
|
"{"
|
||||||
"uint c = texture(texID, coordinate).r;"
|
"uint c = texture(texID, coordinate).r;"
|
||||||
"uint y = (c >> 1) & 7u;"
|
"uint y = c & 14u;"
|
||||||
"uint iPhase = (c >> 4);"
|
"uint iPhase = (c >> 4);"
|
||||||
"float phaseOffset = 6.283185308 * float(iPhase + 13u) / 16.0;"
|
|
||||||
"return (float(y) / 7.0) * (1.0 - amplitude) + step(1, iPhase) * amplitude * cos(phase + phaseOffset);"
|
"float phaseOffset = 6.283185308 * float(iPhase + 13u) / 14.0;"
|
||||||
|
"return (float(y) / 14.0) * (1.0 - amplitude) + step(1, iPhase) * amplitude * cos(phase + phaseOffset);"
|
||||||
"}");
|
"}");
|
||||||
_crt->set_output_device(Outputs::CRT::Television);
|
_crt->set_output_device(Outputs::CRT::Television);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Machine::switch_region()
|
||||||
|
{
|
||||||
|
// the PAL function
|
||||||
|
_crt->set_composite_sampling_function(
|
||||||
|
"float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)"
|
||||||
|
"{"
|
||||||
|
"uint c = texture(texID, coordinate).r;"
|
||||||
|
"uint y = c & 14u;"
|
||||||
|
"uint iPhase = (c >> 4);"
|
||||||
|
|
||||||
|
"float phaseOffset = (0.5 + 2.0 * (float(iPhase&1u) - 0.5) * (float((iPhase >> 1) + (iPhase&1u)) / 14.0));"
|
||||||
|
"return (float(y) / 14.0) * (1.0 - amplitude) + step(4, (iPhase + 2u) & 15u) * amplitude * cos(phase + 6.283185308 * phaseOffset);"
|
||||||
|
"}");
|
||||||
|
_crt->set_new_timing(228, 312, Outputs::CRT::ColourSpace::YUV, 228, 1);
|
||||||
|
}
|
||||||
|
|
||||||
void Machine::close_output()
|
void Machine::close_output()
|
||||||
{
|
{
|
||||||
delete _crt;
|
delete _crt;
|
||||||
@ -54,11 +73,6 @@ Machine::~Machine()
|
|||||||
close_output();
|
close_output();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Machine::switch_region()
|
|
||||||
{
|
|
||||||
_crt->set_new_timing(228, 312, Outputs::CRT::ColourSpace::YUV, 228, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Machine::get_output_pixel(uint8_t *pixel, int offset)
|
void Machine::get_output_pixel(uint8_t *pixel, int offset)
|
||||||
{
|
{
|
||||||
// get the playfield pixel and hence a proposed colour
|
// get the playfield pixel and hence a proposed colour
|
||||||
|
@ -38,8 +38,8 @@ void CRT::set_new_timing(unsigned int cycles_per_line, unsigned int height_of_di
|
|||||||
_sync_capacitor_charge_threshold = (int)(syncCapacityLineChargeThreshold * _cycles_per_line);
|
_sync_capacitor_charge_threshold = (int)(syncCapacityLineChargeThreshold * _cycles_per_line);
|
||||||
|
|
||||||
// create the two flywheels
|
// create the two flywheels
|
||||||
_horizontal_flywheel = std::unique_ptr<Flywheel>(new Flywheel(_cycles_per_line, (millisecondsHorizontalRetraceTime * _cycles_per_line) >> 6));
|
_horizontal_flywheel = std::unique_ptr<Flywheel>(new Flywheel(_cycles_per_line, (millisecondsHorizontalRetraceTime * _cycles_per_line) >> 6, _cycles_per_line >> 6));
|
||||||
_vertical_flywheel = std::unique_ptr<Flywheel>(new Flywheel(_cycles_per_line * height_of_display, scanlinesVerticalRetraceTime * _cycles_per_line));
|
_vertical_flywheel = std::unique_ptr<Flywheel>(new Flywheel(_cycles_per_line * height_of_display, scanlinesVerticalRetraceTime * _cycles_per_line, (_cycles_per_line * height_of_display) >> 3));
|
||||||
|
|
||||||
// figure out the divisor necessary to get the horizontal flywheel into a 16-bit range
|
// figure out the divisor necessary to get the horizontal flywheel into a 16-bit range
|
||||||
unsigned int real_clock_scan_period = (_cycles_per_line * height_of_display) / (_time_multiplier * _common_output_divisor);
|
unsigned int real_clock_scan_period = (_cycles_per_line * height_of_display) / (_time_multiplier * _common_output_divisor);
|
||||||
@ -216,9 +216,13 @@ void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divi
|
|||||||
if(_delegate)
|
if(_delegate)
|
||||||
{
|
{
|
||||||
_frames_since_last_delegate_call++;
|
_frames_since_last_delegate_call++;
|
||||||
if(_frames_since_last_delegate_call == 100)
|
if(_frames_since_last_delegate_call == 20)
|
||||||
{
|
{
|
||||||
|
// Yuck: to deal with the permitted ability of the delegate to make CRT changes that require the lock to be
|
||||||
|
// asserted during the delegate call, temporarily release the lock. TODO: find a less blunt instrument.
|
||||||
|
_openGL_output_builder->unlock_output();
|
||||||
_delegate->crt_did_end_batch_of_frames(this, _frames_since_last_delegate_call, _vertical_flywheel->get_and_reset_number_of_surprises());
|
_delegate->crt_did_end_batch_of_frames(this, _frames_since_last_delegate_call, _vertical_flywheel->get_and_reset_number_of_surprises());
|
||||||
|
_openGL_output_builder->lock_output();
|
||||||
_frames_since_last_delegate_call = 0;
|
_frames_since_last_delegate_call = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -325,24 +325,48 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out
|
|||||||
glViewport(0, 0, (GLsizei)output_width, (GLsizei)output_height);
|
glViewport(0, 0, (GLsizei)output_width, (GLsizei)output_height);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
glActiveTexture(pixel_accumulation_texture_unit);
|
||||||
|
framebuffer->bind_texture();
|
||||||
framebuffer->draw((float)output_width / (float)output_height);
|
framebuffer->draw((float)output_width / (float)output_height);
|
||||||
|
|
||||||
_fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
_fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
||||||
_output_mutex->unlock();
|
_output_mutex->unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OpenGLOutputBuilder::reset_all_OpenGL_state()
|
||||||
|
{
|
||||||
|
composite_input_shader_program = nullptr;
|
||||||
|
composite_separation_filter_program = nullptr;
|
||||||
|
composite_y_filter_shader_program = nullptr;
|
||||||
|
composite_chrominance_filter_shader_program = nullptr;
|
||||||
|
rgb_input_shader_program = nullptr;
|
||||||
|
rgb_filter_shader_program = nullptr;
|
||||||
|
output_shader_program = nullptr;
|
||||||
|
framebuffer = nullptr;
|
||||||
|
_last_output_width = _last_output_height = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void OpenGLOutputBuilder::set_openGL_context_will_change(bool should_delete_resources)
|
void OpenGLOutputBuilder::set_openGL_context_will_change(bool should_delete_resources)
|
||||||
{
|
{
|
||||||
|
_output_mutex->lock();
|
||||||
|
reset_all_OpenGL_state();
|
||||||
|
_output_mutex->unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLOutputBuilder::set_composite_sampling_function(const char *shader)
|
void OpenGLOutputBuilder::set_composite_sampling_function(const char *shader)
|
||||||
{
|
{
|
||||||
|
_output_mutex->lock();
|
||||||
_composite_shader = strdup(shader);
|
_composite_shader = strdup(shader);
|
||||||
|
reset_all_OpenGL_state();
|
||||||
|
_output_mutex->unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLOutputBuilder::set_rgb_sampling_function(const char *shader)
|
void OpenGLOutputBuilder::set_rgb_sampling_function(const char *shader)
|
||||||
{
|
{
|
||||||
|
_output_mutex->lock();
|
||||||
_rgb_shader = strdup(shader);
|
_rgb_shader = strdup(shader);
|
||||||
|
reset_all_OpenGL_state();
|
||||||
|
_output_mutex->unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Program compilation
|
#pragma mark - Program compilation
|
||||||
|
@ -85,6 +85,9 @@ class OpenGLOutputBuilder {
|
|||||||
void set_timing_uniforms();
|
void set_timing_uniforms();
|
||||||
void set_colour_space_uniforms();
|
void set_colour_space_uniforms();
|
||||||
|
|
||||||
|
void establish_OpenGL_state();
|
||||||
|
void reset_all_OpenGL_state();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
OpenGLOutputBuilder(unsigned int buffer_depth);
|
OpenGLOutputBuilder(unsigned int buffer_depth);
|
||||||
~OpenGLOutputBuilder();
|
~OpenGLOutputBuilder();
|
||||||
|
@ -27,13 +27,13 @@ struct Flywheel
|
|||||||
Constructs an instance of @c Flywheel.
|
Constructs an instance of @c Flywheel.
|
||||||
|
|
||||||
@param standard_period The expected amount of time between one synchronisation and the next.
|
@param standard_period The expected amount of time between one synchronisation and the next.
|
||||||
|
|
||||||
@param retrace_time The amount of time it takes to complete a retrace.
|
@param retrace_time The amount of time it takes to complete a retrace.
|
||||||
|
@param sync_error_window The permitted deviation of sync timings from the norm.
|
||||||
*/
|
*/
|
||||||
Flywheel(unsigned int standard_period, unsigned int retrace_time) :
|
Flywheel(unsigned int standard_period, unsigned int retrace_time, unsigned int sync_error_window) :
|
||||||
_standard_period(standard_period),
|
_standard_period(standard_period),
|
||||||
_retrace_time(retrace_time),
|
_retrace_time(retrace_time),
|
||||||
_sync_error_window(standard_period >> 7),
|
_sync_error_window(sync_error_window),
|
||||||
_counter(0),
|
_counter(0),
|
||||||
_expected_next_sync(standard_period),
|
_expected_next_sync(standard_period),
|
||||||
_counter_before_retrace(standard_period - retrace_time),
|
_counter_before_retrace(standard_period - retrace_time),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user