refactor: Add vertex buffer struct to ShaderTypes and remove global

This commit is contained in:
Stuart Carnie 2018-09-02 17:53:18 -07:00
parent c41f331e2a
commit 9d22aacaa7
2 changed files with 20 additions and 17 deletions

View File

@ -55,6 +55,12 @@ typedef struct
vector_float2 texCoord METAL_ATTRIBUTE(VertexAttributeTexcoord);
} Vertex;
typedef struct
{
vector_float4 position;
vector_float2 texCoord;
} VertexSlang;
typedef struct
{
vector_float4 position METAL_POSITION;

View File

@ -546,6 +546,7 @@ typedef struct MTLALIGN(16)
Context *_context;
id<MTLTexture> _texture; // final render texture
Vertex _v[4];
VertexSlang _vertex[4];
CGSize _size; // size of view in pixels
CGRect _frame;
NSUInteger _bpp;
@ -587,6 +588,15 @@ typedef struct MTLALIGN(16)
self.size = d.size;
self.frame = CGRectMake(0, 0, 1, 1);
resize_render_targets = YES;
// init slang vertex buffer
VertexSlang v[4] = {
{simd_make_float4(0, 1, 0, 1), simd_make_float2(0, 1)},
{simd_make_float4(1, 1, 0, 1), simd_make_float2(1, 1)},
{simd_make_float4(0, 0, 0, 1), simd_make_float2(0, 0)},
{simd_make_float4(1, 0, 0, 1), simd_make_float2(1, 0)},
};
memcpy(_vertex, v, sizeof(_vertex));
}
return self;
}
@ -816,19 +826,6 @@ typedef struct MTLALIGN(16)
init_history = NO;
}
typedef struct vertex
{
simd_float4 pos;
simd_float2 tex;
} vertex_t;
static vertex_t vertex_bytes[] = {
{{0, 1, 0, 1}, {0, 1}},
{{1, 1, 0, 1}, {1, 1}},
{{0, 0, 0, 1}, {0, 0}},
{{1, 0, 0, 1}, {1, 0}},
};
- (void)drawWithEncoder:(id<MTLRenderCommandEncoder>)rce
{
if (_texture)
@ -942,7 +939,7 @@ static vertex_t vertex_bytes[] = {
[rce setFragmentTextures:textures withRange:NSMakeRange(0, SLANG_NUM_BINDINGS)];
[rce setFragmentSamplerStates:samplers withRange:NSMakeRange(0, SLANG_NUM_BINDINGS)];
[rce setVertexBytes:vertex_bytes length:sizeof(vertex_bytes) atIndex:4];
[rce setVertexBytes:_vertex length:sizeof(_vertex) atIndex:4];
[rce drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
if (!backBuffer)
@ -1162,13 +1159,13 @@ static vertex_t vertex_bytes[] = {
@try
{
MTLVertexDescriptor *vd = [MTLVertexDescriptor new];
vd.attributes[0].offset = offsetof(vertex_t, pos);
vd.attributes[0].offset = offsetof(VertexSlang, position);
vd.attributes[0].format = MTLVertexFormatFloat4;
vd.attributes[0].bufferIndex = 4;
vd.attributes[1].offset = offsetof(vertex_t, tex);
vd.attributes[1].offset = offsetof(VertexSlang, texCoord);
vd.attributes[1].format = MTLVertexFormatFloat2;
vd.attributes[1].bufferIndex = 4;
vd.layouts[4].stride = sizeof(vertex_t);
vd.layouts[4].stride = sizeof(VertexSlang);
vd.layouts[4].stepFunction = MTLVertexStepFunctionPerVertex;
MTLRenderPipelineDescriptor *psd = [MTLRenderPipelineDescriptor new];