GPURenderPassEncoder: phương thức setVertexBuffer()

Khả dụng hạn chế

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

Phương thức setVertexBuffer() của giao diện GPURenderPassEncoder đặt hoặc bỏ đặt GPUBuffer hiện tại cho vị trí đã cho sẽ cung cấp dữ liệu đỉnh cho các lệnh vẽ tiếp theo.

Cú pháp

js
setVertexBuffer(slot, buffer, offset, size)

Tham số

slot

Một số tham chiếu vị trí bộ đệm đỉnh để đặt bộ đệm đỉnh cho.

buffer

Một GPUBuffer biểu diễn bộ đệm chứa dữ liệu đỉnh để sử dụng cho các lệnh vẽ tiếp theo, hoặc null, trong trường hợp đó bất kỳ bộ đệm đã đặt trước đó trong vị trí đã cho sẽ bị bỏ đặt.

offset Optional

Một số biểu diễn độ lệch, tính bằng byte, vào buffer nơi dữ liệu đỉnh bắt đầu. Nếu bỏ qua, offset mặc định là 0.

size Optional

Một số biểu diễn kích thước, tính bằng byte, của dữ liệu đỉnh chứa trong buffer. Nếu bỏ qua, size mặc định là GPUBuffer.size của buffer trừ offset.

Giá trị trả về

Không có (Undefined).

Xác thực

Các tiêu chí sau phải được đáp ứng khi gọi setVertexBuffer(), nếu không GPUValidationError sẽ được tạo ra và GPURenderPassEncoder sẽ không hợp lệ:

  • GPUBuffer.usage của buffer chứa cờ GPUBufferUsage.VERTEX.
  • slot nhỏ hơn giới hạn maxVertexBuffers limit của GPUDevice.
  • offset + size nhỏ hơn hoặc bằng GPUBuffer.size của buffer.
  • offset là bội số của 4.

Ví dụ

Đặt bộ đệm đỉnh

Trong demo kết xuất cơ bản của chúng tôi, một số lệnh được ghi lại thông qua GPUCommandEncoder. Hầu hết các lệnh này xuất phát từ GPURenderPassEncoder được tạo thông qua GPUCommandEncoder.beginRenderPass(). setVertexBuffer() được sử dụng phù hợp để đặt nguồn dữ liệu đỉnh.

js
// …

const renderPipeline = device.createRenderPipeline(pipelineDescriptor);

// Create GPUCommandEncoder to issue commands to the GPU
// Note: render pass descriptor, command encoder, etc. are destroyed after use, fresh one needed for each frame.
const commandEncoder = device.createCommandEncoder();

// Create GPURenderPassDescriptor to tell WebGPU which texture to draw into, then initiate render pass
const renderPassDescriptor = {
  colorAttachments: [
    {
      clearValue: clearColor,
      loadOp: "clear",
      storeOp: "store",
      view: context.getCurrentTexture().createView(),
    },
  ],
};

const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);

// Draw the triangle
passEncoder.setPipeline(renderPipeline);
passEncoder.setVertexBuffer(0, vertexBuffer);
passEncoder.draw(3);

// End the render pass
passEncoder.end();

// End frame by passing array of command buffers to command queue for execution
device.queue.submit([commandEncoder.finish()]);

// …

Bỏ đặt bộ đệm đỉnh

js
// Set vertex buffer in slot 0
passEncoder.setVertexBuffer(0, vertexBuffer);

// Later, unset vertex buffer in slot 0
passEncoder.setVertexBuffer(0, null);

Thông số kỹ thuật

Thông số kỹ thuật
WebGPU
# dom-gpurendercommandsmixin-setvertexbuffer

Tương thích trình duyệt

Xem thêm