本文介绍了Apple Metal API中的"setVertexBuffer:offset:atIndex:"问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Apple Metal的新手,运行苹果示例代码"创建和采样纹理",我喜欢一个奇怪的东西,"图1 "是我的 Macmini 2018(GPU是Intel®UHD Graphics 630)的结果 >,您可以看到四边形的左下角受到挤压,原始源使用setVertexBuffer:offset:atIndex:设置顶点缓冲区,我将其替换为setVertexBytes:length:atIndex:并运行,结果显示为"图2 " ,看起来一切都很好,然后我在另一台机器( Macbookpro,GPU是GeForce GT 750M )上构建并运行原始源,四边形呈现正确,结果显示为" Figure 2 ",那么源代码是否丢失了某些内容,或者函数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:?

图1

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

我将quadVertices设置为全局数组,并替换

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

推荐答案

最后,我在缓冲区创建函数中将MTLResourceOptions类型更改为"MTLResourceCPUCacheModeWriteCombined",问题已解决.

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

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

现在该示例在MacMini 2018(GPU为Intel®UHD Graphics 630)和Macbookpro(GPU为GeForce GT 750M)中都可以正常工作.

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

这篇关于Apple Metal API中的"setVertexBuffer:offset:atIndex:"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 03:51