ios - Metal 顶点着色器绘制纹理的点

标签 ios metal vertex-shader opengl-es-3.0

我想执行 Metal(或 OpenGLES 3.0)着色器,通过混合绘制 Points 图元。为此,我需要将纹理的所有像素坐标作为顶点传递给顶点着色器,顶点着色器计算要传递给片段着色器的顶点的位置。片段着色器仅输出启用混合的点的颜色。我的问题是,如果有一个有效的方法是将顶点坐标传递给顶点着色器,因为 1920x1080 图像的顶点太多,并且需要在一秒钟内完成 30 次?就像我们在计算着色器中使用 dispatchThreadgroups 命令所做的那样,除了计算着色器无法在启用混合的情况下绘制几何体。

编辑:这就是我所做的 -

  let vertexFunctionRed = library!.makeFunction(name: "vertexShaderHistogramBlenderRed")

    let fragmentFunctionAccumulator = library!.makeFunction(name: "fragmentShaderHistogramAccumulator")


    let renderPipelineDescriptorRed = MTLRenderPipelineDescriptor()
    renderPipelineDescriptorRed.vertexFunction = vertexFunctionRed
    renderPipelineDescriptorRed.fragmentFunction = fragmentFunctionAccumulator
    renderPipelineDescriptorRed.colorAttachments[0].pixelFormat = .bgra8Unorm
    renderPipelineDescriptorRed.colorAttachments[0].isBlendingEnabled = true
    renderPipelineDescriptorRed.colorAttachments[0].rgbBlendOperation = .add
    renderPipelineDescriptorRed.colorAttachments[0].sourceRGBBlendFactor = .one
    renderPipelineDescriptorRed.colorAttachments[0].destinationRGBBlendFactor = .one

    do {
        histogramPipelineRed = try device.makeRenderPipelineState(descriptor: renderPipelineDescriptorRed)
    } catch {
        print("Unable to compile render pipeline state Histogram Red!")
        return
    }

绘图代码:

  let commandBuffer = commandQueue?.makeCommandBuffer()
        let renderEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: renderPassDescriptor!)
        renderEncoder?.setRenderPipelineState(histogramPipelineRed!)
        renderEncoder?.setVertexTexture(metalTexture, index: 0)
        renderEncoder?.drawPrimitives(type: .point, vertexStart: 0, vertexCount: 1, instanceCount: metalTexture!.width*metalTexture!.height)
  renderEncoder?.drawPrimitives(type: .point, vertexStart: 0, vertexCount: metalTexture!.width*metalTexture!.height, instanceCount: 1)

和着色器:

  vertex MappedVertex vertexShaderHistogramBlenderRed (texture2d<float, access::sample> inputTexture [[ texture(0) ]],
                                                 unsigned int vertexId [[vertex_id]])
  {
        MappedVertex out;

constexpr sampler s(s_address::clamp_to_edge, t_address::clamp_to_edge, min_filter::linear, mag_filter::linear, coord::pixel);

ushort width = inputTexture.get_width();
ushort height = inputTexture.get_height();

float X = (vertexId % width)/(1.0*width);
float Y = (vertexId/width)/(1.0*height);

 int red = inputTexture.sample(s, float2(X,Y)).r;

 out.position = float4(-1.0 + (red * 0.0078125), 0.0, 0.0, 1.0);
 out.pointSize = 1.0;
 out.colorFactor = half3(1.0, 0.0, 0.0);

 return out;
 }

   fragment half4 fragmentShaderHistogramAccumulator ( MappedVertex in [[ stage_in ]]
                                              )
 {
    half3 colorFactor = in.colorFactor;
    return half4(colorFactor*(1.0/256.0), 1.0); 
}

最佳答案

也许您可以绘制实例化 1920x1080 次的单个点。像这样的东西:

vertex float4 my_func(texture2d<float, access::read> image [[texture(0)]],
                      constant uint &width [[buffer(0)]],
                      uint instance_id [[instance_id]])
{
    // decompose the instance ID to a position
    uint2 pos = uint2(instance_id % width, instance_id / width);
    return float4(image.read(pos).r * 255, 0, 0, 0);
}

关于ios - Metal 顶点着色器绘制纹理的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50504961/

相关文章:

three.js - 来自 uv 偏移的 GLSL webgl lerp 法线

c - 在 VertexShader 中使用 mat4

iphone - 了解OpenGLES iOS中的缓冲区

ios - 何时从 drawRect : to OpenGL/Metal? 切换

ios - Metal 质感比原图暗

ios8 - 使用 Metal 进行 64 位数字运算

ios - 防止 UiNavigationBar 标题被截断?

ios - 挂机静音开关(越狱)

ios - Flutter 无法从 iOS 上的路径加载 File()

javascript - 使用 ThreeJS 将 Vec3 数组传递给顶点着色器