Another day, another Unity quirk work around. And yes, I thought something was odd when I hadn’t updated my blog in quite sometime… you could probably blame recent health problems and overall business work load since I’ve been in very high demand, both online and in the field fixing computers and other devices.
So I’ve been toying around with the “Progressive GPU (Preview)” option in the Unity 3D Lightmapper options that, well, makes your GPU do lighting calculations and related things. It’s much faster than your standard CPU-bound lightmapper, but it’s only in Preview stages for 2018.3 and probably will be fully featured in 2019.1.
The gotcha.
However, there’s a problem, and it’s a big gotcha – sometimes you will get OpenCL issues where you can’t find any OpenCL devices. This happened to me after I installed my AMD RX570 card after taking out the NVIDIA GeForce 1060 3GB card of mine. Symptoms on the AMD card include related lines to the following:
OpenCL Error. Falling back to CPU lightmapper. Error callback from context: CL_PLATFORM_NOT_FOUND_KHR
Failed to find a suitable OpenCL device, falling back to CPU lightmapper.
OpenCL Error. Falling back to CPU lightmapper. Error callback from context: CL_INVALID_PROGRAM
< lots of similar lines omitted for brevity >
[PathTracer] Loaded programs and built CL kernels in 0.004 secs -> Timestamps: [65.610 - 65.613].
[PathTracer] AddGeometry job with hash: 1e61de3339b1282b3cb23ee71e290694 failed with exit code 1.
The NVIDIA GPU would throw other errors about OUT_OF_RESOURCES or MEMORY_ALLOC_FAILURE. Nothing to see here…
Technical Analysis.
What seems to be the issue is that the OpenCL DLL file that Unity ships with is somewhat outdated, or it can’t correctly identify some devices. When I had my GeForce 1060 in the rig, it would always complain about being the OpenCL Kernel being unable to allocate memory and/or the CL code being an invalid program. But then it wouldn’t see the AMD card at all. Huh?
This was probably due to some versions of the nVidia drivers being garbage tier at OpenCL (buggy, regressions, or things just don’t work). So to work around this, I made sure the NVIDIA driver was not present on my system. I rebooted afterwards, made sure the latest AMD Radeon drivers were installed (Unity Tech recommends 18.9, but I said screw that and opted for the 19.3 beta I think) and gave it a test run.
The same errors above occurred, and after spending about a hour looking for answers, I started getting frustrated. But with that said, I saw there was an AMD OpenCL 64Bit DLL called “amd_opencl64.dll” in the extracted installer data files (Usually C:\AMD\…, somewhere there). Could this be the answer?
amd_opencl64.dll: The little DLL that was a superhero in disguise.
Immediately, I closed down Unity, making sure all instances are terminated. I then went about renaming the OpenCL.dll file inside my Unity installation folder to OpenCL.dll.bak. After I did that, I tried running Unity.exe with this file renamed, and I got an error saying that Execution cannot continue because I was missing OpenCL.dll and Unity would shut down. That’s OK – continue reading!
If you’re on Intel graphics, you might find Unity boots as normal and uses your Integrated GPU as the lightmapper’s OpenCL device. That’s fine and dandy, but for my case I’m using my AMD GPU, so as Administrator I opened Command Prompt and entered the following commands:
cd C:\Program Files\Unity\Hub\Editor\2018.3.7f\Editor
move OpenCL.dll OpenCL.dll.bak
copy C:\Drivers\AMD\Packages\Drivers\Display\WT6A_INF\B339766\amd_opencl64.dll "OpenCL.dll"
(if you get "Access is denied", you are not administrator or Unity is running.
Please note my installation folder is under the Hub folder, since Unity Hub is a great tool to use. The AMD Driver was extracted to C:\Drivers\AMD, you might have a different path.
From there I opened Unity up, loaded my project, then went into the light mapping inspector, chose “Progressive GPU (Preview)” and it picked up “Ellesmere” (my GPU’s codename) as a OpenCL device. Fuck yeah!
After I clicked Bake, it took a few moments get into gear and start cooking some sweet sweet light maps. Cooking the Opsive Ultimate Character Controller demo scene was about 20 to 30 minutes on my Ryzen 5 2600 CPU using Progressive CPU lightmapper.
On my RX 570 though… it was like 2 minutes at the most. Fucking FAST, I swear! Granted, I had my samples and stuff set down low since it’s just a simple test scene, but I was blown out of the water how fast it computed the light maps.
So yeah. This will only work for AMD GPUs and/or Intel GPUs. YMMV. I cannot support every configuration but I hope Unity Tech fixes this issue because that’s one hour of my life that I probably won’t get back running around like a lost chook…
Thanks for reading, and I hope this helps you out.
I’ve been plagued with this issue since 2018.3.5f1. I’ve been trying to get this issue to work in a small project to submit a bug report but it only does it at random times. I get the the error:
OpenCL Error . Falling back to CPU lightmapper . Error callback from context: CL callback from context: CL_MEM_OBJECT_ALLOCATION_FAILURE error executing CL_COMMAND_NDRANGE_KERNEL on GeForce GTX 1050 Ti (Device 0).
OpenCL Error . Falling back to CPU lightmapper . Error callback from context : CL_MEM_OBJECT_ALLOCATION_FAILURE
If only there a way around this one for the meanwhile.
Unfortunately that one seems to be caused by the nVidia Drivers themselves – Unity Tech I believe in a forum thread has stated that any newer driver than a specific 418.x (?) version is prone to cause random OpenCL problems. I guess this will be fixed in 2019 releases, but it’s a disappointment that 2018.3 and maybe 2018.4 won’t get the full release of the Progressive Lightmapper (which looks bloody good!).
You could try the paid “Bakery” lightmapper tool available on the Asset Store that is apparently loving nVidia hardware. Unfortunately it cannot use AMD hardware for baking since it uses nVidia OpenCL/CUDA SDKs, but it’s fast according to what I’ve seen from the forum threads.
But that does not take away the fact that a lightmapper should work. I think the problem lies on both sides of the field, both Unity with a borked OpenCL DLL, and nVidia with buggy OpenCL layers in their consumer drivers. That said, how much VRAM does your GeForce 1050 Ti have?
It sucks to hear that these versions of Unity won’t be getting the full GPU Lightmapper. Let alone a functional one. When it worked, the 10 minutes bake times compared to 5 hours w like a miracle. Who knows when 2019 will be releasing and let’s hope that it will be out of preview without any delay. Rolling back to previous drivers only made thing worse. By the way, my 1050ti has 4gb.
Yeah, same error with the GPU lightmapper release of 2019.2, my 11GB 1080ti spits it and falls back on a 77mb light map with only 5GB of GPU memory utilized from 2 objects in the scene, and damn the CPU lightmapper is just brutally slow by contrast.
Definitely. Even a high-end CPU will suffer under Unity’s Lightmapper. Even a 12-core Ryzen 2600 wasn’t enough to satisfy the lightmapper compared to the GPU accelerated light mapper.