| 24 | |
| 25 | template<typename T> |
| 26 | void copy_image(const Array<T> &in, fg_image image) { |
| 27 | ForgeModule &_ = forgePlugin(); |
| 28 | if (isGLSharingSupported()) { |
| 29 | CheckGL("Begin opencl resource copy"); |
| 30 | |
| 31 | auto res = interopManager().getImageResources(image); |
| 32 | |
| 33 | const cl::Buffer *d_X = in.get(); |
| 34 | |
| 35 | unsigned bytes = 0; |
| 36 | FG_CHECK(_.fg_get_image_size(&bytes, image)); |
| 37 | |
| 38 | std::vector<cl::Memory> shared_objects; |
| 39 | shared_objects.push_back(*(res[0].get())); |
| 40 | |
| 41 | glFinish(); |
| 42 | |
| 43 | // Use of events: |
| 44 | // https://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReleaseGLObjects.html |
| 45 | cl::Event event; |
| 46 | |
| 47 | getQueue().enqueueAcquireGLObjects(&shared_objects, NULL, &event); |
| 48 | event.wait(); |
| 49 | getQueue().enqueueCopyBuffer(*d_X, *(res[0].get()), 0, 0, bytes, NULL, |
| 50 | &event); |
| 51 | getQueue().enqueueReleaseGLObjects(&shared_objects, NULL, &event); |
| 52 | event.wait(); |
| 53 | |
| 54 | CL_DEBUG_FINISH(getQueue()); |
| 55 | CheckGL("End opencl resource copy"); |
| 56 | } else { |
| 57 | CheckGL("Begin OpenCL fallback-resource copy"); |
| 58 | unsigned bytes = 0, buffer = 0; |
| 59 | FG_CHECK(_.fg_get_image_size(&bytes, image)); |
| 60 | FG_CHECK(_.fg_get_pixel_buffer(&buffer, image)); |
| 61 | |
| 62 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer); |
| 63 | glBufferData(GL_PIXEL_UNPACK_BUFFER, bytes, 0, GL_STREAM_DRAW); |
| 64 | auto *ptr = static_cast<GLubyte *>( |
| 65 | glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)); |
| 66 | if (ptr) { |
| 67 | getQueue().enqueueReadBuffer(*in.get(), CL_TRUE, 0, bytes, ptr); |
| 68 | glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); |
| 69 | } |
| 70 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); |
| 71 | CheckGL("End OpenCL fallback-resource copy"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #define INSTANTIATE(T) template void copy_image<T>(const Array<T> &, fg_image); |
| 76 |
nothing calls this directly
no test coverage detected