2020-08-04 22:22:14 +00:00
|
|
|
//
|
|
|
|
// ScanTarget.m
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 02/08/2020.
|
|
|
|
// Copyright © 2020 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "CSScanTarget.h"
|
|
|
|
|
2020-08-09 03:11:44 +00:00
|
|
|
#include <atomic>
|
2020-08-04 22:22:14 +00:00
|
|
|
#import <Metal/Metal.h>
|
2020-08-08 02:03:54 +00:00
|
|
|
#include "BufferingScanTarget.hpp"
|
2020-08-04 22:22:14 +00:00
|
|
|
|
2020-08-08 01:19:17 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Uniforms {
|
|
|
|
int32_t scale[2];
|
|
|
|
float lineWidth;
|
2020-08-08 02:29:24 +00:00
|
|
|
float aspectRatioMultiplier;
|
2020-08-12 02:11:50 +00:00
|
|
|
simd::float3x3 toRGB;
|
|
|
|
simd::float3x3 fromRGB;
|
2020-08-08 01:19:17 +00:00
|
|
|
};
|
|
|
|
|
2020-08-08 02:03:54 +00:00
|
|
|
constexpr size_t NumBufferedScans = 2048;
|
2020-08-09 02:49:02 +00:00
|
|
|
constexpr size_t NumBufferedLines = 2048;
|
2020-08-08 02:03:54 +00:00
|
|
|
|
2020-08-09 21:59:52 +00:00
|
|
|
/// The shared resource options this app would most favour; applied as widely as possible.
|
|
|
|
constexpr MTLResourceOptions SharedResourceOptionsStandard = MTLResourceCPUCacheModeWriteCombined | MTLResourceStorageModeShared;
|
|
|
|
|
|
|
|
/// The shared resource options used for the write-area texture; on macOS it can't be MTLResourceStorageModeShared so this is a carve-out.
|
|
|
|
constexpr MTLResourceOptions SharedResourceOptionsTexture = MTLResourceCPUCacheModeWriteCombined | MTLResourceStorageModeManaged;
|
|
|
|
|
2020-08-09 03:11:44 +00:00
|
|
|
#define uniforms() reinterpret_cast<Uniforms *>(_uniformsBuffer.contents)
|
|
|
|
|
2020-08-08 01:19:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 02:49:02 +00:00
|
|
|
using BufferingScanTarget = Outputs::Display::BufferingScanTarget;
|
|
|
|
|
2020-08-04 22:22:14 +00:00
|
|
|
@implementation CSScanTarget {
|
|
|
|
id<MTLCommandQueue> _commandQueue;
|
2020-08-05 01:49:01 +00:00
|
|
|
|
|
|
|
id<MTLFunction> _vertexShader;
|
|
|
|
id<MTLFunction> _fragmentShader;
|
2020-08-09 22:41:15 +00:00
|
|
|
id<MTLRenderPipelineState> _scanPipeline;
|
2020-08-08 01:19:17 +00:00
|
|
|
|
|
|
|
// Buffers.
|
|
|
|
id<MTLBuffer> _uniformsBuffer;
|
2020-08-09 02:49:02 +00:00
|
|
|
|
2020-08-08 01:19:17 +00:00
|
|
|
id<MTLBuffer> _scansBuffer;
|
2020-08-09 02:49:02 +00:00
|
|
|
id<MTLBuffer> _linesBuffer;
|
|
|
|
id<MTLBuffer> _writeAreaBuffer;
|
2020-08-08 02:29:24 +00:00
|
|
|
|
2020-08-09 21:59:52 +00:00
|
|
|
// Textures.
|
|
|
|
id<MTLTexture> _writeAreaTexture;
|
|
|
|
size_t _bytesPerInputPixel;
|
|
|
|
size_t _totalTextureBytes;
|
|
|
|
|
2020-08-09 02:49:02 +00:00
|
|
|
// The scan target in C++-world terms and the non-GPU storage for it.
|
|
|
|
BufferingScanTarget _scanTarget;
|
|
|
|
BufferingScanTarget::LineMetadata _lineMetadataBuffer[NumBufferedLines];
|
2020-08-09 03:11:44 +00:00
|
|
|
std::atomic_bool _isDrawing;
|
2020-08-04 22:22:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 23:44:56 +00:00
|
|
|
- (nonnull instancetype)initWithView:(nonnull MTKView *)view {
|
2020-08-04 22:22:14 +00:00
|
|
|
self = [super init];
|
|
|
|
if(self) {
|
2020-08-04 23:44:56 +00:00
|
|
|
_commandQueue = [view.device newCommandQueue];
|
2020-08-05 01:49:01 +00:00
|
|
|
|
2020-08-09 03:11:44 +00:00
|
|
|
// Allocate space for uniforms.
|
|
|
|
_uniformsBuffer = [view.device
|
|
|
|
newBufferWithLength:sizeof(Uniforms)
|
|
|
|
options:MTLResourceCPUCacheModeWriteCombined | MTLResourceStorageModeShared];
|
2020-08-05 21:27:43 +00:00
|
|
|
|
2020-08-09 02:49:02 +00:00
|
|
|
// Allocate buffers for scans and lines and for the write area texture.
|
2020-08-08 02:03:54 +00:00
|
|
|
_scansBuffer = [view.device
|
|
|
|
newBufferWithLength:sizeof(Outputs::Display::BufferingScanTarget::Scan)*NumBufferedScans
|
2020-08-09 21:59:52 +00:00
|
|
|
options:SharedResourceOptionsStandard];
|
2020-08-09 02:49:02 +00:00
|
|
|
_linesBuffer = [view.device
|
|
|
|
newBufferWithLength:sizeof(Outputs::Display::BufferingScanTarget::Line)*NumBufferedLines
|
2020-08-09 21:59:52 +00:00
|
|
|
options:SharedResourceOptionsStandard];
|
2020-08-09 02:49:02 +00:00
|
|
|
_writeAreaBuffer = [view.device
|
|
|
|
newBufferWithLength:BufferingScanTarget::WriteAreaWidth*BufferingScanTarget::WriteAreaHeight*4
|
2020-08-09 21:59:52 +00:00
|
|
|
options:SharedResourceOptionsTexture];
|
2020-08-09 02:49:02 +00:00
|
|
|
|
|
|
|
// Install all that storage in the buffering scan target.
|
|
|
|
_scanTarget.set_write_area(reinterpret_cast<uint8_t *>(_writeAreaBuffer.contents));
|
|
|
|
_scanTarget.set_line_buffer(reinterpret_cast<BufferingScanTarget::Line *>(_linesBuffer.contents), _lineMetadataBuffer, NumBufferedLines);
|
|
|
|
_scanTarget.set_scan_buffer(reinterpret_cast<BufferingScanTarget::Scan *>(_scansBuffer.contents), NumBufferedScans);
|
2020-08-10 01:19:07 +00:00
|
|
|
|
|
|
|
// Set initial aspect-ratio multiplier.
|
|
|
|
[self mtkView:view drawableSizeWillChange:view.drawableSize];
|
2020-08-04 22:22:14 +00:00
|
|
|
}
|
2020-08-08 02:03:54 +00:00
|
|
|
|
2020-08-04 22:22:14 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-08-04 23:44:56 +00:00
|
|
|
/*!
|
|
|
|
@method mtkView:drawableSizeWillChange:
|
|
|
|
@abstract Called whenever the drawableSize of the view will change
|
|
|
|
@discussion Delegate can recompute view and projection matricies or regenerate any buffers to be compatible with the new view size or resolution
|
|
|
|
@param view MTKView which called this method
|
|
|
|
@param size New drawable size in pixels
|
|
|
|
*/
|
|
|
|
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size {
|
2020-08-09 03:11:44 +00:00
|
|
|
uniforms()->aspectRatioMultiplier = float((4.0 / 3.0) / (size.width / size.height));
|
2020-08-04 23:44:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 02:11:50 +00:00
|
|
|
- (void)setModals:(const Outputs::Display::ScanTarget::Modals &)modals view:(nonnull MTKView *)view {
|
|
|
|
//
|
|
|
|
// Populate uniforms.
|
|
|
|
//
|
|
|
|
uniforms()->scale[0] = modals.output_scale.x;
|
|
|
|
uniforms()->scale[1] = modals.output_scale.y;
|
|
|
|
uniforms()->lineWidth = 1.0f / modals.expected_vertical_lines;
|
|
|
|
|
|
|
|
const auto toRGB = to_rgb_matrix(modals.composite_colour_space);
|
|
|
|
uniforms()->toRGB = simd::float3x3(
|
|
|
|
simd::float3{toRGB[0], toRGB[1], toRGB[2]},
|
|
|
|
simd::float3{toRGB[3], toRGB[4], toRGB[5]},
|
|
|
|
simd::float3{toRGB[6], toRGB[7], toRGB[8]}
|
|
|
|
);
|
|
|
|
|
|
|
|
const auto fromRGB = from_rgb_matrix(modals.composite_colour_space);
|
|
|
|
uniforms()->fromRGB = simd::float3x3(
|
|
|
|
simd::float3{fromRGB[0], fromRGB[1], fromRGB[2]},
|
|
|
|
simd::float3{fromRGB[3], fromRGB[4], fromRGB[5]},
|
|
|
|
simd::float3{fromRGB[6], fromRGB[7], fromRGB[8]}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Generate input texture.
|
|
|
|
//
|
|
|
|
MTLPixelFormat pixelFormat;
|
|
|
|
_bytesPerInputPixel = size_for_data_type(modals.input_data_type);
|
|
|
|
if(data_type_is_normalised(modals.input_data_type)) {
|
|
|
|
switch(_bytesPerInputPixel) {
|
|
|
|
default:
|
|
|
|
case 1: pixelFormat = MTLPixelFormatR8Unorm; break;
|
|
|
|
case 2: pixelFormat = MTLPixelFormatRG8Unorm; break;
|
|
|
|
case 4: pixelFormat = MTLPixelFormatRGBA8Unorm; break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(_bytesPerInputPixel) {
|
|
|
|
default:
|
|
|
|
case 1: pixelFormat = MTLPixelFormatR8Uint; break;
|
|
|
|
case 2: pixelFormat = MTLPixelFormatRG8Uint; break;
|
|
|
|
case 4: pixelFormat = MTLPixelFormatRGBA8Uint; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MTLTextureDescriptor *const textureDescriptor = [MTLTextureDescriptor
|
|
|
|
texture2DDescriptorWithPixelFormat:pixelFormat
|
|
|
|
width:BufferingScanTarget::WriteAreaWidth
|
|
|
|
height:BufferingScanTarget::WriteAreaHeight
|
|
|
|
mipmapped:NO];
|
|
|
|
textureDescriptor.resourceOptions = SharedResourceOptionsTexture;
|
|
|
|
|
|
|
|
// TODO: the call below is the only reason why this project now requires macOS 10.13; is it all that helpful versus just uploading each frame?
|
|
|
|
const NSUInteger bytesPerRow = BufferingScanTarget::WriteAreaWidth * _bytesPerInputPixel;
|
|
|
|
_writeAreaTexture = [_writeAreaBuffer
|
|
|
|
newTextureWithDescriptor:textureDescriptor
|
|
|
|
offset:0
|
|
|
|
bytesPerRow:bytesPerRow];
|
|
|
|
_totalTextureBytes = bytesPerRow * BufferingScanTarget::WriteAreaHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Generate pipeline.
|
|
|
|
//
|
|
|
|
id<MTLLibrary> library = [view.device newDefaultLibrary];
|
|
|
|
MTLRenderPipelineDescriptor *pipelineDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
|
|
|
|
pipelineDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat;
|
|
|
|
|
|
|
|
// TODO: logic somewhat more complicated than this, probably
|
|
|
|
pipelineDescriptor.vertexFunction = [library newFunctionWithName:@"scanToDisplay"];
|
|
|
|
switch(modals.input_data_type) {
|
|
|
|
case Outputs::Display::InputDataType::Luminance1:
|
|
|
|
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"sampleLuminance1"];
|
|
|
|
break;
|
|
|
|
case Outputs::Display::InputDataType::Luminance8:
|
|
|
|
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"sampleLuminance8"];
|
|
|
|
break;
|
|
|
|
case Outputs::Display::InputDataType::PhaseLinkedLuminance8:
|
|
|
|
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"samplePhaseLinkedLuminance8"];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Outputs::Display::InputDataType::Luminance8Phase8:
|
|
|
|
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"sampleLuminance8Phase8"];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Outputs::Display::InputDataType::Red1Green1Blue1:
|
|
|
|
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"sampleRed1Green1Blue1"];
|
|
|
|
break;
|
|
|
|
case Outputs::Display::InputDataType::Red2Green2Blue2:
|
|
|
|
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"sampleRed2Green2Blue2"];
|
|
|
|
break;
|
|
|
|
case Outputs::Display::InputDataType::Red4Green4Blue4:
|
|
|
|
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"sampleRed4Green4Blue4"];
|
|
|
|
break;
|
|
|
|
case Outputs::Display::InputDataType::Red8Green8Blue8:
|
|
|
|
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"sampleRed8Green8Blue8"];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_scanPipeline = [view.device newRenderPipelineStateWithDescriptor:pipelineDescriptor error:nil];
|
|
|
|
}
|
|
|
|
|
2020-08-04 23:44:56 +00:00
|
|
|
/*!
|
|
|
|
@method drawInMTKView:
|
|
|
|
@abstract Called on the delegate when it is asked to render into the view
|
|
|
|
@discussion Called on the delegate when it is asked to render into the view
|
|
|
|
*/
|
|
|
|
- (void)drawInMTKView:(nonnull MTKView *)view {
|
2020-08-09 03:11:44 +00:00
|
|
|
const Outputs::Display::ScanTarget::Modals *const newModals = _scanTarget.new_modals();
|
|
|
|
if(newModals) {
|
2020-08-12 02:11:50 +00:00
|
|
|
[self setModals:*newModals view:view];
|
2020-08-09 03:11:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:41:15 +00:00
|
|
|
// Generate a command encoder for the view.
|
|
|
|
id <MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
|
|
|
|
MTLRenderPassDescriptor *const descriptor = view.currentRenderPassDescriptor;
|
|
|
|
id <MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];
|
|
|
|
|
|
|
|
// Drawing. Just scans.
|
|
|
|
[encoder setRenderPipelineState:_scanPipeline];
|
2020-08-08 01:19:17 +00:00
|
|
|
|
2020-08-09 22:41:15 +00:00
|
|
|
[encoder setFragmentTexture:_writeAreaTexture atIndex:0];
|
2020-08-08 21:12:49 +00:00
|
|
|
[encoder setVertexBuffer:_scansBuffer offset:0 atIndex:0];
|
2020-08-08 01:19:17 +00:00
|
|
|
[encoder setVertexBuffer:_uniformsBuffer offset:0 atIndex:1];
|
2020-08-12 02:11:50 +00:00
|
|
|
[encoder setFragmentBuffer:_uniformsBuffer offset:0 atIndex:0];
|
2020-08-08 01:19:17 +00:00
|
|
|
|
2020-08-09 03:11:44 +00:00
|
|
|
_scanTarget.perform([=] (const BufferingScanTarget::OutputArea &outputArea) {
|
2020-08-09 21:59:52 +00:00
|
|
|
// Ensure texture changes are noted.
|
2020-08-10 00:45:14 +00:00
|
|
|
const auto writeAreaModificationStart = size_t(outputArea.start.write_area_x + outputArea.start.write_area_y * 2048) * _bytesPerInputPixel;
|
|
|
|
const auto writeAreaModificationEnd = size_t(outputArea.end.write_area_x + outputArea.end.write_area_y * 2048) * _bytesPerInputPixel;
|
2020-08-09 21:59:52 +00:00
|
|
|
if(writeAreaModificationStart != writeAreaModificationEnd) {
|
|
|
|
if(writeAreaModificationStart < writeAreaModificationEnd) {
|
|
|
|
[_writeAreaBuffer didModifyRange:NSMakeRange(writeAreaModificationStart, writeAreaModificationEnd - writeAreaModificationStart)];
|
|
|
|
} else {
|
|
|
|
[_writeAreaBuffer didModifyRange:NSMakeRange(writeAreaModificationStart, _totalTextureBytes - writeAreaModificationStart)];
|
|
|
|
if(writeAreaModificationEnd) {
|
|
|
|
[_writeAreaBuffer didModifyRange:NSMakeRange(0, writeAreaModificationEnd)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-09 03:11:44 +00:00
|
|
|
// TEMPORARY: just draw the scans.
|
|
|
|
if(outputArea.start.scan != outputArea.end.scan) {
|
|
|
|
if(outputArea.start.scan < outputArea.end.scan) {
|
|
|
|
[encoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4 instanceCount:outputArea.end.scan - outputArea.start.scan baseInstance:outputArea.start.scan];
|
|
|
|
} else {
|
|
|
|
[encoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4 instanceCount:NumBufferedScans - outputArea.start.scan baseInstance:outputArea.start.scan];
|
|
|
|
if(outputArea.end.scan) {
|
|
|
|
[encoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4 instanceCount:outputArea.end.scan];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-08-04 23:44:56 +00:00
|
|
|
|
2020-08-05 01:49:01 +00:00
|
|
|
// Complete encoding.
|
2020-08-04 23:44:56 +00:00
|
|
|
[encoder endEncoding];
|
|
|
|
|
2020-08-09 21:59:52 +00:00
|
|
|
// Register the drawable's presentation, finalise and commit.
|
2020-08-04 23:44:56 +00:00
|
|
|
[commandBuffer presentDrawable:view.currentDrawable];
|
|
|
|
[commandBuffer commit];
|
|
|
|
}
|
|
|
|
|
2020-08-09 02:49:02 +00:00
|
|
|
- (Outputs::Display::ScanTarget *)scanTarget {
|
|
|
|
return &_scanTarget;
|
|
|
|
}
|
|
|
|
|
2020-08-04 22:22:14 +00:00
|
|
|
@end
|