2015-07-17 00:40:46 +00:00
|
|
|
//
|
2015-07-26 19:25:11 +00:00
|
|
|
// CSCathodeRayView.m
|
|
|
|
// CLK
|
2015-07-17 00:40:46 +00:00
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2015-07-26 19:25:11 +00:00
|
|
|
#import "CSCathodeRayView.h"
|
2015-07-17 00:40:46 +00:00
|
|
|
@import CoreVideo;
|
2015-09-25 01:23:16 +00:00
|
|
|
@import GLKit;
|
2015-07-26 19:13:46 +00:00
|
|
|
#import <OpenGL/gl3.h>
|
|
|
|
#import <OpenGL/gl3ext.h>
|
2015-09-03 16:28:16 +00:00
|
|
|
#import <libkern/OSAtomic.h>
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-08-02 18:25:21 +00:00
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
@implementation CSCathodeRayView {
|
2015-07-17 00:40:46 +00:00
|
|
|
CVDisplayLinkRef displayLink;
|
2015-07-26 19:13:46 +00:00
|
|
|
|
|
|
|
GLuint _vertexShader, _fragmentShader;
|
|
|
|
GLuint _shaderProgram;
|
|
|
|
GLuint _arrayBuffer, _vertexArray;
|
|
|
|
GLint _positionAttribute;
|
2015-07-26 19:25:11 +00:00
|
|
|
GLint _textureCoordinatesAttribute;
|
2015-08-02 18:25:21 +00:00
|
|
|
GLint _lateralAttribute;
|
2015-07-27 03:50:43 +00:00
|
|
|
|
2015-09-25 01:23:16 +00:00
|
|
|
GLint _textureSizeUniform, _windowSizeUniform;
|
2016-01-07 04:14:36 +00:00
|
|
|
GLint _boundsOriginUniform, _boundsSizeUniform;
|
2015-08-06 01:45:47 +00:00
|
|
|
GLint _alphaUniform;
|
|
|
|
|
2015-09-25 01:23:16 +00:00
|
|
|
GLuint _textureName, _shadowMaskTextureName;
|
2015-07-28 00:58:51 +00:00
|
|
|
CRTSize _textureSize;
|
2015-07-31 21:47:10 +00:00
|
|
|
|
|
|
|
CRTFrame *_crtFrame;
|
2015-09-03 16:28:16 +00:00
|
|
|
|
|
|
|
NSString *_signalDecoder;
|
2016-01-04 01:41:43 +00:00
|
|
|
CSCathodeRayViewSignalType _signalType;
|
2015-09-03 16:28:16 +00:00
|
|
|
int32_t _signalDecoderGeneration;
|
|
|
|
int32_t _compiledSignalDecoderGeneration;
|
2016-01-19 04:55:52 +00:00
|
|
|
|
|
|
|
CGRect _aspectRatioCorrectedBounds;
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 01:23:16 +00:00
|
|
|
- (GLuint)textureForImageNamed:(NSString *)name
|
|
|
|
{
|
|
|
|
NSImage *const image = [NSImage imageNamed:name];
|
|
|
|
NSBitmapImageRep *bitmapRepresentation = [[NSBitmapImageRep alloc] initWithData: [image TIFFRepresentation]];
|
|
|
|
|
|
|
|
GLuint textureName;
|
|
|
|
glGenTextures(1, &textureName);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textureName);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)image.size.width, (GLsizei)image.size.height, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapRepresentation.bitmapData);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
return textureName;
|
|
|
|
}
|
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
- (void)prepareOpenGL
|
2015-07-17 00:40:46 +00:00
|
|
|
{
|
2015-07-17 01:16:21 +00:00
|
|
|
// Synchronize buffer swaps with vertical refresh rate
|
|
|
|
GLint swapInt = 1;
|
|
|
|
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// Create a display link capable of being used with all active displays
|
|
|
|
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
|
|
|
|
|
|
|
|
// Set the renderer output callback function
|
2015-07-24 00:45:07 +00:00
|
|
|
CVDisplayLinkSetOutputCallback(displayLink, DisplayLinkCallback, (__bridge void * __nullable)(self));
|
2015-07-17 01:16:21 +00:00
|
|
|
|
|
|
|
// Set the display link for the current renderer
|
|
|
|
CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
|
|
|
|
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
|
|
|
|
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);
|
2015-07-26 19:13:46 +00:00
|
|
|
|
2015-09-25 01:23:16 +00:00
|
|
|
// install the shadow mask texture as the second texture
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
_shadowMaskTextureName = [self textureForImageNamed:@"ShadowMask"];
|
|
|
|
|
|
|
|
// otherwise, we'll be working on the first texture
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
// get the shader ready, set the clear colour
|
2015-07-26 19:25:11 +00:00
|
|
|
[self.openGLContext makeCurrentContext];
|
2015-07-26 19:55:19 +00:00
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
2015-07-26 19:13:46 +00:00
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// Activate the display link
|
|
|
|
CVDisplayLinkStart(displayLink);
|
2015-07-27 03:50:43 +00:00
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
2015-07-31 21:47:10 +00:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
2015-07-17 01:16:21 +00:00
|
|
|
}
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-27 03:50:43 +00:00
|
|
|
static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *now, const CVTimeStamp *outputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext)
|
2015-07-17 01:16:21 +00:00
|
|
|
{
|
2015-07-26 19:13:46 +00:00
|
|
|
CSCathodeRayView *view = (__bridge CSCathodeRayView *)displayLinkContext;
|
2015-07-17 02:14:40 +00:00
|
|
|
[view.delegate openGLView:view didUpdateToTime:*now];
|
2015-07-17 01:16:21 +00:00
|
|
|
return kCVReturnSuccess;
|
|
|
|
}
|
2015-07-28 01:15:10 +00:00
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
CVDisplayLinkStop(displayLink);
|
|
|
|
}
|
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
// Release the display link
|
|
|
|
CVDisplayLinkRelease(displayLink);
|
2015-07-28 01:15:10 +00:00
|
|
|
|
|
|
|
// Release OpenGL buffers
|
|
|
|
[self.openGLContext makeCurrentContext];
|
|
|
|
glDeleteBuffers(1, &_arrayBuffer);
|
|
|
|
glDeleteVertexArrays(1, &_vertexArray);
|
|
|
|
glDeleteTextures(1, &_textureName);
|
2015-09-25 01:23:16 +00:00
|
|
|
glDeleteTextures(1, &_shadowMaskTextureName);
|
2015-07-28 01:15:10 +00:00
|
|
|
glDeleteProgram(_shaderProgram);
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 04:14:36 +00:00
|
|
|
- (NSPoint)backingViewSize
|
|
|
|
{
|
|
|
|
NSPoint backingSize = {.x = self.bounds.size.width, .y = self.bounds.size.height};
|
|
|
|
return [self convertPointToBacking:backingSize];
|
|
|
|
}
|
|
|
|
|
2015-07-24 02:51:53 +00:00
|
|
|
- (void)reshape
|
|
|
|
{
|
|
|
|
[super reshape];
|
|
|
|
|
2015-07-25 03:42:19 +00:00
|
|
|
[self.openGLContext makeCurrentContext];
|
2015-08-13 21:01:25 +00:00
|
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
|
|
|
2016-01-07 04:14:36 +00:00
|
|
|
NSPoint viewSize = [self backingViewSize];
|
2015-08-16 20:08:29 +00:00
|
|
|
glViewport(0, 0, (GLsizei)viewSize.x, (GLsizei)viewSize.y);
|
2015-08-13 21:01:25 +00:00
|
|
|
|
2016-01-07 04:14:36 +00:00
|
|
|
[self pushSizeUniforms];
|
2015-09-25 01:23:16 +00:00
|
|
|
|
2015-08-13 21:01:25 +00:00
|
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
2015-07-24 02:51:53 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 04:14:36 +00:00
|
|
|
- (void)setFrameBounds:(CGRect)frameBounds
|
|
|
|
{
|
|
|
|
_frameBounds = frameBounds;
|
|
|
|
|
|
|
|
[self.openGLContext makeCurrentContext];
|
|
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
|
|
|
|
|
|
[self pushSizeUniforms];
|
|
|
|
|
|
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)pushSizeUniforms
|
|
|
|
{
|
2016-01-10 02:48:53 +00:00
|
|
|
if(_shaderProgram)
|
|
|
|
{
|
2016-01-19 04:55:52 +00:00
|
|
|
NSPoint viewSize = [self backingViewSize];
|
2016-01-10 02:48:53 +00:00
|
|
|
if(_windowSizeUniform >= 0)
|
|
|
|
{
|
|
|
|
glUniform2f(_windowSizeUniform, (GLfloat)viewSize.x, (GLfloat)viewSize.y);
|
|
|
|
}
|
|
|
|
|
2016-01-19 04:55:52 +00:00
|
|
|
CGFloat outputAspectRatioMultiplier = (viewSize.x / viewSize.y) / (4.0 / 3.0);
|
|
|
|
|
|
|
|
// NSLog(@"%0.2f v %0.2f", outputAspectRatio, desiredOutputAspectRatio);
|
|
|
|
_aspectRatioCorrectedBounds = _frameBounds;
|
|
|
|
|
|
|
|
CGFloat bonusWidth = (outputAspectRatioMultiplier - 1.0f) * _frameBounds.size.width;
|
|
|
|
_aspectRatioCorrectedBounds.origin.x -= bonusWidth * 0.5f * _aspectRatioCorrectedBounds.size.width;
|
|
|
|
_aspectRatioCorrectedBounds.size.width *= outputAspectRatioMultiplier;
|
|
|
|
|
|
|
|
if(_boundsOriginUniform >= 0) glUniform2f(_boundsOriginUniform, (GLfloat)_aspectRatioCorrectedBounds.origin.x, (GLfloat)_aspectRatioCorrectedBounds.origin.y);
|
|
|
|
if(_boundsSizeUniform >= 0) glUniform2f(_boundsSizeUniform, (GLfloat)_aspectRatioCorrectedBounds.size.width, (GLfloat)_aspectRatioCorrectedBounds.size.height);
|
2016-01-10 02:48:53 +00:00
|
|
|
}
|
2016-01-07 04:14:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 22:22:51 +00:00
|
|
|
- (void)awakeFromNib
|
2015-07-24 02:51:53 +00:00
|
|
|
{
|
|
|
|
NSOpenGLPixelFormatAttribute attributes[] =
|
|
|
|
{
|
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
2015-09-11 01:30:39 +00:00
|
|
|
NSOpenGLPFASampleBuffers, 1,
|
|
|
|
NSOpenGLPFASamples, 2,
|
2015-07-24 02:51:53 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// When we're using a CoreProfile context, crash if we call a legacy OpenGL function
|
|
|
|
// This will make it much more obvious where and when such a function call is made so
|
|
|
|
// that we can remove such calls.
|
|
|
|
// Without this we'd simply get GL_INVALID_OPERATION error for calling legacy functions
|
|
|
|
// but it would be more difficult to see where that function was called.
|
|
|
|
CGLEnable([context CGLContextObj], kCGLCECrashOnRemovedFunctions);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
self.pixelFormat = pixelFormat;
|
|
|
|
self.openGLContext = context;
|
|
|
|
self.wantsBestResolutionOpenGLSurface = YES;
|
2016-01-07 04:14:36 +00:00
|
|
|
|
|
|
|
// establish default instance variable values
|
|
|
|
self.frameBounds = CGRectMake(0.0, 0.0, 1.0, 1.0);
|
2015-07-24 02:51:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 20:08:29 +00:00
|
|
|
- (GLint)formatForDepth:(unsigned int)depth
|
2015-08-06 03:36:04 +00:00
|
|
|
{
|
|
|
|
switch(depth)
|
|
|
|
{
|
|
|
|
default: return -1;
|
|
|
|
case 1: return GL_RED;
|
|
|
|
case 2: return GL_RG;
|
|
|
|
case 3: return GL_RGB;
|
|
|
|
case 4: return GL_RGBA;
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 21:47:10 +00:00
|
|
|
|
2016-01-02 22:59:21 +00:00
|
|
|
- (BOOL)pushFrame:(nonnull CRTFrame *)crtFrame
|
2015-07-26 19:13:46 +00:00
|
|
|
{
|
2015-08-13 21:01:25 +00:00
|
|
|
[[self openGLContext] makeCurrentContext];
|
|
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
|
|
|
|
2015-07-31 22:04:33 +00:00
|
|
|
BOOL hadFrame = _crtFrame ? YES : NO;
|
2015-07-26 19:13:46 +00:00
|
|
|
_crtFrame = crtFrame;
|
2015-07-26 19:55:19 +00:00
|
|
|
|
2015-08-13 21:01:25 +00:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, _crtFrame->number_of_runs * kCRTSizeOfVertex * 6, _crtFrame->runs, GL_DYNAMIC_DRAW);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, _textureName);
|
|
|
|
if(_textureSize.width != _crtFrame->size.width || _textureSize.height != _crtFrame->size.height)
|
2015-07-27 03:50:43 +00:00
|
|
|
{
|
2015-08-13 21:01:25 +00:00
|
|
|
GLint format = [self formatForDepth:_crtFrame->buffers[0].depth];
|
2015-08-16 20:08:29 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, format, _crtFrame->size.width, _crtFrame->size.height, 0, (GLenum)format, GL_UNSIGNED_BYTE, _crtFrame->buffers[0].data);
|
2015-08-13 21:01:25 +00:00
|
|
|
_textureSize = _crtFrame->size;
|
2015-07-27 03:50:43 +00:00
|
|
|
}
|
2015-08-13 21:01:25 +00:00
|
|
|
else
|
2015-08-16 20:08:29 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _crtFrame->size.width, _crtFrame->dirty_size.height, (GLenum)[self formatForDepth:_crtFrame->buffers[0].depth], GL_UNSIGNED_BYTE, _crtFrame->buffers[0].data);
|
2015-08-13 21:01:25 +00:00
|
|
|
|
|
|
|
[self drawView];
|
|
|
|
|
|
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
2015-07-31 21:47:10 +00:00
|
|
|
|
2015-07-31 22:04:33 +00:00
|
|
|
return hadFrame;
|
2015-07-26 19:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Frame output
|
|
|
|
|
2015-07-26 19:25:11 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
- (void)logErrorForObject:(GLuint)object
|
|
|
|
{
|
2016-01-10 02:48:53 +00:00
|
|
|
GLint isCompiled = 0;
|
|
|
|
glGetShaderiv(object, GL_COMPILE_STATUS, &isCompiled);
|
|
|
|
if(isCompiled == GL_FALSE)
|
|
|
|
{
|
|
|
|
GLint logLength;
|
|
|
|
glGetShaderiv(object, GL_INFO_LOG_LENGTH, &logLength);
|
|
|
|
if (logLength > 0) {
|
|
|
|
GLchar *log = (GLchar *)malloc((size_t)logLength);
|
|
|
|
glGetShaderInfoLog(object, logLength, &logLength, log);
|
|
|
|
NSLog(@"Compile log:\n%s", log);
|
|
|
|
free(log);
|
|
|
|
}
|
2015-07-26 19:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
- (GLuint)compileShader:(const char *)source type:(GLenum)type
|
|
|
|
{
|
|
|
|
GLuint shader = glCreateShader(type);
|
|
|
|
glShaderSource(shader, 1, &source, NULL);
|
|
|
|
glCompileShader(shader);
|
2015-07-26 19:25:11 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
[self logErrorForObject:shader];
|
|
|
|
#endif
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:41:43 +00:00
|
|
|
- (void)setSignalDecoder:(nonnull NSString *)signalDecoder type:(CSCathodeRayViewSignalType)type
|
2015-09-03 16:28:16 +00:00
|
|
|
{
|
2016-01-10 02:32:56 +00:00
|
|
|
_signalType = type;
|
2015-09-03 16:28:16 +00:00
|
|
|
_signalDecoder = [signalDecoder copy];
|
|
|
|
OSAtomicIncrement32(&_signalDecoderGeneration);
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:41:43 +00:00
|
|
|
- (nonnull NSString *)vertexShaderForType:(CSCathodeRayViewSignalType)type
|
|
|
|
{
|
|
|
|
// the main job of the vertex shader is just to map from an input area of [0,1]x[0,1], with the origin in the
|
|
|
|
// top left to OpenGL's [-1,1]x[-1,1] with the origin in the lower left, and to convert input data coordinates
|
|
|
|
// from integral to floating point; there's also some setup for NTSC, PAL or whatever.
|
|
|
|
|
|
|
|
NSString *const ntscVertexShaderGlobals =
|
|
|
|
@"out vec2 srcCoordinatesVarying[4];\n"
|
|
|
|
"out float phase;\n";
|
|
|
|
|
|
|
|
NSString *const ntscVertexShaderBody =
|
|
|
|
@"phase = srcCoordinates.x * 6.283185308;\n"
|
|
|
|
"\n"
|
|
|
|
"srcCoordinatesVarying[0] = vec2(srcCoordinates.x / textureSize.x, (srcCoordinates.y + 0.5) / textureSize.y);\n"
|
|
|
|
"srcCoordinatesVarying[3] = srcCoordinatesVarying[0] + vec2(0.375 / textureSize.x, 0.0);\n"
|
|
|
|
"srcCoordinatesVarying[2] = srcCoordinatesVarying[0] + vec2(0.125 / textureSize.x, 0.0);\n"
|
|
|
|
"srcCoordinatesVarying[1] = srcCoordinatesVarying[0] - vec2(0.125 / textureSize.x, 0.0);\n"
|
|
|
|
"srcCoordinatesVarying[0] = srcCoordinatesVarying[0] - vec2(0.325 / textureSize.x, 0.0);\n";
|
|
|
|
|
|
|
|
NSString *const rgbVertexShaderGlobals =
|
2016-01-12 21:42:16 +00:00
|
|
|
@"out vec2 srcCoordinatesVarying[5];\n";
|
2016-01-04 01:41:43 +00:00
|
|
|
|
|
|
|
NSString *const rgbVertexShaderBody =
|
2016-01-12 21:42:16 +00:00
|
|
|
@"srcCoordinatesVarying[2] = vec2(srcCoordinates.x / textureSize.x, (srcCoordinates.y + 0.5) / textureSize.y);\n"
|
|
|
|
"srcCoordinatesVarying[0] = srcCoordinatesVarying[1] - vec2(1.0 / textureSize.x, 0.0);\n"
|
|
|
|
"srcCoordinatesVarying[1] = srcCoordinatesVarying[1] - vec2(0.5 / textureSize.x, 0.0);\n"
|
|
|
|
"srcCoordinatesVarying[3] = srcCoordinatesVarying[1] + vec2(0.5 / textureSize.x, 0.0);\n"
|
|
|
|
"srcCoordinatesVarying[4] = srcCoordinatesVarying[1] + vec2(1.0 / textureSize.x, 0.0);\n";
|
2016-01-04 01:41:43 +00:00
|
|
|
|
|
|
|
NSString *const vertexShader =
|
|
|
|
@"#version 150\n"
|
|
|
|
"\n"
|
|
|
|
"in vec2 position;\n"
|
|
|
|
"in vec2 srcCoordinates;\n"
|
|
|
|
"in float lateral;\n"
|
|
|
|
"\n"
|
2016-01-07 04:14:36 +00:00
|
|
|
"uniform vec2 boundsOrigin;\n"
|
|
|
|
"uniform vec2 boundsSize;\n"
|
|
|
|
"\n"
|
2016-01-04 01:41:43 +00:00
|
|
|
"out float lateralVarying;\n"
|
|
|
|
"out vec2 shadowMaskCoordinates;\n"
|
|
|
|
"\n"
|
|
|
|
"uniform vec2 textureSize;\n"
|
|
|
|
"\n"
|
|
|
|
"const float shadowMaskMultiple = 600;\n"
|
|
|
|
"\n"
|
|
|
|
"%@\n"
|
|
|
|
"void main (void)\n"
|
|
|
|
"{\n"
|
|
|
|
"lateralVarying = lateral + 1.0707963267949;\n"
|
|
|
|
"\n"
|
|
|
|
"shadowMaskCoordinates = position * vec2(shadowMaskMultiple, shadowMaskMultiple * 0.85057471264368);\n"
|
|
|
|
"\n"
|
|
|
|
"%@\n"
|
|
|
|
"\n"
|
2016-01-07 04:14:36 +00:00
|
|
|
"vec2 mappedPosition = (position - boundsOrigin) / boundsSize;"
|
|
|
|
"gl_Position = vec4(mappedPosition.x * 2.0 - 1.0, 1.0 - mappedPosition.y * 2.0, 0.0, 1.0);\n"
|
2016-01-04 01:41:43 +00:00
|
|
|
"}\n";
|
|
|
|
|
2016-01-07 04:14:36 +00:00
|
|
|
// + mappedPosition.x / 131.0
|
|
|
|
|
2016-01-04 01:41:43 +00:00
|
|
|
switch(_signalType)
|
|
|
|
{
|
|
|
|
case CSCathodeRayViewSignalTypeNTSC: return [NSString stringWithFormat:vertexShader, ntscVertexShaderGlobals, ntscVertexShaderBody];
|
|
|
|
case CSCathodeRayViewSignalTypeRGB: return [NSString stringWithFormat:vertexShader, rgbVertexShaderGlobals, rgbVertexShaderBody];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (nonnull NSString *)fragmentShaderForType:(CSCathodeRayViewSignalType)type
|
|
|
|
{
|
|
|
|
NSString *const fragmentShader =
|
|
|
|
@"#version 150\n"
|
|
|
|
"\n"
|
|
|
|
"in float lateralVarying;\n"
|
|
|
|
"in vec2 shadowMaskCoordinates;\n"
|
|
|
|
"out vec4 fragColour;\n"
|
|
|
|
"\n"
|
|
|
|
"uniform sampler2D texID;\n"
|
|
|
|
"uniform sampler2D shadowMaskTexID;\n"
|
|
|
|
"uniform float alpha;\n"
|
|
|
|
"\n"
|
|
|
|
"%@\n"
|
2016-01-10 02:32:56 +00:00
|
|
|
"%%@\n"
|
2016-01-04 01:41:43 +00:00
|
|
|
"\n"
|
|
|
|
"void main(void)\n"
|
|
|
|
"{\n"
|
|
|
|
"%@\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
NSString *const ntscFragmentShaderGlobals =
|
|
|
|
@"in vec2 srcCoordinatesVarying[4];\n"
|
|
|
|
"in float phase;\n"
|
|
|
|
"\n"
|
|
|
|
"// for conversion from i and q are in the range [-0.5, 0.5] (so i needs to be multiplied by 1.1914 and q by 1.0452)\n"
|
2016-01-10 02:32:56 +00:00
|
|
|
"const mat3 yiqToRGB = mat3(1.0, 1.0, 1.0, 1.1389784, -0.3240608, -1.3176884, 0.6490692, -0.6762444, 1.7799756);\n";
|
2016-01-04 01:41:43 +00:00
|
|
|
|
|
|
|
NSString *const ntscFragmentShaderBody =
|
|
|
|
@"vec4 angles = vec4(phase) + vec4(-2.35619449019234, -0.78539816339745, 0.78539816339745, 2.35619449019234);\n"
|
|
|
|
"vec4 samples = vec4("
|
|
|
|
" sample(srcCoordinatesVarying[0], angles.x),"
|
|
|
|
" sample(srcCoordinatesVarying[1], angles.y),"
|
|
|
|
" sample(srcCoordinatesVarying[2], angles.z),"
|
|
|
|
" sample(srcCoordinatesVarying[3], angles.w)"
|
|
|
|
");\n"
|
|
|
|
"\n"
|
|
|
|
"float y = dot(vec4(0.25), samples);\n"
|
|
|
|
"samples -= vec4(y);\n"
|
|
|
|
"\n"
|
|
|
|
"float i = dot(cos(angles), samples);\n"
|
|
|
|
"float q = dot(sin(angles), samples);\n"
|
|
|
|
"\n"
|
|
|
|
"fragColour = 5.0 * texture(shadowMaskTexID, shadowMaskCoordinates) * vec4(yiqToRGB * vec3(y, i, q), 1.0);//sin(lateralVarying));\n";
|
|
|
|
|
|
|
|
NSString *const rgbFragmentShaderGlobals =
|
2016-01-12 21:42:16 +00:00
|
|
|
@"in vec2 srcCoordinatesVarying[5];\n"; // texture(shadowMaskTexID, shadowMaskCoordinates) *
|
2016-01-04 01:41:43 +00:00
|
|
|
|
|
|
|
NSString *const rgbFragmentShaderBody =
|
2016-01-12 21:42:16 +00:00
|
|
|
@"fragColour = sample(srcCoordinatesVarying[2]);";
|
|
|
|
// @"fragColour = (sample(srcCoordinatesVarying[0]) * -0.1) + \
|
|
|
|
// (sample(srcCoordinatesVarying[1]) * 0.3) + \
|
|
|
|
// (sample(srcCoordinatesVarying[2]) * 0.6) + \
|
|
|
|
// (sample(srcCoordinatesVarying[3]) * 0.3) + \
|
|
|
|
// (sample(srcCoordinatesVarying[4]) * -0.1);";
|
2016-01-12 21:27:09 +00:00
|
|
|
|
|
|
|
// dot(vec3(1.0/6.0, 2.0/3.0, 1.0/6.0), vec3(sample(srcCoordinatesVarying[0]), sample(srcCoordinatesVarying[0]), sample(srcCoordinatesVarying[0])));//sin(lateralVarying));\n";
|
2016-01-04 01:41:43 +00:00
|
|
|
|
|
|
|
switch(_signalType)
|
|
|
|
{
|
|
|
|
case CSCathodeRayViewSignalTypeNTSC: return [NSString stringWithFormat:fragmentShader, ntscFragmentShaderGlobals, ntscFragmentShaderBody];
|
|
|
|
case CSCathodeRayViewSignalTypeRGB: return [NSString stringWithFormat:fragmentShader, rgbFragmentShaderGlobals, rgbFragmentShaderBody];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
- (void)prepareShader
|
|
|
|
{
|
2015-09-03 16:28:16 +00:00
|
|
|
if(_shaderProgram)
|
|
|
|
{
|
|
|
|
glDeleteProgram(_shaderProgram);
|
|
|
|
glDeleteShader(_vertexShader);
|
|
|
|
glDeleteShader(_fragmentShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_signalDecoder)
|
|
|
|
return;
|
|
|
|
|
2016-01-04 01:41:43 +00:00
|
|
|
NSString *const vertexShader = [self vertexShaderForType:_signalType];
|
|
|
|
NSString *const fragmentShader = [self fragmentShaderForType:_signalType];
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
_shaderProgram = glCreateProgram();
|
2015-09-03 16:28:16 +00:00
|
|
|
_vertexShader = [self compileShader:[vertexShader UTF8String] type:GL_VERTEX_SHADER];
|
2016-01-04 01:41:43 +00:00
|
|
|
_fragmentShader = [self compileShader:_signalDecoder ?
|
|
|
|
[[NSString stringWithFormat:fragmentShader, _signalDecoder] UTF8String] :
|
|
|
|
[fragmentShader UTF8String]
|
|
|
|
type:GL_FRAGMENT_SHADER];
|
2015-07-26 19:13:46 +00:00
|
|
|
|
|
|
|
glAttachShader(_shaderProgram, _vertexShader);
|
|
|
|
glAttachShader(_shaderProgram, _fragmentShader);
|
|
|
|
glLinkProgram(_shaderProgram);
|
|
|
|
|
2015-07-26 19:25:11 +00:00
|
|
|
#ifdef DEBUG
|
2016-01-10 02:48:53 +00:00
|
|
|
// [self logErrorForObject:_shaderProgram];
|
2015-07-26 19:25:11 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
glGenVertexArrays(1, &_vertexArray);
|
|
|
|
glBindVertexArray(_vertexArray);
|
|
|
|
glGenBuffers(1, &_arrayBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _arrayBuffer);
|
|
|
|
|
|
|
|
glUseProgram(_shaderProgram);
|
2015-07-26 19:25:11 +00:00
|
|
|
|
2015-08-06 01:45:47 +00:00
|
|
|
_positionAttribute = glGetAttribLocation(_shaderProgram, "position");
|
|
|
|
_textureCoordinatesAttribute = glGetAttribLocation(_shaderProgram, "srcCoordinates");
|
|
|
|
_lateralAttribute = glGetAttribLocation(_shaderProgram, "lateral");
|
|
|
|
_alphaUniform = glGetUniformLocation(_shaderProgram, "alpha");
|
|
|
|
_textureSizeUniform = glGetUniformLocation(_shaderProgram, "textureSize");
|
2015-09-25 01:23:16 +00:00
|
|
|
_windowSizeUniform = glGetUniformLocation(_shaderProgram, "windowSize");
|
2016-01-07 04:14:36 +00:00
|
|
|
_boundsSizeUniform = glGetUniformLocation(_shaderProgram, "boundsSize");
|
|
|
|
_boundsOriginUniform = glGetUniformLocation(_shaderProgram, "boundsOrigin");
|
2015-09-25 01:23:16 +00:00
|
|
|
|
|
|
|
GLint texIDUniform = glGetUniformLocation(_shaderProgram, "texID");
|
|
|
|
GLint shadowMaskTexIDUniform = glGetUniformLocation(_shaderProgram, "shadowMaskTexID");
|
|
|
|
|
2016-01-07 04:14:36 +00:00
|
|
|
[self pushSizeUniforms];
|
|
|
|
|
2015-09-25 01:23:16 +00:00
|
|
|
glUniform1i(texIDUniform, 0);
|
|
|
|
glUniform1i(shadowMaskTexIDUniform, 1);
|
2015-07-26 19:25:11 +00:00
|
|
|
|
2015-08-16 20:08:29 +00:00
|
|
|
glEnableVertexAttribArray((GLuint)_positionAttribute);
|
|
|
|
glEnableVertexAttribArray((GLuint)_textureCoordinatesAttribute);
|
|
|
|
glEnableVertexAttribArray((GLuint)_lateralAttribute);
|
2015-07-26 19:25:11 +00:00
|
|
|
|
2015-08-06 01:12:33 +00:00
|
|
|
const GLsizei vertexStride = kCRTSizeOfVertex;
|
2015-08-16 20:08:29 +00:00
|
|
|
glVertexAttribPointer((GLuint)_positionAttribute, 2, GL_UNSIGNED_SHORT, GL_TRUE, vertexStride, (void *)kCRTVertexOffsetOfPosition);
|
|
|
|
glVertexAttribPointer((GLuint)_textureCoordinatesAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)kCRTVertexOffsetOfTexCoord);
|
|
|
|
glVertexAttribPointer((GLuint)_lateralAttribute, 1, GL_UNSIGNED_BYTE, GL_FALSE, vertexStride, (void *)kCRTVertexOffsetOfLateral);
|
2015-07-27 03:50:43 +00:00
|
|
|
|
|
|
|
glGenTextures(1, &_textureName);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, _textureName);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2015-07-26 19:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drawRect:(NSRect)dirtyRect
|
2015-08-13 21:01:25 +00:00
|
|
|
{
|
|
|
|
[self drawView];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drawView
|
2015-07-26 19:13:46 +00:00
|
|
|
{
|
|
|
|
[self.openGLContext makeCurrentContext];
|
2015-08-13 21:01:25 +00:00
|
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
2015-07-26 19:13:46 +00:00
|
|
|
|
2016-01-04 01:41:43 +00:00
|
|
|
while((!_shaderProgram || (_signalDecoderGeneration != _compiledSignalDecoderGeneration)) && _signalDecoder) {
|
2015-09-03 16:28:16 +00:00
|
|
|
_compiledSignalDecoderGeneration = _signalDecoderGeneration;
|
|
|
|
[self prepareShader];
|
|
|
|
}
|
|
|
|
|
2015-07-26 19:13:46 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
if (_crtFrame)
|
|
|
|
{
|
2016-01-10 02:48:53 +00:00
|
|
|
if(_textureSizeUniform >= 0) glUniform2f(_textureSizeUniform, _crtFrame->size.width, _crtFrame->size.height);
|
2015-08-16 20:08:29 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)(_crtFrame->number_of_runs*6));
|
2015-07-26 19:13:46 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 21:01:25 +00:00
|
|
|
CGLFlushDrawable([[self openGLContext] CGLContextObj]);
|
|
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
2015-07-26 19:13:46 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 00:33:24 +00:00
|
|
|
#pragma mark - NSResponder
|
|
|
|
|
|
|
|
- (BOOL)acceptsFirstResponder
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)keyDown:(NSEvent *)theEvent
|
|
|
|
{
|
|
|
|
[self.responderDelegate keyDown:theEvent];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)keyUp:(NSEvent *)theEvent
|
|
|
|
{
|
|
|
|
[self.responderDelegate keyUp:theEvent];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)flagsChanged:(NSEvent *)theEvent
|
|
|
|
{
|
|
|
|
[self.responderDelegate flagsChanged:theEvent];
|
|
|
|
}
|
|
|
|
|
2015-07-17 00:40:46 +00:00
|
|
|
@end
|