Texture was being bound as a render target while still being set as a shader resource. D3D automatically unbinds the SRV in this case and generates a validation error. The fix is to manually unbind SRV, render into it and then re-bind to old slots.
Fixed include order and whitespace
This avoids most of the redundant API calls.