绘制 MTLBuffer 的一部分?

Draw portion of MTLBuffer?

我正在使用此调用从缓冲区渲染点片段:

renderEncoder.drawPrimitives(type: .point,
                             vertexStart: 0,
                             vertexCount: 1,
                             instanceCount: emitter.currentParticles)

emitter.currentParticles 是缓冲区中的粒子总数。是否可以以某种方式仅绘制缓冲区的一部分?

我已经试过了,但是它绘制了缓冲区的前半部分:

renderEncoder.drawPrimitives(type: .point,
                             vertexStart: emitter.currentParticles / 2,
                             vertexCount: 1,
                             instanceCount: emitter.currentParticles / 2)

其实好像vertexStart没有作用。我似乎可以将它设置为任何值,但它仍然从 0 开始。

编辑:

管道配置:

private func buildParticlePipelineStates() {
    do {
        guard let library = Renderer.device.makeDefaultLibrary(),
        let function = library.makeFunction(name: "compute") else { return }

        // particle update pipeline state
        particlesPipelineState = try Renderer.device.makeComputePipelineState(function: function)

        // render pipeline state
        let vertexFunction = library.makeFunction(name: "vertex_particle")
        let fragmentFunction = library.makeFunction(name: "fragment_particle")
        let descriptor = MTLRenderPipelineDescriptor()
        descriptor.vertexFunction = vertexFunction
        descriptor.fragmentFunction = fragmentFunction

        descriptor.colorAttachments[0].pixelFormat = renderPixelFormat
        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

        renderPipelineState = try
        Renderer.device.makeRenderPipelineState(descriptor: descriptor)
        renderPipelineState = try Renderer.device.makeRenderPipelineState(descriptor: descriptor)
    } catch let error {
        print(error.localizedDescription)
    }
}

顶点着色器:

struct VertexOut {
    float4 position   [[ position ]];
    float  point_size [[ point_size ]];
    float4 color;
};


vertex VertexOut vertex_particle(constant float2 &size [[buffer(0)]],
                             device Particle *particles [[buffer(1)]],
                             constant float2 &emitterPosition [[ buffer(2) ]],
                             uint instance [[instance_id]])
{
    VertexOut out;

    float2 position = particles[instance].position + emitterPosition;
    out.position.xy = position.xy / size * 2.0 - 1.0;
    out.position.z = 0;
    out.position.w = 1;
    out.point_size = particles[instance].size * particles[instance].scale;
    out.color = particles[instance].color;
    return out;
}

fragment float4 fragment_particle(VertexOut in [[ stage_in ]],
                              texture2d<float> particleTexture [[ texture(0) ]],
                              float2 point [[ point_coord ]]) {
    constexpr sampler default_sampler;

    float4 color = particleTexture.sample(default_sampler, point);

    if ((color.a < 0.01) || (in.color.a < 0.01)) {
        discard_fragment();
    }
    color = float4(in.color.xyz, 0.2 * color.a * in.color.a);
    return color;
}

您没有为顶点着色器使用顶点描述符或 [[stage_in]] 参数。所以,Metal 不是 fetching/gathering 适合你的顶点数据。您只是索引到一个缓冲区,该缓冲区已经按照您想要的格式放置了顶点数据。没关系。有关顶点描述符的更多信息,请参阅我的回答

鉴于此,绘制调用的 vertexStart 参数仅影响具有 [[vertex_id]] 属性的顶点函数的参数值。您的顶点函数没有这样的参数,更不用说使用它了。相反,它使用 [[instance_id]] 参数来索引顶点数据缓冲区。您可以阅读我的另一个回答 以快速了解绘制调用以及它们如何导致调用您的顶点着色器函数。

您可以通过多种方式更改内容以仅绘制一半的点。您可以将您使用的绘图调用更改为:

renderEncoder.drawPrimitives(type: .point,
                             vertexStart: 0,
                             vertexCount: 1,
                             instanceCount: emitter.currentParticles / 2,
                             baseInstance: emitter.currentParticles / 2)

这不需要对顶点着色器进行任何更改。它只是更改馈送到 instance 参数的值的范围。但是,由于这似乎并不是真正的实例化,我建议您更改着色器和绘图调用。对于着色器,将 instance 参数重命名为 vertexvid,并将其属性从 [[instance_id]] 更改为 [[vertex_id]]。然后,将绘图调用更改为:

renderEncoder.drawPrimitives(type: .point,
                             vertexStart: emitter.currentParticles / 2,
                             vertexCount: emitter.currentParticles / 2)

事实上,在这种情况下它们的行为方式基本相同,但后者更好地代表了您正在做的事情(并且绘制调用更简单,这很好)。