Problems with creating a CLBuffer from existing DirectBuffer

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems with creating a CLBuffer from existing DirectBuffer

barno
Hi,

I am trying to create a CLBuffer from an existing DirectBuffer. Running that code:
CLBuffer<FloatBuffer> someDataCL =
context.createBuffer(someObject.getExistingDirectFloatBuffer(), READ_ONLY);
(the buffer is being rewinded before doing this)
All the values as read by my OpenCL device become something like 10E-40 (exactly the same value for all elements).

When running this code:
CLBuffer<FloatBuffer> someDataCL =
context.createFloatBuffer(someObject.getExistingDirectFloatBuffer().capacity(), READ_ONLY);
someDataCL.getBuffer().put(someObject.getExistingDirectFloatBuffer());
someDataCL.getBuffer().rewind();
everything works just fine.

I don't understand what's going on.... am I missing something?

btw. the existing DirectFloatBuffer is allocated like this:
FloatBuffer buffer = ByteBuffer.allocateDirect(4*numElements).asFloatBuffer();

Thanks,
barno
Reply | Threaded
Open this post in threaded view
|

Re: Problems with creating a CLBuffer from existing DirectBuffer

barno
sorry, forgot to mention that I of course transfer it to the OpenCL device by queue.putWriteBuffer(someDataCL,true);
Reply | Threaded
Open this post in threaded view
|

Re: Problems with creating a CLBuffer from existing DirectBuffer

notzed
btw. the existing DirectFloatBuffer is allocated like this:
FloatBuffer buffer = ByteBuffer.allocateDirect(4*numElements).asFloatBuffer();

On a little-endian platform (i.e. x86) this wont work, it will treat the data as big-endian.

Something like this should:
FloatBuffer buffer = ByteBuffer.allocateDirect(4*numElements).order(ByteOrder.nativeOrder()).asFloatBuffer();
Although i think there is some jocl api to do this for you too.

Although I don't know if that's your only issue.


Reply | Threaded
Open this post in threaded view
|

Re: Problems with creating a CLBuffer from existing DirectBuffer

barno
That solved it for me, thanks! (although it might be nicer if there is a jocl API)
Reply | Threaded
Open this post in threaded view
|

Re: Problems with creating a CLBuffer from existing DirectBuffer

Michael Bien
  the shortcut for creating a direct allocated buffer with native byte
order is:

Buffers.newFooBuffer(size)

regards,
michael

On 06/27/2011 01:12 PM, barno [via jogamp] wrote:
>
> That solved it for me, thanks! (although it might be nicer if there is a jocl
> API)