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"
|
|
|
|
|
|
|
|
#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-08 01:19:17 +00:00
|
|
|
};
|
|
|
|
|
2020-08-08 02:03:54 +00:00
|
|
|
constexpr size_t NumBufferedScans = 2048;
|
|
|
|
|
2020-08-08 01:19:17 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
id<MTLRenderPipelineState> _gouraudPipeline;
|
2020-08-08 01:19:17 +00:00
|
|
|
|
|
|
|
// Buffers.
|
|
|
|
id<MTLBuffer> _uniformsBuffer;
|
|
|
|
id<MTLBuffer> _scansBuffer;
|
2020-08-08 02:29:24 +00:00
|
|
|
|
|
|
|
// Current uniforms.
|
|
|
|
Uniforms _uniforms;
|
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-08 01:19:17 +00:00
|
|
|
// Allocate space for uniforms.
|
|
|
|
_uniformsBuffer = [view.device newBufferWithLength:16 options:MTLResourceCPUCacheModeWriteCombined];
|
2020-08-08 02:29:24 +00:00
|
|
|
_uniforms.scale[0] = 1024;
|
|
|
|
_uniforms.scale[1] = 1024;
|
|
|
|
_uniforms.lineWidth = 1.0f / 312.0f;
|
|
|
|
_uniforms.aspectRatioMultiplier = 1.0f;
|
|
|
|
[self setUniforms];
|
2020-08-05 21:27:43 +00:00
|
|
|
|
2020-08-08 02:03:54 +00:00
|
|
|
// Allocate a large buffer for scans.
|
|
|
|
_scansBuffer = [view.device
|
|
|
|
newBufferWithLength:sizeof(Outputs::Display::BufferingScanTarget::Scan)*NumBufferedScans
|
|
|
|
options:MTLResourceCPUCacheModeWriteCombined | MTLResourceStorageModeShared];
|
|
|
|
[self setTestScans];
|
|
|
|
|
2020-08-05 01:49:01 +00:00
|
|
|
// Generate TEST pipeline.
|
|
|
|
id<MTLLibrary> library = [view.device newDefaultLibrary];
|
|
|
|
MTLRenderPipelineDescriptor *pipelineDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
|
2020-08-08 01:19:17 +00:00
|
|
|
pipelineDescriptor.vertexFunction = [library newFunctionWithName:@"scanVertexMain"];
|
|
|
|
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"scanFragmentMain"];
|
2020-08-05 01:49:01 +00:00
|
|
|
pipelineDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat;
|
|
|
|
_gouraudPipeline = [view.device newRenderPipelineStateWithDescriptor:pipelineDescriptor error:nil];
|
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-08 02:29:24 +00:00
|
|
|
- (void)setUniforms {
|
|
|
|
memcpy(_uniformsBuffer.contents, &_uniforms, sizeof(Uniforms));
|
2020-08-08 02:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTestScans {
|
|
|
|
Outputs::Display::BufferingScanTarget::Scan scans[2];
|
|
|
|
scans[0].scan.end_points[0].x = 0;
|
|
|
|
scans[0].scan.end_points[0].y = 0;
|
|
|
|
scans[0].scan.end_points[1].x = 1024;
|
|
|
|
scans[0].scan.end_points[1].y = 256;
|
|
|
|
|
|
|
|
scans[1].scan.end_points[0].x = 0;
|
|
|
|
scans[1].scan.end_points[0].y = 768;
|
|
|
|
scans[1].scan.end_points[1].x = 512;
|
2020-08-08 02:20:01 +00:00
|
|
|
scans[1].scan.end_points[1].y = 512;
|
2020-08-08 02:03:54 +00:00
|
|
|
|
|
|
|
memcpy(_scansBuffer.contents, scans, sizeof(scans));
|
2020-08-08 01:19:17 +00:00
|
|
|
}
|
|
|
|
|
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-08 02:29:24 +00:00
|
|
|
_uniforms.aspectRatioMultiplier = float((4.0 / 3.0) / (size.width / size.height));
|
|
|
|
[self setUniforms];
|
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-05 01:49:01 +00:00
|
|
|
// Generate a command encoder for the view.
|
2020-08-04 23:44:56 +00:00
|
|
|
id <MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
|
|
|
|
MTLRenderPassDescriptor *const descriptor = view.currentRenderPassDescriptor;
|
|
|
|
id <MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];
|
|
|
|
|
2020-08-05 01:49:01 +00:00
|
|
|
// Drawing. Just the test triangle, as described above.
|
|
|
|
[encoder setRenderPipelineState:_gouraudPipeline];
|
2020-08-08 01:19:17 +00:00
|
|
|
|
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-08 02:03:54 +00:00
|
|
|
[encoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4 instanceCount:2];
|
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];
|
|
|
|
|
|
|
|
// "Register the drawable's presentation".
|
|
|
|
[commandBuffer presentDrawable:view.currentDrawable];
|
|
|
|
|
|
|
|
// Finalise and commit.
|
|
|
|
[commandBuffer commit];
|
|
|
|
}
|
|
|
|
|
2020-08-04 22:22:14 +00:00
|
|
|
@end
|