It happens to me. I laughed hysterically while being raped once. I laughed at my daughter while my husband was cleaning a painful wound on her shoulder.
I am not dissociative at those moments either. I think I am depersonalizing. It is as if I'm watching a movie. In movies, there are a ton of scenarios that are funny because we're watching them from the outsider's perspective. We laugh at people tripping and falling. We laugh at all kinds of terrible things, as long as we can detach our feelings of concern and empathy from the characters. A lot of times, the show's creators help us do that by demonstrating that the character is invincible... so, it's okay to laugh.
I think it is due to depersonalization, and you should discuss that with your Therapist, or do some research about it.
I think... leading up to it is a feeling of helplessness, and then I can't imagine what I am "supposed" to be thinking. Like, when my daughter was injured and Husband was tending to her wound... I couldn't think of what I should be doing. She didn't want to be touched or held. She just cried and begged us to stop, which was very upsetting. I was trying not to dissociate. I tried to "think of something else". I tried to distract her too, but she wasn't distracted. I tuned her out, and then heard her from the perspective of an outsider. Hence the laughter.
Hope this helps you figure out what you're doing, and why.