OpenGL ComputeShader vs CUDA, can ComputeShader replace CUDA?

OpenGL ComputeShader vs CUDA, can ComputeShader replace CUDA?
and is it very time consuming when switch OpenGL and CUDA context? If it was, what’s the fastest way to use OpenGL render the content generated by CUDA?

General purpose computing on GPUs started with shaders. That offered limited functionality and required some familiarity with graphics APIs. This was the reason CUDA came about, as a more powerful and more convenient way to program GPUs in programming languages many programmers already know: first C, then (around 2009) C++. Today CUDA is basically a subset of C++17, while compute shaders continue to offer limited C-like functionality.

I cannot quantify the overhead of OpenGL interop in CUDA, but my understanding is that it is more than negligible. If you have (soft!) real-time requirements, you may need to do a little feasibility study first to make sure the desired throughput is achievable despite the interop overhead.

I would say that if your application is mostly geared towards graphics, and has compute needs that are modest in nature, try compute shaders first, as that will likely offer the best performance and keep the complexity of the programing environment as low as possible.

On the other hand, if an application’s focus is mostly on computation, and possibly of a quite complex nature at that, with OpenGL merely needed for outputting final results, then look at CUDA’s OpenGL interop first.

You could do a literature search to see if there are publications in which others have tackled use cases similar to yours with GPUs and see which of the two approaches they favored and why.