diff --git a/Machines/Atari2600/Atari2600.cpp b/Machines/Atari2600/Atari2600.cpp index a5113400f..5615bcebb 100644 --- a/Machines/Atari2600/Atari2600.cpp +++ b/Machines/Atari2600/Atari2600.cpp @@ -30,18 +30,37 @@ Machine::Machine() : void Machine::setup_output(float aspect_ratio) { _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( - "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 y = (c >> 1) & 7u;" + "uint y = c & 14u;" "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); } +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() { delete _crt; @@ -54,11 +73,6 @@ Machine::~Machine() 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) { // get the playfield pixel and hence a proposed colour diff --git a/Outputs/CRT/CRT.cpp b/Outputs/CRT/CRT.cpp index 92f2c7a96..3f68c7684 100644 --- a/Outputs/CRT/CRT.cpp +++ b/Outputs/CRT/CRT.cpp @@ -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); // create the two flywheels - _horizontal_flywheel = std::unique_ptr(new Flywheel(_cycles_per_line, (millisecondsHorizontalRetraceTime * _cycles_per_line) >> 6)); - _vertical_flywheel = std::unique_ptr(new Flywheel(_cycles_per_line * height_of_display, scanlinesVerticalRetraceTime * _cycles_per_line)); + _horizontal_flywheel = std::unique_ptr(new Flywheel(_cycles_per_line, (millisecondsHorizontalRetraceTime * _cycles_per_line) >> 6, _cycles_per_line >> 6)); + _vertical_flywheel = std::unique_ptr(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 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) { _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()); + _openGL_output_builder->lock_output(); _frames_since_last_delegate_call = 0; } } diff --git a/Outputs/CRT/Internals/CRTOpenGL.cpp b/Outputs/CRT/Internals/CRTOpenGL.cpp index ecd1eb6f4..94df2e3b6 100644 --- a/Outputs/CRT/Internals/CRTOpenGL.cpp +++ b/Outputs/CRT/Internals/CRTOpenGL.cpp @@ -325,24 +325,48 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out glViewport(0, 0, (GLsizei)output_width, (GLsizei)output_height); glClear(GL_COLOR_BUFFER_BIT); + glActiveTexture(pixel_accumulation_texture_unit); + framebuffer->bind_texture(); framebuffer->draw((float)output_width / (float)output_height); _fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); _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) { + _output_mutex->lock(); + reset_all_OpenGL_state(); + _output_mutex->unlock(); } void OpenGLOutputBuilder::set_composite_sampling_function(const char *shader) { + _output_mutex->lock(); _composite_shader = strdup(shader); + reset_all_OpenGL_state(); + _output_mutex->unlock(); } void OpenGLOutputBuilder::set_rgb_sampling_function(const char *shader) { + _output_mutex->lock(); _rgb_shader = strdup(shader); + reset_all_OpenGL_state(); + _output_mutex->unlock(); } #pragma mark - Program compilation diff --git a/Outputs/CRT/Internals/CRTOpenGL.hpp b/Outputs/CRT/Internals/CRTOpenGL.hpp index 12c4c79eb..e562767a8 100644 --- a/Outputs/CRT/Internals/CRTOpenGL.hpp +++ b/Outputs/CRT/Internals/CRTOpenGL.hpp @@ -85,6 +85,9 @@ class OpenGLOutputBuilder { void set_timing_uniforms(); void set_colour_space_uniforms(); + void establish_OpenGL_state(); + void reset_all_OpenGL_state(); + public: OpenGLOutputBuilder(unsigned int buffer_depth); ~OpenGLOutputBuilder(); diff --git a/Outputs/CRT/Internals/Flywheel.hpp b/Outputs/CRT/Internals/Flywheel.hpp index e9bd87ed1..f52f949d8 100644 --- a/Outputs/CRT/Internals/Flywheel.hpp +++ b/Outputs/CRT/Internals/Flywheel.hpp @@ -27,13 +27,13 @@ struct Flywheel Constructs an instance of @c Flywheel. @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 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), _retrace_time(retrace_time), - _sync_error_window(standard_period >> 7), + _sync_error_window(sync_error_window), _counter(0), _expected_next_sync(standard_period), _counter_before_retrace(standard_period - retrace_time),