<p><code>float3</code> in Metal Shading Language has the same size/alignment as <code>float4</code>, i.e. 16 bytes. I think your original code would have worked using <code>packed_float3</code> (12 bytes) in the vertex shader.</p>

CC BY-SA 4.0
2021-05-26T17:17:52.070Z
2021-05-26T17:17:52.070Z
Noah Witherspoon
<p>I am trying to port my simple app from D3D12 to Metal and I am stuck at rendering a simple triangle.</p>
<p>The window does not show any primitives; only the clear color is visible.</p>
<p>Debugging my application, I noticed, that all primitives are clipped (Performance &gt; Primitives &gt; Primitives Clipped: 100%). I fail to understand why this happens, as these are my coordinates:</p>
<pre class="lang-c prettyprint-override"><code>struct vertex { float x, y, z; };
static const struct vertex vertices[] =
{
    { 0.0f, 0.5f, 0.0f },
    { 0.5f, -0.5f, 0.0f },
    { -0.5f, -0.5f, 0.0f }
};
</code></pre>
<p>What I have tried so far:</p>
<ul>
<li>Explicitly setting the viewport <code>{0, 0, 800, 600, 0, 1}</code>.</li>
<li>Explicitly setting scissors <code>{0, 0, 800, 600}</code>.</li>
<li>Setting the cull mode to <code>MTLCullModeNone</code>.</li>
<li>Setting the fill mode to <code>MTLTriangleFillModeFill</code>.</li>
<li>Changing the winding order.</li>
<li>Altering the vertex input layout; but I am confident, that mine matches the input.</li>
</ul>
<p>Are there any pitfalls of Metal that I am missing? (i.e. setting the sample mask like in D3D12?)</p>
<p>My pipeline for reference:</p>
<pre><code>    MTLRenderPipelineDescriptor* descriptor = [MTLRenderPipelineDescriptor new];
    descriptor.vertexFunction   = desc-&gt;vertex-&gt;handle;
    descriptor.fragmentFunction = desc-&gt;pixel-&gt;handle;
    descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
    
    MTLVertexDescriptor* vs = descriptor.vertexDescriptor;
    vs.attributes[0].format = MTLVertexFormatFloat3;
    vs.attributes[0].offset = 0;
    vs.attributes[0].bufferIndex = 0;
    
    vs.layouts[0].stride        = sizeof(float) * 3;
    vs.layouts[0].stepFunction  = MTLVertexStepFunctionPerVertex;
    vs.layouts[0].stepRate      = 1;
</code></pre>
<p>Note, that I am NOT using <code>MTKView</code>.</p>
<p>The following is my shader used:</p>
<pre><code>struct Out 
{
    float4 pos [[position]];
};

vertex Out vertexShader(
    const device float3* vertices [[buffer(0)]],
    unsigned int idx [[vertex_id]]
) {
    Out out;
    out.pos = float4(vertices[idx], 1.0);
    return out;
}


fragment float4 fragmentShader(Out in [[stage_in]])
{
    // TODO: Write fragment shader
    return float4(1, 0, 0, 1);
}
</code></pre>
<hr />
<p>EDIT:
I made following changes and my triangle is finally being rendered:</p>
<pre class="lang-c prettyprint-override"><code>struct vertex { float x, y, z, w; };
static const struct vertex vertices[] =
{
    { 0.0f, 0.5f, 0.0f, 1.f },
    { 0.5f, -0.5f, 0.0f, 1.f},
    { -0.5f, -0.5f, 0.0f, 1.f }
};

// Vertex Descriptor
    MTLVertexDescriptor* vs = descriptor.vertexDescriptor;
    vs.attributes[0].format = MTLVertexFormatFloat4;
    vs.attributes[0].offset = 0;
    vs.attributes[0].bufferIndex = 0;
    
    vs.layouts[0].stride        = sizeof(float) * 4;
    vs.layouts[0].stepFunction  = MTLVertexStepFunctionPerVertex;
    vs.layouts[0].stepRate      = 1;
</code></pre>
<p>Shader:</p>
<pre><code>struct Out 
{
    float4 pos [[position]];
};

vertex Out vertexShader(
    const device float4* vertices [[buffer(0)]],
    unsigned int idx [[vertex_id]]
) {
    Out out;
    out.pos = vertices[idx];
    return out;
}


fragment float4 fragmentShader(Out in [[stage_in]])
{
    // TODO: Write fragment shader
    return float4(1, 0, 0, 1);
}
</code></pre>
<p>I don't understand why my application fails, when using 3D vectors instead of 4D ones. Any ideas?</p>

<metal>
Metal API clips all primitives?
Julian Kirsch
# Metal API clips all primitives?

(Asked by: username_0 on 2021-05-26)

I am trying to port my simple app from D3D12 to Metal and I am stuck at rendering a simple triangle.

The window does not show any primitives; only the clear color is visible.

Debugging my application, I noticed, that all primitives are clipped (Performance > Primitives > Primitives Clipped: 100%). I fail to understand why this happens, as these are my coordinates:

```c
struct vertex { float x, y, z; };
static const struct vertex vertices[] =
{
    { 0.0f, 0.5f, 0.0f },
    { 0.5f, -0.5f, 0.0f },
    { -0.5f, -0.5f, 0.0f }
};

```

What I have tried so far:

* Explicitly setting the viewport `{0, 0, 800, 600, 0, 1}`.
* Explicitly setting scissors `{0, 0, 800, 600}`.
* Setting the cull mode to `MTLCullModeNone`.
* Setting the fill mode to `MTLTriangleFillModeFill`.
* Changing the winding order.
* Altering the vertex input layout; but I am confident, that mine matches the input.

Are there any pitfalls of Metal that I am missing? (i.e. setting the sample mask like in D3D12?)

My pipeline for reference:

```
    MTLRenderPipelineDescriptor* descriptor = [MTLRenderPipelineDescriptor new];
    descriptor.vertexFunction   = desc->vertex->handle;
    descriptor.fragmentFunction = desc->pixel->handle;
    descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
    
    MTLVertexDescriptor* vs = descriptor.vertexDescriptor;
    vs.attributes[0].format = MTLVertexFormatFloat3;
    vs.attributes[0].offset = 0;
    vs.attributes[0].bufferIndex = 0;
    
    vs.layouts[0].stride        = sizeof(float) * 3;
    vs.layouts[0].stepFunction  = MTLVertexStepFunctionPerVertex;
    vs.layouts[0].stepRate      = 1;

```

Note, that I am NOT using `MTKView`.

The following is my shader used:

```
struct Out 
{
    float4 pos [[position]];
};

vertex Out vertexShader(
    const device float3* vertices [[buffer(0)]],
    unsigned int idx [[vertex_id]]
) {
    Out out;
    out.pos = float4(vertices[idx], 1.0);
    return out;
}

fragment float4 fragmentShader(Out in [[stage_in]])
{
    // TODO: Write fragment shader
    return float4(1, 0, 0, 1);
}

```

---

EDIT:
I made following changes and my triangle is finally being rendered:

```c
struct vertex { float x, y, z, w; };
static const struct vertex vertices[] =
{
    { 0.0f, 0.5f, 0.0f, 1.f },
    { 0.5f, -0.5f, 0.0f, 1.f},
    { -0.5f, -0.5f, 0.0f, 1.f }
};

// Vertex Descriptor
    MTLVertexDescriptor* vs = descriptor.vertexDescriptor;
    vs.attributes[0].format = MTLVertexFormatFloat4;
    vs.attributes[0].offset = 0;
    vs.attributes[0].bufferIndex = 0;
    
    vs.layouts[0].stride        = sizeof(float) * 4;
    vs.layouts[0].stepFunction  = MTLVertexStepFunctionPerVertex;
    vs.layouts[0].stepRate      = 1;

```

Shader:

```
struct Out 
{
    float4 pos [[position]];
};

vertex Out vertexShader(
    const device float4* vertices [[buffer(0)]],
    unsigned int idx [[vertex_id]]
) {
    Out out;
    out.pos = vertices[idx];
    return out;
}

fragment float4 fragmentShader(Out in [[stage_in]])
{
    // TODO: Write fragment shader
    return float4(1, 0, 0, 1);
}

```

I don't understand why my application fails, when using 3D vectors instead of 4D ones. Any ideas?

**Tags:** metal


---

## 1 answers

### Answer by username_1 on 2021-05-26. Score: 2 | Accepted answer

`float3` in Metal Shading Language has the same size/alignment as `float4`, i.e. 16 bytes. I think your original code would have worked using `packed_float3` (12 bytes) in the vertex shader.


---

