0

我需要将颜色值作为数组传递给着色器。现在我将它作为结构 RGBColors 传递并作为结构接收它,它工作正常。

但我想在着色器中将它作为 float3 值接收。但是,一旦我将其更改为 float3,它就会表现得很奇怪并且开始闪烁,它并没有给我正确的颜色。

这是我用来设置片段缓冲区的代码,

func setFragmentBuffer(_ values: [Float], at index: Int) {
     let bufferValues = values
     let datasize = 16 * values.count / 3

     let colorBuffer = device.makeBuffer(bytes: bufferValues, length: datasize, options: [])
     renderEncoder.setFragmentBuffer(colorBuffer, offset: 0, at: index)
}

这是 RGBColors 的结构

struct RGBColors {
    var r: Float
    var g: Float
    var b: Float

    func floatBuffers() -> [Float] {
        return [r,g,b]
    }
}

从这个结构中,我将创建一个值数组Float并将其设置为片段缓冲区。

我给出的代码let datasize = 16 * values.count / 3值为 16,因为尽管 C++ 中的浮点数据类型是 4 字节,但 simd 中的 float3 是 16 字节。

在着色器中我正在实现该方法

fragment float4
singleShader(RasterizerData in [[stage_in]],
                   texture2d<half> sourceTexture [[ texture(0) ]],
                   const device float3 &rgbColor [[ buffer(1) ]]
{
    constexpr sampler textureSampler (mag_filter::linear,
                                      min_filter::linear);

    // Sample the texture and return the color to colorSample
    const half4 colorSample = sourceTexture.sample (textureSampler, in.textureCoordinate);

    float4 outputColor;


    float red = colorSample.r * rgbColor.r;
    float green = colorSample.g * rgbColor.g;
    float blue =  colorSample.b * rgbColor.b;

    outputColor = float4(red, green,blue, colorSample.a);
    outputColor = float4((outputColor.rgb * param1 + param2) / 4, colorSample.a);
    return outputColor;
}

最后我没有得到正确的输出颜色。

如何将float3 simd数据类型与数据类型匹配swift Float。有人可以建议我吗?

编辑:

我找到了一个关于如何从 float3 创建 MTLBuffer 的解决方案,代码如下:

func setFragmentBuffer(_ values: [float3], at index: Int) {
    var valueBuffer = values

    let bufferCreated = device.makeBuffer(length: MemoryLayout.size(ofValue: valueBuffer[0]) * 2 * valueBuffer.count , options: [])
    let bufferPointer = bufferCreated.contents()

    memcpy(bufferPointer, &valueBuffer, 16 * valueBuffer.count)
    renderEncoder.setFragmentBuffer(bufferCreated, offset: 0, at: index)
}

这段代码工作得很好。但仍然在代码中,let bufferCreated = device.makeBuffer(length: MemoryLayout.size(ofValue: valueBuffer[0]) * 2 * valueBuffer.count , options: [])您可以看到长度应乘以2以使代码正常工作。

为什么要有一个乘数来使它起作用?我不明白这一点。有人可以建议我吗?

4

0 回答 0