gpt4 book ai didi

android - 绘制过多元素时 glDrawArrays() 出现段错误

转载 作者:行者123 更新时间:2023-11-29 14:41:57 31 4
gpt4 key购买 nike

我有一些简单的 C++ OpenGL ES 代码,我在运行 Android 的手机、运行 webOS 的手机和使用 SDL/OpenGL 的 Mac 上运行。有问题的代码只是绘制了一个 GL_LINE_STRIP,它存储在一个 GLfloat 数组中。以下是我尝试执行此操作的方式(版本 1):

GLfloat array* = new GLfloat[2048];
//later... (array gets filled)
int howMany = 1024;
glVertexPointer(2, GL_FLOAT, 0, array); checkError();
glDrawArrays(GL_LINE_STRIP, 0, howMany); checkError();

看起来应该很简单。但是,Android 和我的 Mac 上的 OpenGL 驱动程序中的上述代码段错误(似乎在 webOS 上运行良好,无论出于何种原因。)这是我必须做的事情来避免这种情况(版本 2):

GLfloat array* = new GLfloat[2048];
//later... (array gets filled)
int howMany = 1024;
for (unsigned i = 0; i < howMany; i += 789) {
glVertexPointer(2, GL_FLOAT, 0, array+i*2); checkError();
//this is not a bug: we draw one more than we increment by, or there is a hole in the graph
glDrawArrays(GL_LINE_STRIP, 0, std::min(790U, howMany-i)); checkError();
}

限制一次绘制的线条数量始终如一地避免段错误。 790这个数字是通过实验确定的;在 791 时它在 OSX 上崩溃(Android 可以更高,尽管它在许多版本之前被限制为 113...

我的应用程序中还有很多其他 GL 内容,但它是一个大型应用程序,发布所有这些代码是不切实际的。那么,什么可能导致这种情况呢?我应该研究哪些可能性?

这是当我尝试使用版本 1 时我的程序在 OSX 上运行和崩溃的 valgrind 输出:

==85414== Conditional jump or move depends on uninitialised value(s)
==85414== at 0x1D022993: glVertexPointer_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414== by 0x6AA59A9: glVertexPointer (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib)
==85414== by 0x2B001: draw_elements (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x2B44C: glDrawArrays (in ./rta.app/Contents/MacOS/rta)
==85414== by 0xB639: GLImage::glDraw(int, int) (in ./rta.app/Contents/MacOS/rta)
==85414== by 0xC292: GLLabel::glDraw(int, int) (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x1AC67: RTAPlot::drawAxes(int) (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x1B30A: RTAPlot::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x225F5: RTAPlotView::updateForeground() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x19FC4: RTAComponent::update() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x805D: AudiaApplication_private::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x267DF: AudiaApplication::run() (in ./rta.app/Contents/MacOS/rta)
==85414== Uninitialised value was created by a stack allocation
==85414== at 0x1D02268F: glVertexPointer_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414==
==85414== Conditional jump or move depends on uninitialised value(s)
==85414== at 0x1D022E0C: glTexCoordPointer_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414== by 0x6AA5216: glTexCoordPointer (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib)
==85414== by 0x2B279: draw_elements (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x2B44C: glDrawArrays (in ./rta.app/Contents/MacOS/rta)
==85414== by 0xB639: GLImage::glDraw(int, int) (in ./rta.app/Contents/MacOS/rta)
==85414== by 0xC292: GLLabel::glDraw(int, int) (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x1AC67: RTAPlot::drawAxes(int) (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x1B30A: RTAPlot::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x225F5: RTAPlotView::updateForeground() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x19FC4: RTAComponent::update() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x805D: AudiaApplication_private::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x267DF: AudiaApplication::run() (in ./rta.app/Contents/MacOS/rta)
==85414== Uninitialised value was created by a stack allocation
==85414== at 0x1D022A6B: glTexCoordPointer_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414==
==85414== Invalid read of size 8
==85414== at 0x1D0B9166: gleRunVertexSubmitImmediate (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414== by 0x1D0B84FB: gleLLVMArrayFunc (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414== by 0x1D0A14F2: gleDrawArraysOrElements_ExecCore (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414== by 0x1D0A2A04: glDrawElements_IMM_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414== by 0x6AA304F: glDrawElements (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib)
==85414== by 0x2B2DB: draw_elements (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x2B44C: glDrawArrays (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x1FEF6: RTAPlotCanvas::drawLineStrip(float*, unsigned int) (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x1E91A: RTAPlotCanvas::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x1B318: RTAPlot::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x225F5: RTAPlotView::updateForeground() (in ./rta.app/Contents/MacOS/rta)
==85414== by 0x19FC4: RTAComponent::update() (in ./rta.app/Contents/MacOS/rta)
==85414== Address 0xc0000000 is not stack'd, malloc'd or (recently) free'd
==85414==
==85414==
==85414== Process terminating with default action of signal 11 (SIGSEGV)
==85414== General Protection Fault
==85414== at 0x10AED0: misaligned_stack_error_ (in /usr/lib/libSystem.B.dylib)
==85414== by 0x1E45E87: -[NSCustomReleaseData dealloc] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414== by 0x1E45E37: -[NSBitmapImageRep _freeData] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414== by 0x1E45DCA: -[NSBitmapImageRep _freeImage] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414== by 0x1E45D74: -[NSBitmapImageRep dealloc] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414== by 0x171DF37: CFRelease (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
==85414== by 0x1748691: __CFArrayReleaseValues (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
==85414== by 0x171E100: _CFRelease (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
==85414== by 0x1D89F7A: -[NSImage dealloc] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414== by 0x20857D4: -[NSCursor dealloc] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414== by 0xDFE33: QZ_FreeWMCursor (in /opt/PalmPDK/host/lib/libSDL-1.2.0.11.2.dylib)
==85414== by 0xCA11F: SDL_FreeCursor (in /opt/PalmPDK/host/lib/libSDL-1.2.0.11.2.dylib)
--85414:0:schedule VG_(sema_down): read returned -4

请注意,前两个条件跳转错误每次都会发生,并且可能不是我的应用程序的问题。仅当我尝试在 OSX 上使用版本 1 时才会出现无效读取错误。

最佳答案

它可能与你的顶点数组根本无关。例如,可能仍然存在指向其他顶点属性集的指针,当然,如果它们的缓冲区小于新顶点数组,您将遇到段错误。

如果您仔细查看 valgrind 输出,您会发现仍然有一个纹理坐标指针集。

所以我建议您禁用所有不需要的顶点属性数组:

glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
/* ... */

你的情况

glDisableClientState(GL_TEXTURE_COORD_ARRAY);

关于android - 绘制过多元素时 glDrawArrays() 出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6325119/

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