我的问题有两个部分:
第一节
我有一个要求,我必须传递一个包含 3 个颜色 (RGB) 值的结构,这些值从 0 到 1 不等,但是当我测试对这些值进行硬编码的代码时,我收到的任何值都是不同的。
这是我的片段着色器方法
struct RGBColors {
half red;
half green;
half blue;
};
fragment float4
samplingShader(RasterizerData in [[stage_in]],
texture2d<half> colorTexture [[ texture(0) ]],
const device struct RGBColors *color [[ buffer(0) ]]
)
{
constexpr sampler textureSampler (mag_filter::linear,
min_filter::linear,
s_address::repeat,
t_address::repeat,
r_address::repeat);
const half4 colorSample = colorTexture.sample (textureSampler, in.textureCoordinate);
float4 outputColor = float4(0,0,0,0);
half red = color->red;
half blue = color->blue;
half green = color->green;
outputColor = float4(colorSample.r * red, colorSample.g * green, colorSample.b * blue, 0);
return outputColor;
}
我的快速结构看起来像这样,
struct RGBColors {
var r: Float
var g: Float
var b: Float
func floatBuffers() -> [Float] {
return [r,g,b]
}
}
我像这样将缓冲区传递给片段,
let colors = color.floatBuffers()
let colorBuffer = device.makeBuffer(bytes: colors, length: 16, options: [])
renderEncoder.setFragmentBuffer(colorBuffer, offset: 0, at: 0)
但是,如果我像这样将参数颜色更改const device struct RGBColors *color [[ buffer(0) ]]
为 float3constant float3 *color [[ buffer(0) ]]
并通过rgb
值访问它可以正常工作。
第二节
正如您在我的代码中看到的那样
let colorBuffer = device.makeBuffer(bytes: colors, length: 16, options: [])
,长度为 16,但如果我将其更改为
`MemoryLayout.size(ofValue: colors[0]) * colors.count`
它崩溃了,说
`failed assertion `(length - offset)(12) must be >= 16 at buffer binding at index 0 for color[0].'`
我无法弄清楚发生了什么。有人可以建议我。
谢谢。