2
$\begingroup$

I want to write (fragment) shaders in OpenCL rather than GLSL. Is there a standard mechanism for this (from OpenGL, and ideally also from DirectX)? Are there any drawbacks to doing this?

I also want to write screenspace effects in OpenCL. Is the mechanism the same? Are there any drawbacks?

Thanks

$\endgroup$
3
$\begingroup$

OpenCL isn't really a drawing API, it's designed for doing compute workloads across different devices, not necessarily GPUs. You can certainly write a kernel that renders to a texture in OpenCL, and have this texture be available to Open GL or D3D. AFAIK, though, you wouldn't have access to the fixed-function hardware like rasterization, attribute interpolation, and fragment blending, so you'd have to implement it yourself (unless you're writing a ray tracer). You'd also likely have to use one of the drawing APIs to present your texture to the screen, by rasterizing a full screen quad and sampling your texture at each pixel. This carries a synchronization penalty - although OpenGL and D3D can interoperate with OpenCL, they can't access the same memory at the same time, so you need to wait for control to transfer from one API to the other.

Using compute on an already rendered buffer for screenspace effects is a good idea, since you save the fillrate of rasterizing a full screen quad every time you apply a new effect compared to the fragment shader approach. These days though, OpenGL and D3D have compute shaders built-in to handle this kind of use case. Why do you want to use OpenCL exactly? Seems to me you'd be better off using one of the rendering APIs.

$\endgroup$
  • $\begingroup$ I want to use OpenCL mainly for reasons of familiarity and portability. $\endgroup$ – barneypitt Mar 15 '18 at 9:55
  • 2
    $\begingroup$ Familiarity, fair enough. As for portability, you can run OpenGL anywhere you can run OpenCL (although Apple's version is pretty badly out of date). Still worth learning GLSL though, it's pretty easy if you already know any of the C-variant languages. $\endgroup$ – russ Mar 15 '18 at 10:03
  • $\begingroup$ By portability I meant portable to DirectX. I certainly don't want to do my own rasterisation! Your answer is very elucidating and I think I'm beginning to understand what OpenCL should and shouldn't be used for in a graphics pipeline. One thing I would like to do is manipulate textures and potentially generate textures before they are used in the pipeline - am I right in thinking this would be something OpenCL would be suitable for? It seems I can use a shared GL texture/surface in OpenCL and write to it, and don't see any reason this wouldn't be possible. $\endgroup$ – barneypitt Mar 15 '18 at 10:05
  • $\begingroup$ Yeah, for asset creation and manipulation it's fine. I've even seen a paper where they were doing Marching Cubes in OpenCL to generate a vertex buffer each frame then rendering it with OpenGL - but the cost of syncing between APIs was eating a significant chunk of frame time. $\endgroup$ – russ Mar 15 '18 at 10:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.