I had the same problem yesterday… I spent like an hour trying to update my video card driver, update directx, change mame64.exe version… until I realise that turning HLSL on and off actually caused this error.
You just have to copy the HLSL folder (version 202) over the old one and that's it…. mine was maybe 199 or 200.