Windows 上的 OpenCL/OpenGL 纹理互操作:调整 OpenGL 纹理的大小
Posted
技术标签:
【中文标题】Windows 上的 OpenCL/OpenGL 纹理互操作:调整 OpenGL 纹理的大小【英文标题】:OpenCL/OpenGL Texture interop on windows: resizing OpenGL textures 【发布时间】:2016-08-19 13:23:34 【问题描述】:跟进这个问题Problems with OpenCL/OpenGL Texture interop / windows:
我们有:
渲染到 FBO,纹理附加为 COLOR_ATTACHMENT0,这是inTex
OpenCL 中的可分离降频器,使用inTex
,通过tempTex
在三步过程中生成outTex
现在出现了一个新问题,因为这是一个编辑器实现。如果inTex
的大小改变或outTex
的大小改变,首先使用glTexImage2d
调整OpenGL 纹理的大小。然后释放三个内核对象,最后使用clReleaseMemObj( XTex )
释放所有三个纹理的cl_mem
对象。最后一步只是使用与初始化时相同的代码重新创建所有必要的对象,然后循环继续。
在使用clCreateFromGLTexture2D
为这些纹理重新创建 cl_mem 对象期间,我收到了CL_OUT_OF_RESOURCES
。因此,不再执行缩减,因为设置纹理内核参数会产生CL_INVALID_MEM_OBJECT
。
也许一些伪代码有助于理解这个问题:
// OBJECTS //
////////////
cl_kernel k_x, k_y, k_clear;
cl_mem textureObjects[3];
int src_width, src_height, dst_width, dst_height;
size_t rngClr[3], offsClr[3], range_x[3], range_y[3];
float clrCol[4]
// MAIN ENTRY POINT //
/////////////////////
if ( ( fbo ) && calculate() )
glFinish();
if ( CL_recreate ) recreateCLstuff();
CLmgr->acquireGLObjects( 3, textureObjects );
CLmgr->callKernel( k_clear, rngClr, offsClr, "clear" );
CLmgr->setSimpleKernelArg( k_x, 3, 8, &convertXY, "horizontal::convert" );
CLmgr->setSimpleKernelArg( k_x, 4, 16, &offsets_X, "horizontal::offset" );
CLmgr->callKernel( k_x, range_x, "horizontal downsample" );
CLmgr->setSimpleKernelArg( k_y, 3, 8, &convertXY.m128_f32[ 2 ], "vertical::convert" );
CLmgr->setSimpleKernelArg( k_y, 4, 16, &offsets_Y, "vertical::offset" );
CLmgr->callKernel( k_y, range_y, "vertical downsample" );
CLmgr->releaseGLObjects( 3, textureObjects );
clFinish();
bool calculate()
// check if a resize of the "inTex" happened or a resize of
// "outTex" or "tempTex" is necessary
int srcw, srch, dstw, dsth;
// acquire above values
if ( ( srcw != src_width ) || ( srch != src_height ) || ( dstw != dst_width ) || ( dsth != dst_height ) )
CL_recreate = true;
// rebuild temporary texture
if ( ( dst_width != dstw ) || ( src_height != srch ) )
resizeTemporaryOpenGLTexture();
// rebuild target texture
if ( ( dst_width != dstw ) || ( dst_height != dsth ) )
resizeOutputOpenGLTexture();
// finally copy new values to the non-temporary objects
// produce all necessary parameters:
// - ranges (rngClr, offsClr, range_x, range_y)
// - convertXY
// - offsets_X
// - offsets_Y
void recreateCLstuff()
releaseCLstuff();
cerr << name() << ": recreating CL-stuff...\n";
k_x = CLmgr->newKernelInstance( className(), "separable" );
k_y = CLmgr->newKernelInstance( className(), "separable" );
k_clearEmpty = CLmgr->newKernelInstance( className(), "clearEmpty" );
k_clear = CLmgr->newKernelInstance( className(), "clear" );
// [...]
textureObjects[ 0 ] = CLmgr->createTexture( sourceTexID, WI_CL_TEXTURE_USE::sourceTexture, "source FBO texture" );
CLmgr->setMemKernelArg( k_x, 0, textureObjects[ 0 ], "horizontal::src" );
textureObjects[ 1 ] = CLmgr->createTexture( tempTexID, WI_CL_TEXTURE_USE::tempTexture, "separable downscaler buffer" );
CLmgr->setMemKernelArg( k_x, 1, textureObjects[ 1 ], "horizontal::dst" );
CLmgr->setMemKernelArg( k_y, 0, textureObjects[ 1 ], "vertical::src" );
textureObjects[ 2 ] = CLmgr->createTexture( textureID, WI_CL_TEXTURE_USE::targetTexture, "WI_CLtexture target" );
CLmgr->setMemKernelArg( k_y, 1, textureObjects[ 2 ], "vertical::dst" );
CLmgr->setMemKernelArg( k_clear, 1, textureObjects[ 2 ], "clear::dst" );
CLmgr->setSimpleKernelArg( k_clear, 0, 16, clrCol, "clear::clearColor" );
CL_recreate = false;
void releaseCLstuff()
cerr << name() << ": releasing CL stuff...\n";
cl_int err;
#define releaseKernel( obj ) if ( obj ) if ( err = clReleaseKernel( obj ) ) cerr << "release kernel object \""##obj##"\" failed! (" << clErrorString( err ) << ")\n"
releaseKernel( k_x );
releaseKernel( k_y );
releaseKernel( k_clear );
for ( int i = 0; i < 3; ++i )
if ( textureObjects[ i ] )
if ( err = clReleaseMemObject( textureObjects[ i ] ) )
cerr << "release texture object #" << i << " failed! (" << clErrorString( err ) << ")\n";
else
textureObjects[ i ] = nullptr;
【问题讨论】:
【参考方案1】:简单的答案:显然 OpenCL 不喜欢重用 OpenGL 纹理。如果共享纹理的大小发生变化,则需要处理以下步骤:
glDeleteTextures( 1, &id );
glGenTextures( 1, &id );
glBindTexture( GL_TEXTURE_2D, id );
// set texture parameters, so it becomes complete in the eyes of OpenCL and OpenGL
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
// allocate texture storage in the GPU
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr );
glBindTexture( GL_TEXTURE_2D, 0 );
glFinish();
object = clCreateFromGLTexture2D( context, flags[ usage ], GL_TEXTURE_2D, 0, &id, nullptr );
clSetKernelArg( kernel, argPos, sizeof( cl_mem ), &object );
【讨论】:
以上是关于Windows 上的 OpenCL/OpenGL 纹理互操作:调整 OpenGL 纹理的大小的主要内容,如果未能解决你的问题,请参考以下文章
Windows 10上的IE 11 VS 10上的Windows 11
如何从 Windows 10 上的容器连接到 docker 主机(Docker for Windows)