From 104f44f27f9d48b8d9f059b89969dcef2deb584a Mon Sep 17 00:00:00 2001
From: Thomas Harte <thomas.harte@gmail.com>
Date: Thu, 14 Apr 2016 22:20:47 -0400
Subject: [PATCH] Attempted to improve deinterlacing, gave the CRT full control
 over blend mode, switched back to 2000000Mhz audio.

---
 Machines/Electron/Electron.cpp                   | 2 +-
 OSBindings/Mac/Clock Signal/Views/CSOpenGLView.m | 3 ---
 Outputs/CRT/Internals/CRTConstants.hpp           | 3 ++-
 Outputs/CRT/Internals/CRTOpenGL.cpp              | 9 ++++++---
 4 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/Machines/Electron/Electron.cpp b/Machines/Electron/Electron.cpp
index 81226b084..9005bc3da 100644
--- a/Machines/Electron/Electron.cpp
+++ b/Machines/Electron/Electron.cpp
@@ -32,7 +32,7 @@ namespace {
 	static const unsigned int real_time_clock_interrupt_1 = 16704;
 	static const unsigned int real_time_clock_interrupt_2 = 56704;
 
-	static const unsigned int clock_rate_audio_divider = 8;
+	static const unsigned int clock_rate_audio_divider = 1;
 }
 
 #define graphics_line(v)	((((v) >> 7) - first_graphics_line + field_divider_line) % field_divider_line)
diff --git a/OSBindings/Mac/Clock Signal/Views/CSOpenGLView.m b/OSBindings/Mac/Clock Signal/Views/CSOpenGLView.m
index abbdbff4b..797b0f1b0 100644
--- a/OSBindings/Mac/Clock Signal/Views/CSOpenGLView.m	
+++ b/OSBindings/Mac/Clock Signal/Views/CSOpenGLView.m	
@@ -38,9 +38,6 @@
 
 	// Activate the display link
 	CVDisplayLinkStart(_displayLink);
-
-	glEnable(GL_BLEND);
-	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
 }
 
 static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *now, const CVTimeStamp *outputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext)
diff --git a/Outputs/CRT/Internals/CRTConstants.hpp b/Outputs/CRT/Internals/CRTConstants.hpp
index d343164f8..b2f6b9c69 100644
--- a/Outputs/CRT/Internals/CRTConstants.hpp
+++ b/Outputs/CRT/Internals/CRTConstants.hpp
@@ -49,7 +49,8 @@ const GLsizeiptr SourceVertexBufferDataSize = 87360;	// a multiple of 2 * Output
 
 // Runs are divided discretely by vertical syncs in order to put a usable bounds on the uniform used to track
 // run age; that therefore creates a discrete number of fields that are stored. This number should be the
-// number of historic fields that are required fully to 
+// number of historic fields that are required fully to complete a frame. It should be at least two and not
+// more than four.
 const int NumberOfFields = 4;
 
 }
diff --git a/Outputs/CRT/Internals/CRTOpenGL.cpp b/Outputs/CRT/Internals/CRTOpenGL.cpp
index 2987c7547..73318df59 100644
--- a/Outputs/CRT/Internals/CRTOpenGL.cpp
+++ b/Outputs/CRT/Internals/CRTOpenGL.cpp
@@ -62,6 +62,9 @@ OpenGLOutputBuilder::OpenGLOutputBuilder(unsigned int buffer_depth) :
 	}
 	_buffer_builder = std::unique_ptr<CRTInputBufferBuilder>(new CRTInputBufferBuilder(buffer_depth));
 
+	glEnable(GL_BLEND);
+	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+
 	// Create intermediate textures and bind to slots 0, 1 and 2
 	glActiveTexture(GL_TEXTURE0);
 	compositeTexture = std::unique_ptr<OpenGL::TextureTarget>(new OpenGL::TextureTarget(IntermediateBufferWidth, IntermediateBufferHeight));
@@ -226,11 +229,11 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out
 			start = _run_builders[run]->start;
 			run = (run - 1 + NumberOfFields) % NumberOfFields;
 		}
-		glUniform4fv(timestampBaseUniform, 1, timestampBases);
 
 		if(count > 0)
 		{
 			// draw
+			glUniform4fv(timestampBaseUniform, 1, timestampBases);
 			GLsizei primitive_count = (GLsizei)(count / OutputVertexSize);
 			GLsizei max_count = (GLsizei)((OutputVertexBufferDataSize - start) / OutputVertexSize);
 			if(primitive_count < max_count)
@@ -395,7 +398,7 @@ char *OpenGLOutputBuilder::get_output_vertex_shader()
 			"iSrcCoordinatesVarying = srcCoordinates;"
 			"srcCoordinatesVarying = vec2(srcCoordinates.x / textureSize.x, (srcCoordinates.y + 0.5) / textureSize.y);"
 			"float age = (timestampBase[int(lateralAndTimestampBaseOffset.y)] - timestamp) / ticksPerFrame;"
-			"alpha = 10.0 * exp(-age * 2.0);"
+			"alpha = exp(-age) + 0.2;"
 
 			"vec2 floatingPosition = (position / positionConversion) + lateralAndTimestampBaseOffset.x * scanNormal;"
 			"vec2 mappedPosition = (floatingPosition - boundsOrigin) / boundsSize;"
@@ -439,7 +442,7 @@ char *OpenGLOutputBuilder::get_output_fragment_shader(const char *sampling_funct
 
 		"void main(void)"
 		"{"
-			"fragColour = vec4(rgb_sample(texID, srcCoordinatesVarying, iSrcCoordinatesVarying), alpha);" //*sin(lateralVarying)
+			"fragColour = vec4(rgb_sample(texID, srcCoordinatesVarying, iSrcCoordinatesVarying), clamp(alpha, 0.0, 1.0)*sin(lateralVarying));" //
 		"}"
 	, sampling_function);
 }