Problems with OpenCL/OpenGL Texture interop / windows
<p>To improve rendering quality I'm writing on a versatile separable downscaler in OpenCL 1.1.</p>

<p>The basic image (covering only a small part of the final image) is rendered into a very large framebuffer.  Then its color-attached texture is downsampled and placed into another texture via OpenCL.  Finally a screen-aligned quad gets rendered to show the result.</p>

<p>So far the idea.  What do we have:</p>

<ul>
<li>2 instances of the downscaler-kernel (it stores the results with coordinates exchanged (i.e. as (y,x) )</li>
<li>inputTexture (the color attachment of the rtt-framebuffer)</li>
<li>tempTexture, size: inputHeight x outputWidth, created with CL_MEM_READ_WRITE</li>
<li>outputTexture</li>
</ul>

<p>Running <code>kernel_instance_1( &lt;otherParams&gt;, inputTexture, tempTexture )</code> produces the desired result, <strong>but only in the very first frame</strong> - somehow the changes happening in the animation don't show up at all.  As I get no errors (see below) I assume the kernel runs every frame, but the source texture content stays the same (which it doesn't, I also have a live-output of that texture).</p>

<p><em><code>Question:</code></em> Do I have to call clCreateFromGLTexture2D() every time the contents of the framebuffer changed?</p>

<p><strong>EDIT</strong> I just realized: the inputTexture is still attached to the framebuffer object's <code>GL_COLOR_ATTACHMENT0</code> - may this be a problem? <strong>ENDEDIT</strong></p>

<p>Running <code>kernel_instance_2( &lt;otherParams&gt;, tempTexture, outputTexture )</code> yields no visible result, even with a barrier enqueued between both kernel calls.  I.e. the outputTexture stays empty.</p>

<p><em><code>Question:</code></em> Do I need to release and re-acquire the texture object <code>tempTexture</code> in between both kernel calls, so OpenCL sees the changes?</p>

<p>Just to see what OpenCL-calls are made, the following output was produced:</p>

<pre><code>clCreateKernel( separable_X )
clRetainMemObject( separable_X::convolution )
clCreateKernel( separable_Y )
clRetainMemObject( separable_Y::convolution )
clCreateFromGLTexture2D( separable_X::dst + separable_y::src, texID=24, usage=temporary (source and target) )
clCreateFromGLTexture2D( separable_Y::dst, texID=18, usage=target )
clCreateFromGLTexture2D( separable_X::src, texID=22, usage=source )
clRetainMemObject( separable_X::dst )
clRetainMemObject( separable_Y::src )
clRetainMemObject( separable_Y::dst )
clRetainMemObject( clearEmpty::dst )
clEnqueueAcquireGLObjects( count=3 )
clEnqueueBarrier()
clSetKernelArg( separable_X::convert )
clSetKernelArg( separable_X::offset )
clSetKernelArg( separable_X::convolution )
clSetKernelArg( separable_X::dst )
clSetKernelArg( separable_X::src )
clEnqueueNDRangeKernel( separable_X, (1440, 1080, 0), waiting4 0 events )
clSetKernelArg( separable_Y::convert )
clSetKernelArg( separable_Y::offset )
clEnqueueBarrier()
clSetKernelArg( separable_Y::convolution )
clSetKernelArg( separable_Y::dst )
clSetKernelArg( separable_Y::src )
clEnqueueNDRangeKernel( separable_Y, (540, 1440, 0), waiting4 0 events )
clEnqueueBarrier()
clEnqueueReleaseGLObjects( count=3 )
</code></pre>

<p>If any call had produced an error, it would've been inside that output.</p>

<p>Another situation I get lots of times is that <code>clEnqueueReleaseGLObjects()</code> returns error code -9999, which somebody filed as "NVidia: Illegal read or write to a buffer".</p>

<p><em><code>Question:</code></em> could it be that <code>write_imagef()</code> does not clamp the color value if any component exceeds 1.0f and the storage format is RGBA8?  So that'd actually mean one must write <code>write_imagef( texture, (int2)coord, clamp( color, 0.f, 1.f ) );</code>...</p>

<p>Thanks a lot in advance - this gets me banging my head since nearly a week...</p>

<p><strong>EDIT</strong>
Some more infos that might be worth mentioning:</p>

<p><strong><em>how I can distinguish the two instances?</em></strong><br>
There are 2 distinct <code>__kernel</code> functions with different names (<code>separable_X</code> and <code>separable_Y</code>) inside the program source, which both have the same body calling the <code>separable()</code>-function.</p>

<p><strong><em>how do I sync between GL and CL?</em></strong><br>
 - the function taking care of acquiring GL objects issues a <code>glFinish()</code> before calling <code>clEnqueueAcquireGLObjects()</code><br>
 - I wait for completion of <code>clEnqueueReleaseGLObjects()</code> by using cl_events (likely to change in the future)</p>