Title: Arrayfire -> glium interaction possible? · arrayfire/arrayfire-rust · Discussion #278 · GitHub
Open Graph Title: Arrayfire -> glium interaction possible? · arrayfire/arrayfire-rust · Discussion #278
X Title: Arrayfire -> glium interaction possible? · arrayfire/arrayfire-rust · Discussion #278
Description: Arrayfire -> glium interaction possible?
Open Graph Description: Hi, I'm trying to get arrayfire and glium to cooperate; Is that currently possible with both libraries as they are? or is there some lower level work that needs to be done? Basically trying to figu...
X Description: Hi, I'm trying to get arrayfire and glium to cooperate; Is that currently possible with both libraries as they are? or is there some lower level work that needs to be done? Basically trying to ...
Opengraph URL: https://github.com/arrayfire/arrayfire-rust/discussions/278
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"Arrayfire -> glium interaction possible?","text":"Hi, I'm trying to get arrayfire and glium to cooperate; Is that currently possible with both libraries as they are? or is there some lower level work that needs to be done?
\nBasically trying to figure out how to do this:
\nlet arr: Array<f32>\nglium::VertexBuffer::new_raw(display as &Facade, arr.device_ptr() ,bindings, arr.elements() * std::mem::size_of::<f32>()).unwrap()\n//or\nglium::buffer::Buffer::new<f32>(display as &Facade, arr.device_ptr(), BufferType::TextureBuffer, glium::buffer::BufferMode::Default)
\nwhile keeping everything on the gpu and avoiding expensive gpu->cpu->gpu operations.
\nIs this even possible? And if so, what steps need to be taken?
\nThanks!
","upvoteCount":2,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"@nodeSpace Yes, I believe it is completely possible to do that. However, it would require some additional helper code apart from using the libraries glium and arrayfire. arrayfire/forge essentially handles the same workflow(avoids via CPU data transfers) which is ArrayFire's graphics(OpenGL based) functionality provider.
\nLet's say you are using CUDA backend of ArrayFire. Have a look at the functions from forge helper header: createGLBuffer, copyToGLBuffer, releaseGLBuffer for an idea on C++ code does it. You would need similar functions in rust that are written with glium as target. I think the following steps should essentially let you handle the OpenGL interop successfully.
\n\n- Create a shared resource handle
cudaGraphicsResource in rust using vertex buffer resource id returned by glium. Note that this vertex buffer's size in bytes should match the array you want to display. Matching data type is also important - Essentially what createGLBuffer does in forge project \n- Copy data from CUDA device pointer to shared resource created in step (1). Note that this is also a device buffer sitting on GPU. So, you are essentially only doing a device-to-device memcopy which is many times faster than host-to-device/device-to-host copy. - Essentially what
copyToGLBuffer does in forge project. \n- Release the shared resource handle during program exit or when you are done using it.
\n
\nThis workflow would need CUDA rust API. You can take a look at arrayfire's CUDA/OpenCL interop crates to see what we use in those projects. Any crate exposes CUDA API will do the job of creating/copying/releasing the shared CUDA-OpenGL resource.
\n\nThe entire process remains the same if you use OpenCL backend with OpenCL APIs replacing their respective CUDA APIs.
\nUpdated
\nMy mistake, I didn't mention one additional detail regarding OpenCL, you would need to some additional modifications to your code.
\nFirstly, use ArrayFire's OpenCL interop crate - https://github.com/arrayfire/arrayfire-rust/tree/master/opencl-interop
\nYou need to use some crate that enables you use OpenCL API in rust, you can use the OpenCL crate that af-opencl-interop crate uses for easy integration.
\nThe following are the additional steps:
\n\n- create an OpenCL context using OpenGL context handle
\n- Now add the device on which context from step (1) is created and the context itself to ArrayFire device manager - Please look at the documentation of af_opencl_interop for relevant functions.
\n- Set device to this newly added device in ArrayFire and use that for computations.
\n
\nWith the above setup done before any devices are used by OpenCL in ArrayFire. You should be able to share the OpenCL-OpenGL context. There are some examples in af-opencl-interop crate, please have a look - they might help with figuring out the opencl context creation etc.
","upvoteCount":2,"url":"https://github.com/arrayfire/arrayfire-rust/discussions/278#discussioncomment-348321"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:3f934123-352c-8152-3024-4220340db5d7 |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | BC5A:27C189:5E17:74DF:696E637E |
| html-safe-nonce | 3052330059fd065ef675bb740575b72c72f060bfa500f6bac7904c92c5e06dbe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzVBOjI3QzE4OTo1RTE3Ojc0REY6Njk2RTYzN0UiLCJ2aXNpdG9yX2lkIjoiODY0MjY3NzU2NTI1NzgzNTM5MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 06ec54b7c8f18d5f10b16087269716b1553988550c08e2cfaf88870a2c4c0892 |
| hovercard-subject-tag | discussion:2815976 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/arrayfire/arrayfire-rust/278/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/2aa007abb8e5468289da672056b071f0728ed0c57982e151face5b2021e17fc5/arrayfire/arrayfire-rust/discussions/278 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2aa007abb8e5468289da672056b071f0728ed0c57982e151face5b2021e17fc5/arrayfire/arrayfire-rust/discussions/278 |
| og:image:alt | Hi, I'm trying to get arrayfire and glium to cooperate; Is that currently possible with both libraries as they are? or is there some lower level work that needs to be done? Basically trying to figu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 99a4a74f92e7618f6e15b4dbb9f4470c8d31701a90d89c21ff31970528b7f0c3 |
| turbo-cache-control | no-preview |
| go-import | github.com/arrayfire/arrayfire-rust git https://github.com/arrayfire/arrayfire-rust.git |
| octolytics-dimension-user_id | 5395442 |
| octolytics-dimension-user_login | arrayfire |
| octolytics-dimension-repository_id | 35817370 |
| octolytics-dimension-repository_nwo | arrayfire/arrayfire-rust |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 35817370 |
| octolytics-dimension-repository_network_root_nwo | arrayfire/arrayfire-rust |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ef0d97829e997c64864f25a29b00067f5dc3a4c4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width