<div dir="ltr"><div>I see a few downsides with the proposed callback:<br></div><div>- feels like a solution too tailored for WINE</div><div>- there's a layering violation: the application suddenly takes the driving seat for a thing deep down in the GL driver</div><div>so I fear Mesa community might regret it doing, and once WINE supports there would be outcry if to go back.</div><div><br></div><div><br></div><div>IIUC the problem at hand, another way to go about this would be an extension that allows applications to get a malloc'ed/valloc'ed memory exposed to the GPU as a GL buffer object.</div><div><br></div><div><div>I feel this would be potentially useful to applications other than just WINE, especially on systems with unified memory. And there have been extensions along these lines before, for example, <a href="https://registry.khronos.org/OpenGL/extensions/AMD/AMD_pinned_memory.txt">https://registry.khronos.org/OpenGL/extensions/AMD/AMD_pinned_memory.txt</a></div><div><br></div><div>There's also NVIDIA's Heterogeneous Memory Management, which takes this idea to a whole different level:<br></div></div><div>- <a href="https://developer.nvidia.com/blog/simplifying-gpu-application-development-with-heterogeneous-memory-management/">https://developer.nvidia.com/blog/simplifying-gpu-application-development-with-heterogeneous-memory-management/</a></div><div>- <a href="https://www.kernel.org/doc/html/v5.0/vm/hmm.html">https://www.kernel.org/doc/html/v5.0/vm/hmm.html</a><br></div><div>- <a href="https://lpc.events/event/2/contributions/70/attachments/14/6/hmm-lpc18.pdf">https://lpc.events/event/2/contributions/70/attachments/14/6/hmm-lpc18.pdf</a></div><div>- <a href="https://lwn.net/Articles/752964/">https://lwn.net/Articles/752964/</a></div><div>- <a href="https://lwn.net/Articles/684916/">https://lwn.net/Articles/684916/</a></div><div><br></div><div>But I remember that Thomas Hellstrom (while at VMware, now Intel) once prototyped this without HMM, just plain DRM. I think HMM provides the ability to do this transparently for application, which is above and beyond what's strictly needed for WINE.</div><div><br></div><div>Metal API also provides this -- <a href="https://developer.apple.com/documentation/metal/mtldevice/1433382-newbufferwithbytesnocopy?language=objc">https://developer.apple.com/documentation/metal/mtldevice/1433382-newbufferwithbytesnocopy?language=objc</a><br></div><div><br></div><div>Jose<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 18, 2024 at 11:10 PM Derek Lesho <<a href="mailto:dlesho@codeweavers.com" target="_blank">dlesho@codeweavers.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hey everyone 👋,<br>
<br>
I'm Derek from the Wine project, and wanted to start a discussion with <br>
y'all about potentially extending the Mesa OGL drivers to help us with a <br>
functionality gap we're facing.<br>
<br>
Problem Space:<br>
<br>
In the last few years Wine's support for running 32-bit windows apps in <br>
a 64-bit host environment (wow64) has almost reached feature completion, <br>
but there remains a pain point with OpenGL applications: Namely that <br>
Wine can't return a 64-bit GL implementation's buffer mappings to a 32 <br>
bit application when the address is outside of the 32-bit range.<br>
<br>
Currently, we have a workaround that will copy any changes to the <br>
mapping back to the host upon glBufferUnmap, but this of course is slow <br>
when the implementation directly returns mapped memory, and doesn't work <br>
for GL_PERSISTENT_BIT, where directly mapped memory is required.<br>
<br>
A few years ago we also faced this problem with Vulkan's, which was <br>
solved through the VK_EXT_map_memory_placed extension Faith drafted, <br>
allowing us to use our Wine-internal allocator to provide the pages the <br>
driver maps to. I'm now wondering if an GL equivalent would also be seen <br>
as feasible amongst the devs here.<br>
<br>
Proposed solution:<br>
<br>
As the GL backend handles host mapping in its own code, only giving <br>
suballocations from its mappings back to the App, the problem is a <br>
little bit less straight forward in comparison to our Vulkan solution: <br>
If we just allowed the application to set its own placed mapping when <br>
calling glMapBuffer, the driver might then have to handle moving buffers <br>
out of already mapped ranges, and would lose control over its own memory <br>
management schemes.<br>
<br>
Therefore, I propose a GL extension that allows the GL client to provide <br>
a mapping and unmapping callback to the implementation, to be used <br>
whenever the driver needs to perform such operations. This way the <br>
driver remains in full control of its memory management affairs, and the <br>
amount of work for an implementation as well as potential for bugs is <br>
kept minimal. I've written a draft implementation in Zink using <br>
map_memory_placed [1] and a corresponding Wine MR utilizing it [2], and <br>
would be curious to hear your thoughts. I don't have experience in the <br>
Mesa codebase, so I apologize if the branch is a tad messy.<br>
<br>
In theory, the only requirement from drivers from the extension would be <br>
that glMapBuffer always return a pointer from within a page allocated <br>
through the provided callbacks, so that it can be guaranteed to be <br>
positioned within the required address space. Wine would then use it's <br>
existing workaround for other types of buffers, but as Mesa seems to <br>
often return directly mapped buffers in other cases as well, Wine could <br>
also avoid the slowdown that comes with copying in these cases as well.<br>
<br>
Why not use Zink?:<br>
<br>
There's also a proposal to use a 32-bit PE build of Zink in Wine <br>
bypassing the need for an extension; I brought this to discussion in <br>
this Wine-Devel thread last week [3], which has some arguments against <br>
this approach.<br>
<br>
<br>
If any of you have thoughts, concerns, or questions about this potential <br>
approach, please let me know, thanks!<br>
<br>
1: <a href="https://gitlab.freedesktop.org/Guy1524/mesa/-/commits/placed_allocation" rel="noreferrer" target="_blank">https://gitlab.freedesktop.org/Guy1524/mesa/-/commits/placed_allocation</a><br>
<br>
2: <a href="https://gitlab.winehq.org/wine/wine/-/merge_requests/6663" rel="noreferrer" target="_blank">https://gitlab.winehq.org/wine/wine/-/merge_requests/6663</a><br>
<br>
3: <a href="https://marc.info/?t=172883260300002&r=1&w=2" rel="noreferrer" target="_blank">https://marc.info/?t=172883260300002&r=1&w=2</a><br>
<br>
</blockquote></div>
<br>
<span style="background-color:rgb(255,255,255)"><font size="2">This electronic communication and the information and any files transmitted with it, or attached to it, are confidential and are intended solely for the use of the individual or entity to whom it is addressed and may contain information that is confidential, legally privileged, protected by privacy laws, or otherwise restricted from disclosure to anyone else. If you are not the intended recipient or the person responsible for delivering the e-mail to the intended recipient, you are hereby notified that any use, copying, distributing, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited. If you received this e-mail in error, please return the e-mail to the sender, delete it from your computer, and destroy any printed copy of it.</font></span>