Pixelshader does not run - invalid SV_POSITION coordinates

Started by
18 comments, last by IonThrust 6 years, 1 month ago

Hello,

I'm currently working on a visualisation program which should render a low-poly car model in real-time and highlight some elements based on data I get from a database. So far no difficulty here. So far I managed to set up my UWP project so that I have a working DirectX 11 device and real-time swapchain-based output.

When I debug my Input Assembler stage shows a valid model - the whole thing is correctly rendered in the VS Graphics Analyzer. The Vertex Shader output seems okay, too - but it's likely that the camera view-matrix is a bit wrong (too close, maybe the world matrix is rotated wrong as well) but the VS-GA does show some output there. Now the Pixel Shader does not run, though. My assumption is, that the coordinates I get from the Vertex Shader are bad (high values which I guess should actually be between -1.0f and 1.0f). So obviously the Rasterizer clips all vertices and nothing ends up rendered.

I've been struggling with this for days now and since I really need to get this working soon I hoped someone here has the knowledge to help me fix this.

Here's a screenshot of the debugger:

VSGA.thumb.png.00cb03758d8e94c254087c707a86c273.png

I'm currently just using a simple ambient light shader (see here).

And that's my code for rendering. The model class I'm using simply loads the vertices from a STL-file and creates the vertex buffer for it. It sets it and renders the model indexed or not (right now I don't have a index buffer since I haven't figured out how I calculate the indices for the model ... but that'll do for now).


public override void Render()
{
    Device.Clear(Colors.CornflowerBlue, DepthStencilClearFlags.Depth, 1, 0);

    _camera.Update();

    ViewportF[] viewports = Device.Device.ImmediateContext.Rasterizer.GetViewports<ViewportF>();
    _projection = Matrix.PerspectiveFovRH((float) Math.PI / 4.0f, viewports[0].Width / viewports[0].Height,
        0.1f, 5000f);
    _worldMatrix.SetMatrix(Matrix.RotationX(MathUtil.DegreesToRadians(-90f)));
    _viewMatrix.SetMatrix(_camera.View);
    _projectionMatrix.SetMatrix(_projection);

    Device.Context.InputAssembler.InputLayout = _inputLayout;
    Device.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

    EffectTechnique tech = _shadingEffect.GetTechniqueByName("Ambient");
    for (int i = 0; i < tech.Description.PassCount; i++)
    {
        EffectPass pass = tech.GetPassByIndex(i);
        pass.Apply(Device.Context);
                
        _model.Render();
    }
}

The world is rotated around the X-axis since the model coordinate system is actually a right-handed CS where X+ is depth and Y is the horizontal and Z the vertical coordinate. Couldn't figure out if it was right that way, but should be theoretically.


public void Update()
{
    _rotation = Quaternion.RotationYawPitchRoll(Yaw, Pitch, Roll);
    Vector3.Transform(ref _target, ref _rotation, out _target);

    Vector3 up = _up;
    Vector3.Transform(ref up, ref _rotation, out up);

    _view = Matrix.LookAtRH(Position, _target, up);
}

The whole camera setup (static since no input has been implemented) is for now:


_camera = new Camera(Vector3.UnitZ);
_camera.SetView(new Vector3(0, 0, 5000f), new Vector3(0, 0, 0), MathUtil.DegreesToRadians(-90f));

So I tried to place the camera above the origin, looking down (thus the UnitZ as up-vector).

 

 

So can anybody explain me why the vertex shader ouput is so wrong and obviously all vertices get clipped?

Thank you very much!

Advertisement

There are a few places where you could check. 

  • Are you setting a correct viewport? A zero size viewport will result in non invoking the pixel shader.
  • Is your rasterizer state set up correctly? Double check especially cullmodes, try with CULL_NONE.
  • Are you using any scissor rectangles? Double check that they are correctly set.
  • Is your vertex shader actually correct? Does it output vertices in z range [0,1]?
  • Does enabling the debug layer warn you about anything?

Good luck!

Quote

Are you setting a correct viewport? A zero size viewport will result in non invoking the pixel shader.

I did as one of the first things. But I can ensure that the viewport is correct. It's about 1400x800.

 

11 minutes ago, turanszkij said:

Is your rasterizer state set up correctly? Double check especially cullmodes, try with CULL_NONE.

Yes, I checked this. Moreover it's set in the pass of the effect shader explicitly.

 

11 minutes ago, turanszkij said:

Are you using any scissor rectangles? Double check that they are correctly set.

I do not. Don't need them in this scenario and I know this "trap" so I disabled them from the start of.

 

11 minutes ago, turanszkij said:

Is your vertex shader actually correct? Does it output vertices in z range [0,1]?

Not really - as you can see in the image I attached. All components of the SV_Position output are >1. But the vertex shader only does the basic matrix multiplication. So there can't be much wrong unless the matrices themselves are wrong. This is my number one thing, but I don't get what could be wrong ...

 

11 minutes ago, turanszkij said:

Does enabling the debug layer warn you about anything?

Nope, everything clear.

 

Just tried to manually create the view matrix with fixed values. Now I do get values clamped between 0 and 1 for z and w but 1.14 and -0.5 for x and y... Still no pixel shader running.


_viewMatrix.SetMatrix(Matrix.LookAtRH(new Vector3(500f, 500f, -1000f), new Vector3(500f, 500f, 0f), Vector3.Up));

 

What happens if you turn off depth clipping in the rasterizer state? 

I already disabled depth-stencil because I initially thought this would be the problem. But it seems like the rasterizer still clips them... the mysterious thing is I can't even find any source on the internet stating why "State did not run. No output." shows up at the Pixel Shader Stage. This is rather unusual.

So far I've managed to move the camera so that I actually see the whole (correctly rendered) model in the preview of the vertex shader stage. So now I really am confused why the pixel shader won't run...

Note that I didn't mean depth testing, but RASTERIZER_DESC::DepthClipEnable = FALSE actually means bypassing the check 0<=fragmentZ<=1. But turning off depth stencil was also a good idea by the way.

23 minutes ago, IonThrust said:

So there can't be much wrong unless the matrices themselves are wrong. This is my number one thing, but I don't get what could be wrong

Have you checked the matrix by hand?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

8 minutes ago, turanszkij said:

Note that I didn't mean depth testing, but RASTERIZER_DESC::DepthClipEnable = FALSE

Ah sorry. Misunderstood that. I forgot and disabled it now, but the PS stage still won't run.

Meanwhile I do get the following result in the debugger (at least something):VSGA2.thumb.png.c5078373299e9510b2bf897f5f7ad915.png

Based on the view I can ensure that the view would be absolutely correct. It was intentional to render the front of the car. But somehow no matter if I set the X position to -1000f or -1000f I do get the same result, like this was the maxmimum I can move away from the car ... I expected it to be fully visible (even small) with -10000f. Nonetheless the World-View seems correct. Question is: Why is there still no PS stage running?

I'm absolutely lost here...

World:


    Row1: {X:1 Y:0 Z:0 W:0}
    Row2: {X:0 Y:-4,371139E-08 Z:-1 W:0}
    Row3: {X:0 Y:1 Z:-4,371139E-08 W:0}
    Row4: {X:0 Y:0 Z:0 W:1}

View:


    Row1: {X:0 Y:0 Z:-1 W:0}
    Row2: {X:0 Y:1 Z:0 W:0}
    Row3: {X:1 Y:0 Z:0 W:0}
    Row4: {X:-250 Y:0 Z:-10000 W:1}

Projection


    Row1: {X:1,434897 Y:0 Z:0 W:0}
    Row2: {X:0 Y:2,414213 Z:0 W:0}
    Row3: {X:0 Y:0 Z:-1,00002 W:-1}
    Row4: {X:0 Y:0 Z:-0,100002 W:0}

I'm not sure if this is correct. I'm lacking the background knowledge for this to validate... And it's been years since I actively worked with DirectX / game dev, so I'm a bit rusty. But I'm doing everything rather standard-ish, so it shouldn't be so hard...

Well, in the world-matrix, are there supposed to be such low values as -4,371139E-08? Seems like some error to me. Also, is your pixel shader the simplest it can be? I mean that it shouldn't have any discard or clip operations and output a simple float4(1,0,0,1) color to SV_Target0?

Just take a look at the shader (relatively at the start of the post I linked it on pastebin). Not the most simple one, but it basically just outputs the input color. At least it would output something, but since it never even runs it doesn't matter anyway.

For the world matrix: All I do is using Matrix.RotationX(MathUtil.DegreesToRadians(-90f)); so it should be correct. Else it would be a bug on SharpDX's side which I doubt. And as you can see in the debugger: It seems to be the right matrix, or why else would it render it correctly positioned and rotated?

EDIT: I just tried setting the X-position of the view matrix to 0f: Everything's black in the VS output. Set to 500f: A closer shot than in the screenshot above but everything upside-down. Set to 1000f it's a bit further away and not upside-down.

Hell, I'm not good at maths but what the heck is going on here? Why does the view matrix swap when I change the X coordinate of the camera? It just doesn't make any sense to me...

This topic is closed to new replies.

Advertisement