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.
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…
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.