From 6d65bc9b3a0970cd36cbccf14ca7b2943097d8d6 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Wed, 11 May 2016 22:11:01 -0400 Subject: [PATCH] Permitted a wider error window on vertical sync, tidied things up a little and started trying to move towards full implementation of the OpenGL contract. --- Machines/Atari2600/Atari2600.cpp | 6 +++--- Outputs/CRT/CRT.cpp | 4 ++-- Outputs/CRT/Internals/CRTOpenGL.cpp | 23 +++++++++++++++++++++-- Outputs/CRT/Internals/CRTOpenGL.hpp | 3 +++ Outputs/CRT/Internals/Flywheel.hpp | 6 +++--- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Machines/Atari2600/Atari2600.cpp b/Machines/Atari2600/Atari2600.cpp index 429a084dc..d41f3c8cc 100644 --- a/Machines/Atari2600/Atari2600.cpp +++ b/Machines/Atari2600/Atari2600.cpp @@ -33,7 +33,7 @@ void Machine::setup_output(float aspect_ratio) // 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;" @@ -49,14 +49,14 @@ 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)\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 iPhase = (c >> 4);" "float phaseOffset = (0.5 + 2.0 * (float(iPhase&1u) - 0.5) * (float((iPhase >> 1) + (iPhase&1u)) / 14.0));" - "return (float(y) / 7.0) * (1.0 - amplitude) + step(2, iPhase) * step(iPhase, 13) * amplitude * cos(phase + 6.283185308 * phaseOffset);" + "return (float(y) / 7.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); } diff --git a/Outputs/CRT/CRT.cpp b/Outputs/CRT/CRT.cpp index e1fe9ca0c..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); diff --git a/Outputs/CRT/Internals/CRTOpenGL.cpp b/Outputs/CRT/Internals/CRTOpenGL.cpp index f929da5b2..7132eb6fd 100644 --- a/Outputs/CRT/Internals/CRTOpenGL.cpp +++ b/Outputs/CRT/Internals/CRTOpenGL.cpp @@ -325,28 +325,47 @@ 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; +} + 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); - output_shader_program = nullptr; - framebuffer = nullptr; + 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),