Macos 在金属中混合图像
我一直在努力学习金属,在尝试将两幅图像混合在一起时,我陷入了困境 我有两个带纹理的四边形,其中一个的不透明度设置为0.5,它被缩放到另一个大小的75% 四边形是: 及 MTKView被清除为红色。当我尝试混合它们时,结果是: 我期待的是: 对于我的管道设置,我正在使用:Macos 在金属中混合图像,macos,metal,Macos,Metal,我一直在努力学习金属,在尝试将两幅图像混合在一起时,我陷入了困境 我有两个带纹理的四边形,其中一个的不透明度设置为0.5,它被缩放到另一个大小的75% 四边形是: 及 MTKView被清除为红色。当我尝试混合它们时,结果是: 我期待的是: 对于我的管道设置,我正在使用: descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm descriptor.colorAttachments[0].isBlendingEnabled = tr
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
descriptor.colorAttachments[0].isBlendingEnabled = true
descriptor.colorAttachments[0].rgbBlendOperation = .add
descriptor.colorAttachments[0].alphaBlendOperation = .add
descriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
descriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
descriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
descriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
金属着色器功能包括:
vertex VertexOut vertex_shader(const VertexIn vertex_in [[ stage_in ]], constant ModelMatrix &matrix [[ buffer(1) ]], constant const UniformsStruct &uniforms [[ buffer(2) ]]) {
VertexOut vertex_out;
vertex_out.position = matrix.mvpMatrix * vertex_in.position;
vertex_out.colour = vertex_in.colour;
vertex_out.textureCoordinates = vertex_in.textureCoordinates;
vertex_out.opacity = uniforms.opacity;
return vertex_out;
}
fragment half4 masked_textured_fragment_shader(VertexOut vertex_from_vertex_shader [[ stage_in ]], sampler sampler2d [[ sampler(0) ]], texture2d<float> mask [[ texture(1) ]], texture2d<float> texture [[ texture(0) ]]) {
float4 keyPixel = mask.sample(sampler2d, vertex_from_vertex_shader.textureCoordinates);
float4 colour = texture.sample(sampler2d, vertex_from_vertex_shader.textureCoordinates);
return half4(colour.r * keyPixel.r, colour.g * keyPixel.g, colour.b * keyPixel.b, vertex_from_vertex_shader.opacity);
}
VertexOut vertex_着色器(const VertexIn vertex_in[[stage_in]]、常数ModelMatrix&matrix[[buffer(1)]、常数const UniformsStruct&uniforms[[buffer(2)]){
顶点输出顶点输出;
vertex_out.position=矩阵.MVP矩阵*顶点_in.position;
顶点颜色=顶点颜色;
顶点_out.textureCoordinates=顶点_in.textureCoordinates;
顶点_out.opacity=一致性.opacity;
返回顶点u;
}
fragment half4遮罩_纹理_fragment_着色器(来自_vertex_着色器[[stage_in]],取样器取样器2D[[sampler(0)]],纹理2D遮罩[[texture(1)]],纹理2D纹理[[texture(0)]){
float4 keyPixel=mask.sample(采样器2D,来自顶点着色器的顶点顶点纹理坐标);
float4 color=纹理.sample(采样器2D,来自顶点着色器.纹理坐标的顶点);
返回half4(color.r*keyPixel.r,color.g*keyPixel.g,color.b*keyPixel.b,来自_vertex_着色器的顶点_。不透明度);
}
我目前最好的猜测是管道没有设置正确的选项,但更改它们不会使两个四边形混合,但会产生一些有趣的效果 为第二个四元组设置正确的管道状态是实现混合所必须做的唯一事情-不必在fragment函数中进行任何计算 尝试设置一个简单的管道,不为后四边形设置任何混合。 然后设置管道,就像前面的四元结构一样 渲染两个四元体时,切换管道状态,以便后四元体渲染时不进行混合,前四元体渲染时进行混合 为了得到上述结果,这是我对两个四边形的片段函数:
fragment float4 fragment_main (VertexOut in [[ stage_in ]],
texture2d<float> texture [[ texture(0) ]]) {
constexpr sampler sampler2d;
float4 color = texture.sample(sampler2d, in.textureCoordinates);
color.a = 0.5; // set opacity. Ignored if blending is not enabled
return color;
}
fragment float4 fragment_main(顶点输出在[[stage_in]],
纹理2D纹理[[纹理(0)]){
constexpr取样器取样器2D;
float4颜色=纹理采样(采样2D,英寸纹理坐标);
color.a=0.5;//设置不透明度。如果未启用混合,则忽略
返回颜色;
}
这是“固定功能”混合-通过使用两种不同的管道状态将GPU的状态设置为混合
你可以在