What would be a solution for cases when you need ...
# compose-desktop
r
What would be a solution for cases when you need to render some 3D scene (using OpenGL) inside a compose widget? I've seen lwjgl integration example, it was very helpful with understanding how to use CfD in opengl applications and I plan to use it in my solution. However, I need to somehow get callback every frame of compose rendering (from Skia?) when my specific widget is shown. It would be ideal if it was possible to render directly into active skia's framebuffer without any unneccessary image copies. So far I've only been able to find this example of 3D rendering inside of compose: https://github.com/romainguy/sample-materials-shop, but it is specific for Android and does some trickery with AndroidView, I need something similiar for desktop.
e
This will be a bit tricky, because OpenGL requires an OpenGL context, and creating a context is platform specific. My first question is which OpenGL API you're using? I.e. Do you intend to write OpenGL C code and use JNI? or use a Java wrapper? The first step you'd want to look into is to see how you can create an OpenGL context from a java swing context. In the end what you probably want is something similar to Android GLSurfaceView, but that works on deskop. However, this is extra tricky since not all OSes have OpenGL Drivers, most notably MacOS
r
To reduce ambiguity, let's assume that Windows is the only target platform. Application itself is written in java and uses LWJGL as OpenGL wrapper, and also relies on glfw's context creation abstraction. For now, I am using code from this example - https://github.com/JetBrains/compose-jb/blob/master/experimental/lwjgl-integration/src/main/kotlin/main.kt for GLFW, OpenGL and Skia initialization and rendering. So my question boils down to achieving something similiar to GLSurfaceView. I actually was hoping that it already exists and I just missed while searching the api.
r
Filament works on other platforms
But you'd need to build the JVM integration
We used to have one you could get inspiration from that
Basically it's using JNI to get the native pointer of an awt component
Note that you can't just share the Skia back buffer (or at least not easily)
Here's our JVM code:
e
@redenergy I think it's safe to say that you haven't missed any API. So, not matter which approach you'll choose, you will have to do something novel
r
There are two techniques: rendering directly into an awt window or copying to a buffered image
r
@romainguy I do not intend to use filament specifically, your project with filament integration inside of compose was just a pretty good example of what I want to achieve. I guess my question is more about sharing framebuffer with skia and accessing it for rendering inside the widget. I won't use awt so drawing to it's window is not really an option, and copying to buffered image on cpu is really wasteful, would like to avoid it.
r
I would not try to share the Skia buffer, for a number of reasons. I was pointing to our code as an example because you usually either want to render in a separate surface (an AWT component for instance, if Compose for Desktop provides interop which I assume it does) or you need to do a composition step using some kind of shared texture. I don't think CfD exposes anything for this yet
r
@SrSouza I apologize in advance for pinging. I've found your discussion in which you've tried to solve basically the same problem (rendering OpenGL inside of compose component) and wanted to ask if you've managed to solve it somehow.
One possible solution could be implemented by using lwjgl3-awt integration but it can't be used if compose is not executed inside of AWT window. At least that was my impression since SwingPanel, which would be used to instantiate AWTGLCanvas, requires owning awt Container when created and I am trying to use compose for UI in OpenGL application.