gpt4 book ai didi

c++ - 使用VBO,CPU使用率非常高

转载 作者:行者123 更新时间:2023-11-30 18:08:41 25 4
gpt4 key购买 nike

我真的不知道该怎么办了。我让我的应用程序使用 VBO,但我的 CPU 使用率仍然在 70 年代和 80 年代。我的渲染过程是这样的:

设置相机变换如果形状尚未 segmentation ,则对其进行 segmentation 。创建它的VBO如果它有 VBO,请使用它。

您会注意到我也有显示列表,如果不支持 VBO,我可能会使用这些列表。我找到了一个 OpenGL 演示,它在我的 PC 上以 60fps 渲染 32000 个多边形网格,并使用 4% 的 CPU。我使用 vbos 以 60fps 渲染大约 10,000 个多边形,其使用率为 70-80%。

这是我的渲染过程:

        glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

POINT hh = controls.MainGlFrame.GetMousePos();
POINTFLOAT S;
S.x = static_cast<float>(hh.x);
S.y = static_cast<float>(hh.y);
POINTFLOAT t;
t.x = 256;
t.y = 256;
POINT dimensions;
dimensions.x = 512;
dimensions.y = 512;
glDeleteTextures(1,&texName);
texName = functions.CreateGradient(col,t,S,512,512,true);

itt = true;
}
HDC hdc;
PAINTSTRUCT ps;
glEnable(GL_MULTISAMPLE_ARB);
glEnable(GL_BLEND);

glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

hdc = BeginPaint(controls.MainGlContext.mhWnd,&ps);

//start OGL code
glClearColor( 1.0f, 1.0f, 1.0f, 0.0f );
if(!current.isdrawing)
glClear( GL_COLOR_BUFFER_BIT );

glPushMatrix();
glTranslatef(controls.MainGlFrame.GetCameraX(),
controls.MainGlFrame.GetCameraY(),0);
//glTranslatef(current.ScalePoint.x,current.ScalePoint.y,0);


glScalef(current.ScaleFactor,current.ScaleFactor,current.ScaleFactor);
//glTranslatef(-current.ScalePoint.x,-current.ScalePoint.y,0);


if(!current.isdrawing)
{
for(unsigned int currentlayer = 0; currentlayer < layer.size(); ++currentlayer)
{
PolygonTesselator.Init();
for(unsigned int i = 0; i < layer[currentlayer].Shapes.size(); i++)
{
if(layer[currentlayer].Shapes[i].DisplayListInt == -999)
{
gluTessNormal(PolygonTesselator.tobj, 0, 0, 1);
PolygonTesselator.Set_Winding_Rule(layer[currentlayer].Shapes[i].WindingRule);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texName);

layer[currentlayer].Shapes[i].DisplayListInt = glGenLists(1);
glNewList(layer[currentlayer].Shapes[i].DisplayListInt,GL_COMPILE);

PolygonTesselator.SetDimensions(layer[currentlayer].Shapes[i].Dimensions,layer[currentlayer].Shapes[i].minima);
PolygonTesselator.Begin_Polygon();
for(unsigned int c = 0; c < layer[currentlayer].Shapes[i].Contour.size(); ++c)
{
if(layer[currentlayer].Shapes[i].Color.a != 0)
{
PolygonTesselator.Begin_Contour();

for(unsigned int j = 0; j < layer[currentlayer].Shapes[i].Contour[c].DrawingPoints.size(); ++j)
{
gluTessVertex(PolygonTesselator.tobj,&layer[currentlayer].Shapes[i].Contour[c].DrawingPoints[j][0],
&layer[currentlayer].Shapes[i].Contour[c].DrawingPoints[j][0]);
}

PolygonTesselator.End_Contour();
}
}
PolygonTesselator.End_Polygon();
glEndList();
PolygonTesselator.TransferVerticies(layer[currentlayer].Shapes[i].OutPoints);
glGenBuffersARB(1,&layer[currentlayer].Shapes[i].VBOInt);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,layer[currentlayer].Shapes[i].VBOInt);
glBufferDataARB(GL_ARRAY_BUFFER_ARB,sizeof(GLfloat) * layer[currentlayer].Shapes[i].OutPoints.size(),
&layer[currentlayer].Shapes[i].OutPoints[0], GL_STATIC_DRAW_ARB);

InvalidateRect(controls.MainGlFrame.framehWnd,NULL,false);
}
else //run vbo
{
//glEnable(GL_TEXTURE_2D);
//glDisable(GL_TEXTURE_2D);
//glBindTexture(GL_TEXTURE_2D, texName);
glColor4f(layer[currentlayer].Shapes[i].Color.r,
layer[currentlayer].Shapes[i].Color.g,
layer[currentlayer].Shapes[i].Color.b,
layer[currentlayer].Shapes[i].Color.a);
//glColor4f(1,1,1,1);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, layer[currentlayer].Shapes[i].VBOInt);
//glCallList(layer[currentlayer].Shapes[i].DisplayListInt);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, layer[currentlayer].Shapes[i].OutPoints.size() / 2);

glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
}

glDisable(GL_TEXTURE_2D);
//Draw outlines
if(layer[currentlayer].Shapes[i].Outline.OutlinePoints.size() > 4)
{
glColor4f(layer[currentlayer].Shapes[i].Outline.OutlineColor.r
,layer[currentlayer].Shapes[i].Outline.OutlineColor.g
,layer[currentlayer].Shapes[i].Outline.OutlineColor.b
,layer[currentlayer].Shapes[i].Outline.OutlineColor.a);
}

}
PolygonTesselator.End();
}
}

glPopMatrix();

//end OGL code
glFlush();
SwapBuffers(hdc);

glDisable(GL_MULTISAMPLE_ARB);
EndPaint(controls.MainGlContext.mhWnd,&ps);

}

为什么我的 CPU 使用率会这么高?

最佳答案

第一段代码在什么条件下运行?其中有几行看起来可疑的行:

glDeleteTextures(1,&texName);
texName = functions.CreateGradient(col,t,S,512,512,true);

如果您每次绘制时都删除并重新创建纹理,则成本可能会很高。我不能说 OpenGL 部分会有多昂贵——我希望上传纹理数据相当有效,即使删除和创建纹理名称可能不太有效——但也许 CreateGradient 是本质上很慢。或者您可能不小心遇到了显卡的某种慢速路径。或者该函数正在创建所有 mipmap 级别。等等。

除此之外,还有一些随机的想法:

  • 当前间隔是多少?如果缓冲区交换设置为与显示器同步,则可能会因此而产生延迟。 (您可以使用 WGL_EXT_swap_control 扩展来调整此值。)

  • 如果所有这些都是为了响应 WM_PAINT 而运行,请检查您是否没有因某种原因收到意外的额外 WM_PAINT。

  • 检查多边形 segmentation 器 InitEnd 函数是否没有执行任何操作,因为它们每次都会被调用,即使没有进行 segmentation 完成。

关于c++ - 使用VBO,CPU使用率非常高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3134041/

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