Ethan
Ethan

Reputation: 313

issues in Apple Metal API "setVertexBuffer:offset:atIndex:"

I am new in Apple Metal, when running the apple sample code "Creating and Sampling Textures", I fond something strange, "Figure 1" is the result in my Macmini 2018 (the GPU is Intel(R) UHD Graphics 630), you can see the quad's left bottom corner is squeezed, the origin source use setVertexBuffer:offset:atIndex: to set vertex buffer, I replace it to setVertexBytes:length:atIndex: and run, the result show as "Figure 2", it seems everything going fine, then I build and run the origin source in another machine(Macbookpro, GPU is GeForce GT 750M), the quad is render correct, result is show as "Figure 2", so does the source missing something or is there some issues in the function setVertexBuffer:offset:atIndex:?

Figure 1

 figure 1

Figure 2

 Figure 2

// Apple's origin source
//
static const AAPLVertex quadVertices[] =
    {
        // Pixel positions, Texture coordinates
        { {  250,  -250 },  { 1.f, 1.f } },
        { { -250,  -250 },  { 0.f, 1.f } },
        { { -250,   250 },  { 0.f, 0.f } },

        { {  250,  -250 },  { 1.f, 1.f } },
        { { -250,   250 },  { 0.f, 0.f } },
        { {  250,   250 },  { 1.f, 0.f } },
    };
...
...
...
//Create a vertex buffer, and initialize it with the quadVertices array
_vertices = [_device newBufferWithBytes:quadVertices
                                length:sizeof(quadVertices)
                               options:MTLResourceOptionCPUCacheModeDefault];
...
...
// set the vertex buffer
[renderEncoder setVertexBuffer:_vertices
                        offset:0
                      atIndex:AAPLVertexInputIndexVertices];

I make the quadVertices as a global array, and replace the "setVertexBuffer:offset:atIndex:" to "setVertexBytes:length:atIndex:"

[renderEncoder setVertexBytes:&quadVertices
                       length:sizeof(quadVertices)
                       atIndex:AAPLVertexInputIndexVertices];

Upvotes: 3

Views: 1143

Answers (1)

Ethan
Ethan

Reputation: 313

Finally, I changed the MTLResourceOptions type to "MTLResourceCPUCacheModeWriteCombined" in the buffer create function, problem solved.

_vertices = [_device newBufferWithBytes:quadVertices
                                 length:sizeof(quadVertices)
             options:MTLResourceCPUCacheModeWriteCombined];

Now the sample works fine in both MacMini 2018(GPU is Intel(R) UHD Graphics 630) and Macbookpro(GPU is GeForce GT 750M).

Upvotes: 2

Related Questions