Announcement

Collapse
No announcement yet.

How to call OpenGL functions directly?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to call OpenGL functions directly?

    Greetings!

    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:

    Code:
    PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "OpenGLDrv" }); 
    
    PrivateDependencyModuleNames.AddRange(new string[] { "OpenGLDrv" });
    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! :-)

    #2
    What do you want to do that requires accessing the OpenGL context directly?

    Comment


      #3
      Well, anything really, but I'm particularly interested in Compute Shaders.

      Comment


        #4
        Originally posted by dsl View Post
        Well, anything really, but I'm particularly interested in Compute Shaders.
        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.
        FluidSurface Plugin: https://github.com/Ehamloptiran/UnrealEngine/releases
        TextureMovie Plugin: https://github.com/Ehamloptiran/TextureMoviePlugin

        Comment


          #5
          Originally posted by Ehamloptiran View Post
          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.
          Yes, I understand those implications.

          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.

          Thank you.

          Comment


            #6
            Originally posted by dsl View Post
            Yes, I understand those implications.

            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.

            Thank you.
            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.
            FluidSurface Plugin: https://github.com/Ehamloptiran/UnrealEngine/releases
            TextureMovie Plugin: https://github.com/Ehamloptiran/TextureMoviePlugin

            Comment


              #7
              Originally posted by Ehamloptiran View Post
              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. :-)

              Thanks!

              Comment


                #8
                Originally posted by dsl View Post
                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)
                Im sure you could, but it seems a lot more effort than its worth.

                Originally posted by dsl View Post
                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. :-)

                Thanks!
                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 plugin in my signature. It uses a compute shader which I create and invoke via RHI.
                FluidSurface Plugin: https://github.com/Ehamloptiran/UnrealEngine/releases
                TextureMovie Plugin: https://github.com/Ehamloptiran/TextureMoviePlugin

                Comment


                  #9
                  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.

                  Comment


                    #10
                    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!

                    Comment


                      #11
                      Originally posted by dsl View Post
                      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.
                      FluidSurface Plugin: https://github.com/Ehamloptiran/UnrealEngine/releases
                      TextureMovie Plugin: https://github.com/Ehamloptiran/TextureMoviePlugin

                      Comment


                        #12
                        Originally posted by Ehamloptiran View Post
                        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.

                        Any suggestions on that aspect?

                        Comment


                          #13
                          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)

                          Comment


                            #14
                            Originally posted by dsl View Post
                            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

                            Comment


                              #15
                              Originally posted by jCoder View Post
                              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
                              I eventually discovered this as well, although I'm now having difficulties finding the actual needed library.

                              I tried:

                              Code:
                              PublicAdditionalLibraries.Add("opengl32.lib");
                              Still didn't work. I saw this example in OpenGL.Build.cs, in ThirdParty\OpenGL.

                              What I'm wondering now:
                              • Which of the various "opengl32.lib" should be used? A search in Windows reveals a bunch of them in folders like "Windows Kits" or "Microsoft SDKs".
                              • Must I specify the path using PublicLibraryPaths.Add(path) ?

                              Comment

                              Working...
                              X