Orthographic projectify the vertexes
This commit is contained in:
parent
6dc175c39d
commit
1ed5f0bf6e
2 changed files with 26 additions and 4 deletions
|
@ -19,6 +19,10 @@ public protocol RendererDelegate {
|
||||||
var metalView: MTKView { get }
|
var metalView: MTKView { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private struct RenderParameters {
|
||||||
|
var projection: Matrix4x4
|
||||||
|
}
|
||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
let position: Float2
|
let position: Float2
|
||||||
let textureCoordinate: Float2
|
let textureCoordinate: Float2
|
||||||
|
@ -52,6 +56,8 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
private var pixelPipeline: MTLRenderPipelineState?
|
private var pixelPipeline: MTLRenderPipelineState?
|
||||||
private var marchingSquaresPipeline: MTLRenderPipelineState?
|
private var marchingSquaresPipeline: MTLRenderPipelineState?
|
||||||
|
|
||||||
|
private var parametersBuffer: MTLBuffer?
|
||||||
|
|
||||||
override public init() {
|
override public init() {
|
||||||
guard let device = MTLCreateSystemDefaultDevice() else {
|
guard let device = MTLCreateSystemDefaultDevice() else {
|
||||||
fatalError("Unable to create Metal system device")
|
fatalError("Unable to create Metal system device")
|
||||||
|
@ -63,6 +69,9 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
self.device = device
|
self.device = device
|
||||||
commandQueue = queue
|
commandQueue = queue
|
||||||
|
|
||||||
|
let parametersLength = MemoryLayout<RenderParameters>.size
|
||||||
|
parametersBuffer = device.makeBuffer(length: parametersLength, options: .storageModeShared)
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,6 +152,13 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
|
|
||||||
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
|
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
|
||||||
delegate?.renderSize = Size(size: size)
|
delegate?.renderSize = Size(size: size)
|
||||||
|
|
||||||
|
if let buffer = parametersBuffer {
|
||||||
|
let aspectRatio = Float(size.width / size.height)
|
||||||
|
let projectionMatrix = Matrix4x4.orthographicProjection(top: 1.0, left: -aspectRatio, bottom: -1.0, right: aspectRatio, near: 0.0, far: 1.0)
|
||||||
|
let params = RenderParameters(projection: projectionMatrix)
|
||||||
|
memcpy(buffer.contents(), [params], MemoryLayout<RenderParameters>.size)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func draw(in view: MTKView) {
|
public func draw(in view: MTKView) {
|
||||||
|
@ -172,9 +188,9 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
if let pipeline = pixelPipeline,
|
if let pipeline = pixelPipeline,
|
||||||
let encoder = buffer.makeRenderCommandEncoder(descriptor: renderPass) {
|
let encoder = buffer.makeRenderCommandEncoder(descriptor: renderPass) {
|
||||||
encoder.label = "Pixel Render"
|
encoder.label = "Pixel Render"
|
||||||
encoder.setViewport(MTLViewport(originX: 0.0, originY: 0.0, width: Double(view.drawableSize.width), height: Double(view.drawableSize.height), znear: -1.0, zfar: 1.0))
|
|
||||||
encoder.setRenderPipelineState(pipeline)
|
encoder.setRenderPipelineState(pipeline)
|
||||||
encoder.setVertexBytes(points, length: points.count * MemoryLayout<Vertex>.stride, index: 0)
|
encoder.setVertexBytes(points, length: points.count * MemoryLayout<Vertex>.stride, index: 0)
|
||||||
|
encoder.setVertexBuffer(parametersBuffer, offset: 0, index: 1)
|
||||||
encoder.setFragmentBuffer(field.parametersBuffer, offset: 0, index: 0)
|
encoder.setFragmentBuffer(field.parametersBuffer, offset: 0, index: 0)
|
||||||
encoder.setFragmentBuffer(field.ballBuffer, offset: 0, index: 1)
|
encoder.setFragmentBuffer(field.ballBuffer, offset: 0, index: 1)
|
||||||
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
|
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
|
||||||
|
@ -191,6 +207,7 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
encoder.label = "Marching Squares Render"
|
encoder.label = "Marching Squares Render"
|
||||||
encoder.setRenderPipelineState(pipeline)
|
encoder.setRenderPipelineState(pipeline)
|
||||||
encoder.setVertexBytes(points, length: points.count * MemoryLayout<Vertex>.stride, index: 0)
|
encoder.setVertexBytes(points, length: points.count * MemoryLayout<Vertex>.stride, index: 0)
|
||||||
|
encoder.setVertexBuffer(parametersBuffer, offset: 0, index: 1)
|
||||||
encoder.setTriangleFillMode(.lines)
|
encoder.setTriangleFillMode(.lines)
|
||||||
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
|
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
|
||||||
encoder.endEncoding()
|
encoder.endEncoding()
|
||||||
|
|
|
@ -43,17 +43,22 @@ typedef struct {
|
||||||
float3x3 colorTransform;
|
float3x3 colorTransform;
|
||||||
} Parameters;
|
} Parameters;
|
||||||
|
|
||||||
|
struct RenderParameters {
|
||||||
|
float4x4 projection;
|
||||||
|
};
|
||||||
|
|
||||||
typedef float3 Ball;
|
typedef float3 Ball;
|
||||||
|
|
||||||
#pragma mark - Vertex
|
#pragma mark - Vertex
|
||||||
|
|
||||||
vertex RasterizerData
|
vertex RasterizerData
|
||||||
passthroughVertexShader(uint vid [[vertex_id]],
|
passthroughVertexShader(uint vid [[vertex_id]],
|
||||||
constant Vertex* vertexes [[buffer(0)]])
|
constant Vertex* vertexes [[buffer(0)]],
|
||||||
|
constant RenderParameters &renderParameters [[buffer(1)]])
|
||||||
{
|
{
|
||||||
RasterizerData out;
|
RasterizerData out;
|
||||||
Vertex v = vertexes[vid];
|
Vertex v = vertexes[vid];
|
||||||
out.position = float4(v.position.xy, 0.0, 1.0);
|
out.position = renderParameters.projection * float4(v.position.xy, 0.0, 1.0);
|
||||||
out.textureCoordinate = v.textureCoordinate;
|
out.textureCoordinate = v.textureCoordinate;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue