Overview
Last week I was creating a condition based pixel shader in HLSL
. The goal was to color only pixels which met a specific condition. In my case the condition was based on "time", which I say loosely because it's not what we consider traditional time. I was rendering a line in 3D space which is a trivial task. However, only a certain percentage of the line should be displayed based on the current user selected time.
I created a Line
class and added a SliderControl
to my form to allow the user to control the current point in time. This was simple enough to accomplish, as was setting up all of the underlying code.
The Issue
While I was creating the underlying code, I made a simple mistake with my time types. In the constant buffer for the current time, I used double
; within my Vertex
structures (arbitrary definition and input, along with pixel shader input), I used float
. This caused the condition in my pixel shader to always result in false
. The reason for the difference was that the original data is double
in type, but I ended up choosing float
to match everything else in the code, plus there was some kind of issue with using double
types in HLSL
.
The HLSL
The code was very straightforward:
cbuffer TimeBuffer : register(b4) {
double CurrentTime;
}
struct VertexInput {
float Time;
//...
}
struct PixelInput {
float Time;
//...
}
float4 PSMain(PixelInput input) : SV_Target {
float4 result = 0;
if (input.Time < CurrentTime)
result = input.Diffuse;
return result;
}
The Question
Why is the line never being rendered?