gpt4 book ai didi

android - OpenGL ES : Bad performance when calculating vertex position in vertex shader

转载 作者:行者123 更新时间:2023-11-30 05:01:05 27 4
gpt4 key购买 nike

我是 OpenGL 的初学者,我试图每 5 秒将大量“对象”从一个位置移动到另一个位置。如果我在顶点着色器中计算位置,fps 急剧下降,难道不应该在 GPU 上完成这些类型的计算吗?

这是顶点着色器代码:

#version 300 es
precision highp float;
precision highp int;

layout(location = 0) in vec3 vertexData;
layout(location = 1) in vec3 colourData;
layout(location = 2) in vec3 normalData;
layout(location = 3) in vec3 personPosition;
layout(location = 4) in vec3 oldPersonPosition;
layout(location = 5) in int start;
layout(location = 6) in int duration;

layout(std140, binding = 0) uniform Matrices
{ //base //offset
mat4 projection; // 64 // 0
mat4 view; // 64 // 0 + 64 = 64
int time; // 4 // 64 + 64 = 128
bool shade; // 4 // 128 + 4 = 132 two empty slots after this
vec3 midPoint; // 16 // 128 + 16 = 144
vec3 cameraPos; // 16 // 144 + 16 = 160
// size = 160+16 = 176. Alligned to 16, becomes 176.
};

out vec3 vertexColour;
out vec3 vertexNormal;
out vec3 fragPos;

void main() {
vec3 scalePos;
scalePos.x = vertexData.x * 3.0;
scalePos.y = vertexData.y * 3.0;
scalePos.z = vertexData.z * 3.0;
vertexColour = colourData;
vertexNormal = normalData;

float startFloat = float(start);
float durationFloat = float(duration);
float timeFloat = float(time);

// Wrap around catch to avoid start being close to 1M but time has wrapped around to 0
if (startFloat > timeFloat) {
startFloat = startFloat - 1000000.0;
}

vec3 movePos;
float elapsedTime = timeFloat - startFloat;
if (elapsedTime > durationFloat) {
movePos = personPosition;
} else {
vec3 moveVector = personPosition - oldPersonPosition;
float moveBy = elapsedTime / durationFloat;
movePos = oldPersonPosition + moveVector * moveBy;
}

fragPos = movePos;
gl_Position = projection * view * vec4(scalePos + movePos, 1.0);
}

缓冲区每 5 秒更新一次:

glBindBuffer(GL_ARRAY_BUFFER, this->personPositionsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * this->persons.size() * 3, this->positions, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, this->personOldPositionsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * this->persons.size() * 3, this->oldPositions, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, this->timeStartVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(int) * this->persons.size(), animiationStart, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, this->timeDurationVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(int) * this->persons.size(), animiationDuration, GL_STATIC_DRAW);

我做了一个测试,计算 CPU 上的位置,并在每次绘制调用时更新位置缓冲区,这并没有给我带来性能下降,但感觉根本不对?

void PersonView::animatePositions() {
float duration = 1500;
double currentTime = now_ms();
double elapsedTime = currentTime - animationStartTime;
if (elapsedTime > duration) {
return;
}

for (int i = 0; i < this->persons.size() * 3; i++) {
float moveDistance = this->positions[i] - this->oldPositions[i];
float moveBy = (float)(elapsedTime / duration);
this->moveByPositions[i] = this->oldPositions[i] + moveDistance * moveBy;
}

glBindBuffer(GL_ARRAY_BUFFER, this->personMoveByPositionsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * this->persons.size() * 3, this->moveByPositions, GL_STATIC_DRAW);
}

在具有更好 SOC:s(Snapdragon 835 等)的设备上,帧下降不像在具有中等 SOC:s(Snapdragon 625)的设备上那么剧烈

最佳答案

马上,我可以看到您在顶点着色器中将投影矩阵和 View 矩阵相乘,但没有任何地方可以独立依赖 View 或投影矩阵。

将两个 4x4 矩阵相乘会导致对您绘制的每个顶点进行大量算术计算。就您而言 - 看来您可以完全避免这种情况。

代替您当前的实现 - 尝试在着色器外部乘以 View 和投影矩阵,然后将生成的矩阵绑定(bind)为单个 viewProjection 矩阵:

Old:
gl_Position = projection * view * vec4(scalePos + movePos, 1.0);

New:
gl_Position = projectionView * vec4(scalePos + movePos, 1.0);

这样,proj 和 view 矩阵每帧相乘一次,而不是每个顶点相乘一次。此更改应该会显着提高性能 - 特别是当您有大量顶点时。

一般来说,像这样的算术运算,GPU确实比CPU效率高很多,但是也要考虑计算量。顶点着色器按顶点执行 - 并且应该只计算顶点之间不同的东西。

在 CPU 上执行 1 次计算总是优于在 GPU 上执行 n 次相同的计算(n = 总顶点数)。

关于android - OpenGL ES : Bad performance when calculating vertex position in vertex shader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50444740/

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