1 回答

TA貢獻1725條經驗 獲得超8個贊
因為 API 才是唯一真正的答案。
許多人都同意你的觀點,認為不同的 API 會更好。這是出現新 api(DirectX11/12、Vulkan、Metal、WebGPU)的原因之一
但問題中的描述在技術上不正確
我們通過 JS 在 RAM 上創建一個浮點數組。
WebGL 創建一個代表 GPU 緩沖區的對象(GPU 上未分配任何內容)
將緩沖區上的指針設置為 gl.ARRAY_BUFFER。
現在我們分配一個緩沖區并將數據從 RAM 復制到 GPU 緩沖區。
從 gl.ARRAY_BUFFER 取消綁定緩沖區(但該緩沖區在 GPU 上仍然可用,我們可以多次重新綁定它)。
不需要第 5 步。沒有理由解除緩沖區的綁定。
你可以這樣想。想象一下,您有一個 javascript 函數,可以將圖像繪制到畫布上,但圖像的傳遞方式與示例中的緩沖區相同。這是代碼
class Context {
constructor(canvas) {
this.ctx = canvas.getContext('2d');
}
bindImage(img) {
this.img = img;
}
drawImage(x, y) {
this.ctx.drawImage(this.img, x, y);
}
}
假設您想繪制 3 張圖像
const ctx = new Context(someCanvas);
ctx.bindImage(image1);
ctx.drawImage(0, 0);
ctx.bindImage(image2);
ctx.drawImage(10, 10);
ctx.bindImage(image3);
ctx.drawImage(20, 20);
會工作得很好。沒有理由這樣做
const ctx = new Context(someCanvas);
ctx.bindImage(image1);
ctx.drawImage(0, 0);
ctx.bindImage(null); // not needed
ctx.bindImage(image2);
ctx.drawImage(10, 10);
ctx.bindImage(null); // not needed
ctx.bindImage(image3);
ctx.drawImage(20, 20);
ctx.bindImage(null); // not needed
WebGL 中也是如此。有時會將 null 綁定到某些東西,例如
gl.bindFramebuffer(gl.FRAMEBUFFER, null); // start drawing to the canvas
但大多數時候解除綁定只是程序員的個人喜好,而不是 API 本身所要求的
添加回答
舉報