DirectComposition render to texture? - c++

I would like to have directcomposition render to a texture. Is this possible?
The reason for this is that I would like to be able to render a gpu accelerated windowless transparent flash player activex control to a texture. Something that is usually not possible, but which I hope to achieve with DirectComposition.

It's unlikely that this is possible, to quote MSDN (emphasis mine)
DirectComposition does not offer any rasterization services. An application must use some other software-based or hardware-accelerated rasterization library such as Direct2D or Direct3D to populate the bitmaps that are to be composed. After composing, DirectComposition passes composed bitmap content to Desktop Window Manager (DWM) for rendering to the screen.
As far as I know there are only official APIs to share your offscreen surfaces with DWM, but no API allowing you to get read-access to a DWM surface.
What DWM does allow you is redirecting HWND surfaces, so you can display the surfaces of other HWNDs on your window. This can be done either through DirectComposition (via CreateSurfaceFromHwnd) or the DWM API (via DwmRegisterThumbnail). For an example of the latter look here.
If you want to go the "hacking route" as indicated in your comment, there are undocumented APIs which look like they can give you access to the DWM surfaces, in particular DwmpDxGetWindowSharedSurface sounds promising. Someone else already did some reverse engineering and figured out the signature, but couldn't get it to work (texture works but renders black). This guy seems to have had more luck and was able to render window textures in 3d. I don't understand his language but you seem to have to use DwmpDxUpdateWindowSharedSurface (also undocumented).
You should be aware however that using undocumented functions is not a good idea, Microsoft can change them anytime (even in service pack releases) or remove them completely, since they are only used by Microsoft themselves they have no reason to maintain compatibility. Also there is a good chance that you are going to use them wrong (e.g. you might be missing necessary synchronization and cause random crashes, or worse).
However since the functionality is actually available there is hope that Microsoft may actually open it for puplic use in some future version of Windows.

Related

What does SDL_RenderClear do?

I'm Using the SDL library in C to learn some game dev , however i'm quite confused as to what SDL_RenderClear does and when do we use it. I did check the SDL documentation about the same , however i still wasn't able to understand where exactly we would use it and what is its use.
It's like rendering background color to color you have set via another API namely SDL_SetRenderDrawColor as you already found on documentation online.
Imagine you have rendered several things on screen. To begin again, you need to clear it to start over. Underlying of SDL_RenderClear wraps specific native platform your application runs on top, it can be OpenGL, DirectX, etc. It helps in communicating with such specific function that platform provides in order for you to flexibly clear your screen without a need to know low level functions, and use SDL2 for other things else like windowing, inputs from keyboard/mouse/joysticks, sound, and even utility functions related to rendering to aid your own rendering implementation.
To add a few more, SDL2 provides minimal but optimized rendering capability. SDL_RenderClear is one of those several functions you can use in rendering category. Anyway, you can decide to go on integrating with what you prefer i.e. OpenGL, DirectX, Vulkan etc. yourself.

How do I render a win32 window to an opengl texture?

I want to be able to render a win32 control inside an OpenGL texture. In order to be able to 3d transform it. Specifically I want to embed Internet explorer to show webpages and video inside a 3D transformed window.
How do I render a win32 window to a texture in a fast enough way (I need to keep 60 fps)?
There are a few ways you can go about it. In all cases, you will need to fetch a device context (DC) from the target window (GetDC, GetDCEx).
Then, you can either read the pixels directly (GetPixel); or blit the contents into your own DC (BitBlt) and then access that (e.g. GetDIBits).
Since you want to "embed" Internet Explorer, and by that you probably mean keeping its window hidden; you will want to consider how to make the above work on windows that are not visible, partially visible, etc. Consider as well your Windows version and/or whether the DWM might be disabled (and therefore the content of a window may not be available). Take a look at PrintWindow to force a WM_PAINT/WM_PRINT.
One small MSDN guide that can introduce you to all this and has a full example is: Capturing an Image.
In any case, you can always go for an alternative solution by embedding some other browser/engine as a library (e.g. Chromium Embedded Framework (CEF), Qt's WebEngine, Electron...).

Rendering a live preview of a background desktop in Windows

I have been looking at the DXGI Desktop Duplication Sample on msdn, which looks very nice but it isn't clear that it will work for what I want to do.
What I would like to do is to use the Win32 Desktop api's to create a new desktop (CreateDesktop) then switch to that desktop (SwitchDesktop) and then in that new desktop I would like to create a window and render a live preview of the original desktop.
Despite the name the DXGI api's appear to be more related to "screens" not Desktops, in the Win32 sense. Before I invest a lot of time attempting to implement this only to find out that it doesn't work I was hoping someone more knowledgeable of these api's could chime in and steer me in the right direction.
Previous attempts to do this with other common screen capture methods have resulted in failure as the images are always black. I am hoping DXGI will allow me to render non-active desktops into a DirectX texture which I can then display in my app.
I think the answer to this is that you can't do it. I have since abandoned this approach and now believe that the RDP api's are what I actually need to accomplish this. I think that in DXGI when they say "Desktop" they actually mean "Screen".
I haven't been able to figure out how to do this with the RDP api's yet either but I believe it is possible.

How to capture desktop on windows so that it would capture both directX and normally rendered parts of screen?

Simple example - on one side we see camera rendered via standard software rendered "Input" on other hand (labeled "Output") rendered via some directX stuff (at least it seems to me) :
So what function is provided by windows api or DirectX api for capturing such mixed scenes?
TightVNC Server can do it, you may want to look into what they are doing. From a simple glance through their source code it looks like they are creating a virtual screen that mirrors the primary screen.
Specifically though, look into the
CreateCompatibleDC and CreateDIBSection API's
As I known, there is not a direct way to capture DirectX render area, although we can see that on the screen. Because the real render action(aka render instruction) happens in hardware layer. So the API in standard SDK cannot know the finally render result, which lead to the black square.
The only way to do this maybe put your hope on the Render layer(such as DirectX engine) itself can support output interface as well as underlying render action. So I suggest to check some documentation to find if there indeed is.
DirectX can present to a limited subsection of the window that you give it, enabling you to create small regions of DX content in larger windows.

Handle Alt Tab in fullscreen OpenGL application properly

When trying to implement a simple OpenGL application, I was surprised that while it is easy to find plenty of examples and documentation on advanced rendering stuff, the Win32 framework is poorly documented and even most samples and tutorials do not implement this properly even for basic cases, not mentioning advanced stuff like multiple monitors. Despite of several hours of searching I was unable to find a way which would handle Alt-Tab reliably.
How should OpenGL fullscreen application respond to Alt-Tab? Which messages should the app react to (WM_ACTIVATE, WM_ACTIVATEAPP)? What should the reaction be? (Change the display resolution, destroy / create the rendering context, or destroy / create some OpenGL resources?)
If the application uses some animation loop, suspend the loop, then just minimize the window. If it changed display resolution and gamma revert to the settings before changing them.
There's no need to destroy OpenGL context or resources; OpenGL uses an abstract resource model: If another program requires RAM of the GPU or other resources, your programs resources will be swapped out transparently.
EDIT:
Changing the window visibility status may require to reset the OpenGL viewport, so it's a good idea to either call glViewport apropriately in the display/rendering function, or at least set it in the resize handler, followed by a complete redraw.