I’ve been trying to use OpenGL functions directly within my code. I know that there is a FOpenGL name I can use to access them. That name, however, is defined in a private header (for Windows: OpenGLWindows.h) and therefore, I can’t use it. I’ve tried modifying my project’s Build.cs file to also include OpenGLDrv module, by adding:
I can use public headers fine, but this wasn’t enough for the compiler to find that private header I mentioned. Also, if I simply try including a public header (such as OpenGL4.h), there are compiling errors complaining it can’t find some OpenGL primitives (in this case it was GLsync), which I think tells me that it isn’t linking with OpenGL libraries.
So, in short, what is the correct way to enable the use of OpenGL directly within my code? Were my attempts in the right direction or is there a much simpler way?
I hope I was clear, I’d much appreciate some help!
In that case ur better off looking into the RHI architecture thats in place. That way you can write code that will work for both OpenGL and D3D on all platforms. Using any of the rendering interfaces directly is going to lock you down severly.
Despite that, for now we want to be able to use GLSL code that’s already made and use it directly. It’s not a matter of stubbornness. We’ll deal with DirectX later. For now we really need to be able to use our compute shader directly.
You wont be able to use GLSL. The shaders are written in HLSL and converted to GLSL during the compilation process. So for the very least you won’t be able to write the shaders in GLSL.
Somewhere within the engine there’s surely a module can converts HLSL to GLSL. Conceptually speaking, if one could create a module that bypassed that conversion and simply provided previously written GLSL code, wouldn’t that work? Is that possible? I would say so, by what I’ve seen while sniffing around the source code, but I’m not sure. (based on OpenGLShaderComplier.cpp module, CompileShader_Windows_OGL function)
Also, while we’re at it, what alternatives are there to create a compute shader in UE4?
To me it seems much simpler to simply use OpenGL functions, which UE4 does have! I just can’t access them, hence my initial question.
Im sure you could, but it seems a lot more effort than its worth.
The simplest way would be using RHI. By creating a new compute shader derived from the FGlobalShader class, then RHICmdList.SetComputeShader can be used to set it, and DispatchComptueShader can be used to execute it. That will work for both D3D and OGL. The shader side of things would need to be written in HLSL to be able to do the previous, but I really think it will be much easier to convert your GLSL code into HLSL then it would be to invoke the HLSL -> GLSL converter and expose the OGL interface.
Take a look at my FluidSurface in my signature. It uses a compute shader which I create and invoke via RHI.
Seems to me that the amount of work you’d have to put into modifying the engine so that you can do what you want to do is quite a bit higher than the amount of work you’d have to do to rewrite your shaders so that they can fit into the standard UE pipeline.
Thank you for your answers. Indeed, using HLSL seems to be easier, but let’s assume for a moment that I’m willing to suffer any pain of forcing GLSL into UE4. Is there a way? Is it possible? Has anyone done it before?
Rest assured, all your input is greatly appreciated!
If I was to try this approach, I would probably start with “BuildShaderOutput” in OpenGLShaderCompiler.cpp (Bypass the UE4 shader system and send the GLSL code directly to the compiler). Then using FShaderCompilerOutput::Code (TArray<uint8>). I would pass that to RHICreateComputeShader and then execute as per normal (This way I still use RHI, but can write shaders in GLSL). This in theory should work, but the BuildShaderOutput function is only available from the editor, so you would need to serialize out the compiled shader code somewhere for packaged products.
I must thank you for your most useful replies. Eventually I will want to do it exactly as you are suggesting.
However, right now it would be more convenient to be able to use the private headers in OpenGLDrv module. It feels that I just need to modify my project’s Build.cs file or something similar, although I can’t exactly grasp what it could be.
While trying to solve my problem I figured my project isn’t linking correctly (or at all) with OpenGL library.
How can I link to OpenGL library? (there’s a third party folder for OpenGL, and although I managed to include the headers there I still seem to be missing that final library link)
In your build.cs add the full path of the lib file to the PublicAdditionalLibraries list. You can find all the Module Rules in Progams > UnrealBuildTool > System > RulesCompilers.cs