gpt4 book ai didi

glsl - 绑定(bind)多个统一缓冲区对象

转载 作者:行者123 更新时间:2023-12-02 06:50:59 25 4
gpt4 key购买 nike

借助 WebGL 2,我们现在可以使用统一缓冲区对象。

它们看起来是个好主意,不必将通用制服附加到每个单独的程序(例如每个正在渲染的对象都通用的投影和 View 矩阵)。

我创建了一个辅助类,每次我想绑定(bind)一个统一的缓冲区对象时都会调用它。

class UniformBuffer {
constructor(gl, data, boundLocation = 0) {
this.boundLocation = boundLocation;

this.data = new Float32Array(data);

this.buffer = gl.createBuffer();
gl.bindBuffer(gl.UNIFORM_BUFFER, this.buffer);
gl.bufferData(gl.UNIFORM_BUFFER, this.data, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.UNIFORM_BUFFER, null);
gl.bindBufferBase(gl.UNIFORM_BUFFER, this.boundLocation, this.buffer);
}

update(gl, data, offset = 0) {
this.data.set(data, offset);

gl.bindBuffer(gl.UNIFORM_BUFFER, this.buffer);
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, this.data, 0, null);
gl.bindBuffer(gl.UNIFORM_BUFFER, null);
gl.bindBufferBase(gl.UNIFORM_BUFFER, this.boundLocation, this.buffer);
}
};

是否像这样创建统一缓冲区的想法

const perScene = new UniformBuffer(gl, [
...vec4.create(),
...vec4.create(),
], 0); // and bind it to bind location 0?

const perObject = new UniformBuffer(gl, [
...vec4.create(),
], 1); // and bind it to bind location 1?

在我的渲染循环中,然后我通过调用更新“perScene”制服

perScene.update(gl, [
...vec4.fromValues(1, 0, 0, 1),
], 4); // giving an offset to update only the 2nd color.

然后我将查看场景中的所有对象,我的想法是像这样更新 perObject 统一缓冲区

for (let i = 0; i < objects.length; i++) {
perObject.update(gl, [
...vec4.fromValues(0, 0, 1, 1),
]);
}

我在谈论 vec4 只是为了使示例更简单,但其想法是在 perScene 上拥有矩阵(投影和 View ),在 perObject 上拥有(对象和法线矩阵)。

在我的着色器中,我将它们声明为

uniform perScene {
vec4 color1;
vec4 color2;
};


uniform perModel {
vec4 color3;
};

我在这里有一个工作片段

class UniformBuffer {
constructor(gl, data, boundLocation = 0) {
this.boundLocation = boundLocation;

this.data = new Float32Array(data);

this.buffer = gl.createBuffer();
gl.bindBuffer(gl.UNIFORM_BUFFER, this.buffer);
gl.bufferData(gl.UNIFORM_BUFFER, this.data, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.UNIFORM_BUFFER, null);
gl.bindBufferBase(gl.UNIFORM_BUFFER, this.boundLocation, this.buffer);
}

update(gl, data, offset = 0) {
this.data.set(data, offset);

gl.bindBuffer(gl.UNIFORM_BUFFER, this.buffer);
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, this.data, 0, null);
gl.bindBuffer(gl.UNIFORM_BUFFER, null);
gl.bindBufferBase(gl.UNIFORM_BUFFER, this.boundLocation, this.buffer);
}
};

const vertex = `#version 300 es

uniform perScene {
vec4 color1;
vec4 color2;
};

uniform perModel {
vec4 color3;
};

in vec3 a_position;
out vec3 v_color;

void main() {
gl_Position = vec4(a_position, 1.0);
v_color = color1.rgb + color2.rgb; // WORKS
// v_color = color1.rgb + color2.rgb + color3.rgb; // DOESNT WORK
}
`;

const fragment = `#version 300 es
precision highp float;
precision highp int;

in vec3 v_color;
out vec4 outColor;

void main() {
outColor = vec4(v_color, 1.0);
}
`;

const geometry = {
positions: [-0.5, -0.5, 0, -0.5, 0.5, 0, 0.5, -0.5, 0, 0.5, 0.5, 0],
indices: [0, 2, 1, 1, 2, 3],
};

const renderList = [];

// STEP 1 (create canvas)
var canvas = document.getElementById("canvas");
var gl = canvas.getContext("webgl2");
if (!gl) {
console.log('no webgl2 buddy');
}

// STEP 2 (create program)
const v = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(v, vertex);
gl.compileShader(v);

const f = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(f, fragment);
gl.compileShader(f);

const program = gl.createProgram();
gl.attachShader(program, v);
gl.attachShader(program, f);
gl.linkProgram(program);

// STEP 3 (create VAO)
const positionAttributeLocation = gl.getAttribLocation(program, 'a_position');
const colorUniformLocation = gl.getUniformLocation(program, 'color');

const positionsBuffer = gl.createBuffer();
const indicesBuffer = gl.createBuffer();

const vao = gl.createVertexArray();
gl.bindVertexArray(vao);

// position & indices
gl.enableVertexAttribArray(positionAttributeLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, positionsBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geometry.positions), gl.STATIC_DRAW);
gl.vertexAttribPointer(positionAttributeLocation, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, null);

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indicesBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(geometry.indices), gl.STATIC_DRAW);

// STEP 4 (create UBO)

// bound to location 0
const perScene = new UniformBuffer(gl, [
...vec4.create(), // color 1
...vec4.create(), // color 2
], 0);

// bound to location 1 ?
const perModel = new UniformBuffer(gl, [
...vec4.create(), // color 3
], 3);

// STEP 5 (add instances)
for (let i = 0; i < 1; i++) {
renderList.push({
id: i,
vao: vao,
program: program,
color: [0, 1, 1],
});
}

// STEP 6 (draw)
gl.clearColor(0, 0, 0, 0);

gl.enable(gl.DEPTH_TEST);

gl.viewport(0, 0, canvas.width, canvas.height);

perScene.update(gl, [
...vec4.fromValues(1, 0, 0, 1),
...vec4.fromValues(0, 1, 0, 1),
]);

for (let i = 0; i < renderList.length; i++) {
const current = renderList[i];
gl.useProgram(current.program);
gl.bindVertexArray(current.vao);

// update perObject
perModel.update(gl, [
...vec4.fromValues(0, 0, 1, 1),
]);

gl.drawElements(gl.TRIANGLES, geometry.indices.length, gl.UNSIGNED_SHORT, 0);

// unbind
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
}

console.log('compiled!');
canvas {
background-color: black;
}
<canvas id="canvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.3.2/gl-matrix-min.js"></script>

我不应该看到一个白色方 block ,因为所有颜色加起来会产生 vec4(1.0, 1.0, 1.0, 1.0)? (jsfiddle 第 41 行)

我做错了什么?谢谢

最佳答案

所以,你做错的第一件事是你没有调用 gl.getUniformBlockIndex。就像制服一样,您必须查询位置,或者在这种情况下查询每个 block 的索引。

第二件事是 block 统一是间接的一级,你需要调用 gl.uniformBlockBinding(program, uniformBlockIndex, uniformBufferIndex);

uniformBlockIndex 是您从 gl.getUniformBlockIndex 获得的索引。 uniformBufferIndex 类似于一个纹理单元。有 N 个统一缓冲区索引。您可以选择从 0MAX_UNIFORM_BUFFER_BINDINGS - 1 的任何缓冲区索引。

如果您有一个程序使用 block A、B 和另一个使用 A 和 C 的程序,则此间接会有所帮助。在这种情况下, block A 可能在 2 个程序中具有不同的索引,但您让它从同一个 uniformBufferIndex 中提取其值.

请注意,此状态是每个程序状态,因此如果您计划始终为同一个统一 block 使用相同的统一缓冲区索引,则可以在初始化时设置它。

更详细地说明它。你有一个着色器程序。它有状态

var someProgram = {
uniforms: {
projectionMatrix: [1, 0, 0, 0, 0, ... ], // etc
},
uniformBlockIndcies[ // one per uniform block
0,
0,
0,
],
...
}

接下来你有统一的缓冲区索引,它们是全局状态

glState = {
textureUnits: [ ... ],
uniformBuffers: [ null, null, null ..., ],
};

您告诉程序对于每个统一缓冲区 block ,使用 gl.uniformBlockBinding 使用哪个统一缓冲区索引。然后使用 gl.bindBufferBasegl.bindBufferRange 将缓冲区绑定(bind)到该索引。

这非常类似于告诉程序使用哪个纹理单元,然后将纹理绑定(bind)到该单元。当你这样做时,在初始化时间或渲染时间真的取决于你。在我看来,我似乎更有可能在初始化时决定我的 perScene 东西总是在缓冲区索引 0 和 perModel 东西在索引 1 上,因此我可以设置它们的程序部分(调用 gl.uniformBlockBinding) 在初始化时。

class UniformBuffer {
constructor(gl, data, boundLocation = 0) {
this.boundLocation = boundLocation;

this.data = new Float32Array(data);

this.buffer = gl.createBuffer();
gl.bindBuffer(gl.UNIFORM_BUFFER, this.buffer);
gl.bufferData(gl.UNIFORM_BUFFER, this.data, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.UNIFORM_BUFFER, null);
gl.bindBufferBase(gl.UNIFORM_BUFFER, this.boundLocation, this.buffer);
}

update(gl, data, offset = 0) {
this.data.set(data, offset);

gl.bindBuffer(gl.UNIFORM_BUFFER, this.buffer);
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, this.data, 0, null);
gl.bindBuffer(gl.UNIFORM_BUFFER, null);
gl.bindBufferBase(gl.UNIFORM_BUFFER, this.boundLocation, this.buffer);
}
};

const vertex = `#version 300 es

uniform perScene {
vec4 color1;
vec4 color2;
};

uniform perModel {
vec4 color3;
};

in vec3 a_position;
out vec3 v_color;

void main() {
gl_Position = vec4(a_position, 1.0);
v_color = color1.rgb + color2.rgb + color3.rgb;
}
`;

const fragment = `#version 300 es
precision highp float;
precision highp int;

in vec3 v_color;
out vec4 outColor;

void main() {
outColor = vec4(v_color, 1.0);
}
`;

const geometry = {
positions: [-0.5, -0.5, 0, -0.5, 0.5, 0, 0.5, -0.5, 0, 0.5, 0.5, 0],
indices: [0, 2, 1, 1, 2, 3],
};

const renderList = [];

// STEP 1 (create canvas)
var canvas = document.getElementById("canvas");
var gl = canvas.getContext("webgl2");
if (!gl) {
console.log('no webgl2 buddy');
}

// STEP 2 (create program)
const v = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(v, vertex);
gl.compileShader(v);

const f = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(f, fragment);
gl.compileShader(f);

const program = gl.createProgram();
gl.attachShader(program, v);
gl.attachShader(program, f);
gl.linkProgram(program);

// STEP 3 (create VAO)
const positionAttributeLocation = gl.getAttribLocation(program, 'a_position');
const colorUniformLocation = gl.getUniformLocation(program, 'color');

const positionsBuffer = gl.createBuffer();
const indicesBuffer = gl.createBuffer();

const vao = gl.createVertexArray();
gl.bindVertexArray(vao);

// position & indices
gl.enableVertexAttribArray(positionAttributeLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, positionsBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geometry.positions), gl.STATIC_DRAW);
gl.vertexAttribPointer(positionAttributeLocation, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, null);

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indicesBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(geometry.indices), gl.STATIC_DRAW);

// STEP 4 (create UBO)

// bound to location 0
const perScene = new UniformBuffer(gl, [
...vec4.create(), // color 1
...vec4.create(), // color 2
], 0);

// bound to location 1 ?
const perModel = new UniformBuffer(gl, [
...vec4.create(), // color 3
], 1);

gl.uniformBlockBinding(program, gl.getUniformBlockIndex(program, "perScene"), perScene.boundLocation);
gl.uniformBlockBinding(program, gl.getUniformBlockIndex(program, "perModel"), perModel.boundLocation);


// STEP 5 (add instances)
for (let i = 0; i < 1; i++) {
renderList.push({
id: i,
vao: vao,
program: program,
color: [0, 1, 1],
});
}

// STEP 6 (draw)
gl.clearColor(0, 0, 0, 0);

gl.enable(gl.DEPTH_TEST);

gl.viewport(0, 0, canvas.width, canvas.height);

perScene.update(gl, [
...vec4.fromValues(1, 0, 0, 1),
...vec4.fromValues(0, 1, 0, 1),
]);

for (let i = 0; i < renderList.length; i++) {
const current = renderList[i];
gl.useProgram(current.program);
gl.bindVertexArray(current.vao);

// update perObject
perModel.update(gl, [
...vec4.fromValues(0, 0, 1, 1),
]);

gl.drawElements(gl.TRIANGLES, geometry.indices.length, gl.UNSIGNED_SHORT, 0);

// unbind
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
}

console.log('compiled!');
canvas {
background-color: black;
}
<canvas id="canvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.3.2/gl-matrix-min.js"></script>

this example有5个统一的 block 。

  1. 共享矩阵,如 projectionviewviewProjection
  2. 每个模型矩阵,如 worldworldInverseTransform
  3. 每个灯光信息,例如 lightPositionlightColor
  4. 有 2 盏灯,所以第 4 block 类似于第 3 block
  5. 环境颜色、镜面反射等 Material 数据。

我并不是说这是完美的设置。我真的不知道。但是制作一种称为“ Material ”的东西并在多个模型之间共享该 Material 是很常见的,所以这就像一个不同于 perModel block 的 perMaterial block 。共享照明信息也很常见。我不知道理想的设置是什么,只是指出 perSceneperModel 对于相当常见的情况可能还不够。

另一件事,这条线

    // unbind
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

没有意义。 ELEMENT_ARRAY_BUFFER 是 VAO 状态的一部分。

关于glsl - 绑定(bind)多个统一缓冲区对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44629165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com