1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-05 10:28:58 +00:00

Switches to interleaved vertex data.

This more closely relates to what I actually want to do.
This commit is contained in:
Thomas Harte 2020-08-05 17:27:43 -04:00
parent 5c836604c0
commit 7551782a25
3 changed files with 48 additions and 28 deletions

View File

@ -1010,7 +1010,7 @@
4B228CD624D773CA0077EF25 /* CSScanTarget.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CSScanTarget.h; sourceTree = "<group>"; };
4B228CD724DA12C50077EF25 /* CSScanTargetView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSScanTargetView.h; sourceTree = "<group>"; };
4B228CD824DA12C60077EF25 /* CSScanTargetView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CSScanTargetView.m; sourceTree = "<group>"; };
4B228CDA24DA41880077EF25 /* ScanTarget.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; name = ScanTarget.metal; path = "Clock Signal/ScanTarget/ScanTarget.metal"; sourceTree = "<group>"; };
4B228CDA24DA41880077EF25 /* ScanTarget.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; name = ScanTarget.metal; path = "Clock Signal/ScanTarget/ScanTarget.metal"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.metal; };
4B24095A1C45DF85004DA684 /* Stepper.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Stepper.hpp; sourceTree = "<group>"; };
4B2530F3244E6773007980BF /* fm.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = fm.json; sourceTree = "<group>"; };
4B2A332C1DB86821002876E3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = "Clock Signal/Base.lproj/OricOptions.xib"; sourceTree = SOURCE_ROOT; };
@ -5167,8 +5167,8 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_PARAMETER = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
@ -5218,7 +5218,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_PARAMETER = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";

View File

@ -18,8 +18,7 @@
// much of the inspiration, albeit that I'm proceeding via MKLView.
id<MTLFunction> _vertexShader;
id<MTLFunction> _fragmentShader;
id<MTLBuffer> _positionBuffer;
id<MTLBuffer> _colourBuffer;
id<MTLBuffer> _verticesBuffer;
id<MTLRenderPipelineState> _gouraudPipeline;
}
@ -29,18 +28,32 @@
_commandQueue = [view.device newCommandQueue];
// Generate some static buffers. AS A TEST.
constexpr float positions[] = {
0.0f, 0.5f, 0.0f, 1.0f,
constexpr float vertices[] = {
0.0f, 0.5f, 0.0f, 1.0f, // Position.
1.0f, 0.0f, 0.0f, 1.0f, // Colour.
-0.5f, -0.5f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};
constexpr float colours[] = {
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};
_positionBuffer = [view.device newBufferWithBytes:positions length:sizeof(positions) options:MTLResourceOptionCPUCacheModeDefault];
_colourBuffer = [view.device newBufferWithBytes:colours length:sizeof(colours) options:MTLResourceOptionCPUCacheModeDefault];
_verticesBuffer = [view.device newBufferWithBytes:vertices length:sizeof(vertices) options:MTLResourceOptionCPUCacheModeDefault];
MTLVertexDescriptor *vertexDescriptor = [[MTLVertexDescriptor alloc] init];
// Position.
vertexDescriptor.attributes[0].bufferIndex = 0;
vertexDescriptor.attributes[0].offset = 0;
vertexDescriptor.attributes[0].format = MTLVertexFormatFloat4;
// Colour.
vertexDescriptor.attributes[1].bufferIndex = 0;
vertexDescriptor.attributes[1].offset = sizeof(float)*4;
vertexDescriptor.attributes[1].format = MTLVertexFormatFloat4;
// Total vertex size.
vertexDescriptor.layouts[0].stride = sizeof(float) * 8;
// Generate TEST pipeline.
id<MTLLibrary> library = [view.device newDefaultLibrary];
@ -48,6 +61,7 @@
pipelineDescriptor.vertexFunction = [library newFunctionWithName:@"vertex_main"];
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"fragment_main"];
pipelineDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat;
pipelineDescriptor.vertexDescriptor = vertexDescriptor;
_gouraudPipeline = [view.device newRenderPipelineStateWithDescriptor:pipelineDescriptor error:nil];
}
return self;
@ -78,8 +92,7 @@
// Drawing. Just the test triangle, as described above.
[encoder setRenderPipelineState:_gouraudPipeline];
[encoder setVertexBuffer:_positionBuffer offset:0 atIndex:0];
[encoder setVertexBuffer:_colourBuffer offset:0 atIndex:1];
[encoder setVertexBuffer:_verticesBuffer offset:0 atIndex:0];
[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3 instanceCount:1];
// Complete encoding.

View File

@ -9,20 +9,27 @@
#include <metal_stdlib>
using namespace metal;
struct ColoredVertex {
float4 position [[position]];
float4 color;
// These two structs are the same, but defined separately as an artefact
// of my learning process, and the fact that they soon won't be.
struct InputVertex {
float4 position [[attribute(0)]];
float4 colour [[attribute(1)]];
};
vertex ColoredVertex vertex_main( constant float4 *position [[buffer(0)]],
constant float4 *color [[buffer(1)]],
struct ColouredVertex {
float4 position [[position]];
float4 colour;
};
vertex ColouredVertex vertex_main( device const InputVertex *vertices [[buffer(0)]],
uint vid [[vertex_id]]) {
ColoredVertex vert;
vert.position = position[vid];
vert.color = color[vid];
return vert;
ColouredVertex output;
output.position = vertices[vid].position;
output.colour = vertices[vid].colour;
return output;
}
fragment float4 fragment_main(ColoredVertex vert [[stage_in]]) {
return vert.color;
fragment float4 fragment_main(ColouredVertex vert [[stage_in]]) {
return vert.colour;
}