Edit: Added images at the bottom to show the current issue.

I have the following few lines in my hlsl pixel shader where color and specular are both float4.

color = color * textureColor;

// Add specular component and clamp
color = saturate(color + specular);

Running VS2013 graphics debugger and debugging into the pixel shader I see specular coming through as (0.0f, 0.0f, 0.0f, 0.0f) which I expect for the pixel I'm checking since it's not within the specular portion of the viewDirection, but the float4 addition is making the final color as zero after the addition.

Am I completely missing something here? I would assume the following math:

   color (0.65f, 0.0f, 0.0f, 1.0f)
 + specular (0.0f, 0.0f, 0.0f, 0.0f)

   result = (0.65f, 0.0f, 0.0f, 1.0f)

Any thoughts would be greatly appreciated. I have a feeling, I'm missing something really simple here.


 // If I remove the specular and return color after multi by tex Sample,
 // I get the following expected result
 color = color * textureColor;

Result with no specular

// As soon as I add the specular component back in
color = saturate(color + specular);

Result with specular

  • \$\begingroup\$ Please don't cross-post between SE sites. You should be able to delete your SO question since it has no answers; if not, please flag it for a moderator to move or delete. \$\endgroup\$ – user1430 Mar 10 '14 at 18:20
  • 2
    \$\begingroup\$ Thank you for the tip. I've deleted it off SO as I feel it is more appropriate here. Slowly learning proper SE etiquette. \$\endgroup\$ – Mebourne Mar 10 '14 at 22:25
  • 1
    \$\begingroup\$ How are you calculating diffuse and specular? You may need to clamp them individually before adding them: saturate(color + specular) -> saturate(color) + saturate(specular). \$\endgroup\$ – GuyRT Mar 13 '14 at 10:44
  • \$\begingroup\$ Clearly, your specular is not a 0 vector, but contains negative numbers. The specular is zero on the backside of the sphere ( from the lights' view ) as seen on the last two images. \$\endgroup\$ – János Turánszki Mar 14 '14 at 0:45
  • \$\begingroup\$ @JánosTuránszki I agree since the far right is showing the unlit red, but VS2013 Visual Graphics Debugger is still showing the specular vector come through as all 0.0f. \$\endgroup\$ – Mebourne Mar 14 '14 at 3:08

Finally got some good time to go back and study this in more depth. In the end it was two simple issues. First my shader constant buffer variable order didnt match the struct I was using to map. Once this was corrected the black portion was corrected but there was still the large solid white spot being calculated versus the small expected specular glow. This was corrected by dividing the view direction vector by its length for normalization.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.