【发布时间】:2012-01-10 05:26:18
【问题描述】:
我的 openGL 应用程序内存不足时遇到了一些问题,我正在尝试找出我的问题。为此,我创建了一个小测试程序,它基本上只是从调用 glDeleteTextures 的文件中加载一个巨大的纹理,然后再次加载它,如果我在 OSX 上运行这个测试程序,这个过程可以运行数百次迭代而没有问题(最我运行它是 1024 并且这没有问题)但是在 windows 上大约 14 次迭代后我得到一个 GLU_OUT_OF_MEMORY 错误。我一遍又一遍地加载的纹理是一个巨大的天空穹顶(4096 x 1024 和 4.9MB)。
这是我使用 cmets 为非 lisp 倾向运行的代码:
测试程序:
(with-glcontext (shared-opengl-view)
;;loop 1024 times
(dotimes (i 1024)
;; create a texture and return its texture name
(let ((texture (create-texture-from-file (truename "lui:resources;sky domes;DarkClouds.png"))))
;;Allocate two bytes to pass to glDeleteTextures
(ccl::rlet ((&texName :long))
;; put the texture string into our allocated bytes
(setf (ccl::%get-long &texName) texture)
;; Delete the textures?
(glDeleteTextures 1 &texName)))))
Create-texture-from-file(这个方法的大部分是由 openGL 调用组成的,所以我认为大多数 openGL 人应该对这里发生的事情有一个合理的理解,但我可以澄清任何令人困惑的事情):
(defun CREATE-TEXTURE-FROM-FILE (Filename &key Verbose (Build-Mipmaps t) Repeat (Mag-Filter *Default-OpenGL-Texture-Magnification-Filter*) Forced-Depth) "
in: Filename {string},
&key Verbose {boolean}, Repeat
out: OpenGL-Texture-Name {int}, width, height, depth
Load the <Filename> texture inside the texture directory.
- Ideal file should be 32 bit ARGB compatible, e.g., .png with mask or 24 bit RGB
- 8 bit and 16 bit image will work too
- Image size must be 2^n x 2^m, at least 64 x 64
- This function must be called with active AGL context, e.g., inside OpenGL-Window INIT method."
(declare (ftype function create-image-from-file))
(rlet ((&texName :long))
(multiple-value-bind (&Image Width Height Depth) (create-image-from-file Filename :verbose Verbose :flip-vertical t :forced-depth Forced-Depth)
(unless &Image (return-from create-texture-from-file nil))
(glPixelStorei GL_UNPACK_ROW_LENGTH Width) ; Set proper unpacking row length for image
(glPixelStorei GL_UNPACK_ALIGNMENT 1) ; Set byte aligned unpacking (needed for 3-byte-per-pixel image)
(glGenTextures 1 &texName)
; Specify the texture's properties.
(glBindTexture GL_TEXTURE_2D (%get-long &texName))
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_S (if Repeat GL_REPEAT GL_CLAMP_TO_EDGE))
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_T (if Repeat GL_REPEAT GL_CLAMP_TO_EDGE))
(glTexParameteri gl_texture_2d gl_texture_mag_filter Mag-Filter) ;; make textures look smooth (can be costly)
;; Mipmaps: make texture look good at different sizes
(if Build-Mipmaps
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR_MIPMAP_NEAREST)
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR))
;; Experimental: anisotropic filtering to make textures less blurry at angle
#-:cocotron (glTexParameterf GL_TEXTURE_2D GL_TEXTURE_MAX_ANISOTROPY_EXT 16.0)
(let ((PixelFormat (ecase Depth (32 GL_RGBA) (24 GL_RGB)))
(InternalFormat (ecase Depth (32 GL_RGBA8) (24 GL_RGB8))))
(glTexImage2D GL_TEXTURE_2D 0 InternalFormat width height 0 PixelFormat GL_UNSIGNED_BYTE &Image)
(when Build-Mipmaps
(when Verbose (format t "~%Building Mipmaps~%"))
(unless
;; The calls to gluBuild2DMipmaps will return GLU_OUT_OF_MEMORY when we run out.
(zerop (print (gluBuild2DMipmaps GL_TEXTURE_2D InternalFormat width height PixelFormat GL_UNSIGNED_BYTE &Image)))
(print (get-opengl-error))
(error "could not create mipmaps"))
(when Verbose (format t "Completed Mipmaps~%"))))
;; OpenGL should have copied now the image data into texture memory: release
(dispose-vector &Image)
;; return the texture handle and image dimensions
(values
(%get-long &texName)
Width
Height
Depth))))
我在其他地方读过 glDeleteTextures 并没有完全释放所有资源,但这些帖子都没有提供任何替代方案或解决方案。有没有这样的解决方案?
【问题讨论】:
-
不创建 mipmap 会发生什么?
-
@genpfault:当我不创建 mipmap 时,它会在 17 次迭代(而不是 14 次)后耗尽内存。
-
但是如果不生成mipmap,是不是生成的是GL_OUT_OF_MEMORY而不是GLU_OUT_OF_MEMORY?
-
在删除纹理后尝试调用 glIsTexture 和 glAreTexturesResident。使用与删除纹理时相同的 texID(在两种情况下都应该返回 false,但如果不是我们知道问题出在哪里)