issues in Apple Metal API “setVertexBuffer:offset:atIndex:”

风流意气都作罢 提交于 2020-01-24 22:00:46

问题


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 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];

回答1:


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).



来源:https://stackoverflow.com/questions/58767565/issues-in-apple-metal-api-setvertexbufferoffsetatindex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!