首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >金属defaultLibrary不加载.metal函数

金属defaultLibrary不加载.metal函数
EN

Stack Overflow用户
提问于 2022-11-16 22:06:15
回答 2查看 29关注 0票数 1

我的金属默认库不包含同一个目录的.metal文件中的顶点和着色器函数。

然后是library.makeFunction(名称:..)对于应该分配给pipelineDescriptor vars的顶点函数和着色器函数,返回零。

金属文件和头文件是从苹果示例应用程序"BasicTexturing“(纹理的创建和采样)中复制的。

文件APPLShaders.metal和APPLShaderTypes.h包含由AAPLRenderer.m加载的vertexShader和samplingShader函数。

在示例中,这是非常简单的

代码语言:javascript
复制
 id<MTLLibrary> defaultLibrary = [_device newDefaultLibrary];
    id<MTLFunction> vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"];
    id<MTLFunction> fragmentFunction = [defaultLibrary newFunctionWithName:@"samplingShader"];

我已将这些文件复制到RayWenderlich Swift教程中,并使用了有init的快捷版本来设置库

代码语言:javascript
复制
Renderer.library = device.makeDefaultLibrary()

然后

代码语言:javascript
复制
 let library = Renderer.library
    let importVertexFunction = library?.makeFunction(name: "vertexShader")
    let importShaderFunction = library?.makeFunction(name: "samplingShader")

这很好用!

同样的东西,在我的应用程序与相同的文件复制,它不加载的功能。

我已经在构建设置中检查了compileSources -它列出了金属文件。比较设置中的所有内容,看不到工作应用程序和我的应用程序之间的区别。

我没有看到任何错误消息或日志消息来指示语法或路径问题。

有什么想法吗?

苹果示例代码AAPLShaders.metal

代码语言:javascript
复制
/*
See LICENSE folder for this sample’s licensing information.

Abstract:
Metal shaders used for this sample
*/

#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

// Include header shared between this Metal shader code and C code executing Metal API commands
#import "AAPLShaderTypes.h"

// Vertex shader outputs and per-fragment inputs. Includes clip-space position and vertex outputs
//  interpolated by rasterizer and fed to each fragment generated by clip-space primitives.
typedef struct
{
    // The [[position]] attribute qualifier of this member indicates this value is the clip space
    //   position of the vertex wen this structure is returned from the vertex shader
    float4 clipSpacePosition [[position]];

    // Since this member does not have a special attribute qualifier, the rasterizer will
    //   interpolate its value with values of other vertices making up the triangle and
    //   pass that interpolated value to the fragment shader for each fragment in that triangle;
    float2 textureCoordinate;

} RasterizerData;

// Vertex Function
vertex RasterizerData
vertexShader(uint vertexID [[ vertex_id ]],
             constant AAPLVertex *vertexArray [[ buffer(AAPLVertexInputIndexVertices) ]],
             constant vector_uint2 *viewportSizePointer  [[ buffer(AAPLVertexInputIndexViewportSize) ]])

{

    RasterizerData out;

    // Index into our array of positions to get the current vertex
    //   Our positions are specified in pixel dimensions (i.e. a value of 100 is 100 pixels from
    //   the origin)
    float2 pixelSpacePosition = vertexArray[vertexID].position.xy;

    // Get the size of the drawable so that we can convert to normalized device coordinates,
    float2 viewportSize = float2(*viewportSizePointer);

    // The output position of every vertex shader is in clip space (also known as normalized device
    //   coordinate space, or NDC). A value of (-1.0, -1.0) in clip-space represents the
    //   lower-left corner of the viewport whereas (1.0, 1.0) represents the upper-right corner of
    //   the viewport.

    // In order to convert from positions in pixel space to positions in clip space we divide the
    //   pixel coordinates by half the size of the viewport.
    out.clipSpacePosition.xy = pixelSpacePosition / (viewportSize / 2.0);

    // Set the z component of our clip space position 0 (since we're only rendering in
    //   2-Dimensions for this sample)
    out.clipSpacePosition.z = 0.0;

    // Set the w component to 1.0 since we don't need a perspective divide, which is also not
    //   necessary when rendering in 2-Dimensions
    out.clipSpacePosition.w = 1.0;

    // Pass our input textureCoordinate straight to our output RasterizerData. This value will be
    //   interpolated with the other textureCoordinate values in the vertices that make up the
    //   triangle.
    out.textureCoordinate = vertexArray[vertexID].textureCoordinate;
    
    return out;
}

// Fragment function
fragment float4
samplingShader(RasterizerData in [[stage_in]],
               texture2d<half> colorTexture [[ texture(AAPLTextureIndexBaseColor) ]])
{
    constexpr sampler textureSampler (mag_filter::linear,
                                      min_filter::linear);

    // Sample the texture to obtain a color
    const half4 colorSample = colorTexture.sample(textureSampler, in.textureCoordinate);

    // We return the color of the texture
    return float4(colorSample);
}

Apple示例代码头AAPLShaderTypes.h

代码语言:javascript
复制
/*
See LICENSE folder for this sample’s licensing information.

Abstract:
Header containing types and enum constants shared between Metal shaders and C/ObjC source
*/

#ifndef AAPLShaderTypes_h
#define AAPLShaderTypes_h

#include <simd/simd.h>

// Buffer index values shared between shader and C code to ensure Metal shader buffer inputs match
//   Metal API buffer set calls
typedef enum AAPLVertexInputIndex
{
    AAPLVertexInputIndexVertices     = 0,
    AAPLVertexInputIndexViewportSize = 1,
} AAPLVertexInputIndex;

// Texture index values shared between shader and C code to ensure Metal shader buffer inputs match
//   Metal API texture set calls
typedef enum AAPLTextureIndex
{
    AAPLTextureIndexBaseColor = 0,
} AAPLTextureIndex;

//  This structure defines the layout of each vertex in the array of vertices set as an input to our
//    Metal vertex shader.  Since this header is shared between our .metal shader and C code,
//    we can be sure that the layout of the vertex array in the code matches the layout that
//    our vertex shader expects
typedef struct
{
    // Positions in pixel space (i.e. a value of 100 indicates 100 pixels from the origin/center)
    vector_float2 position;

    // 2D texture coordinate
    vector_float2 textureCoordinate;
} AAPLVertex;

#endif /* AAPLShaderTypes_h */

调试我的库的打印

代码语言:javascript
复制
Printing description of self.library:
(MTLLibrary?) library = (object = 0x00006000004af7b0) {
  object = 0x00006000004af7b0 {
    baseNSObject@0 = {
      isa = CaptureMTLLibrary
    }

从RayWenderlich示例应用程序调试工作库打印新添加的sampleShader和vertexShader以及现有的片段和顶点函数显示在库中。

代码语言:javascript
复制
▿ Optional<MTLLibrary>
  - some : <CaptureMTLLibrary: 0x600000f54210> -> <MTLDebugLibrary: 0x600002204050> -> <_MTLLibrary: 0x600001460280>
    label = <none> 
    device = <MTLSimDevice: 0x15a5069d0>
        name = Apple iOS simulator GPU 
    functionNames: fragment_main vertex_main samplingShader vertexShader
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-17 23:04:47

答案-不将函数加载到金属库的问题是通过删除项目目标的Build的另一个选项中的剩馀-fcikernel标志来解决的。该标志是在测试CoreImageKernel.metal时设置的,如https://developer.apple.com/documentation/coreimage/cikernel/2880194-init中所述,我从应用程序中删除了内核定义文件,但忽略了编译器标志。并且在视觉上比较构建设置时错过了它。

票数 0
EN

Stack Overflow用户

发布于 2022-11-17 05:21:04

你检查过文件的目标成员身份了吗?你的代码没什么好奇怪的,所以请检查目标。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74467816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档