Quick question on HLSL code.....

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Quick question on HLSL code.....

      So, I was arguing with a friend of mine about passing Vertex data, like Position and Normal, as float3 and not float4. My point of view is that it will save you memory and since the HLSL intrinsic functions are always inlined the performance hit "cannot" be noticeable.

      Ok, so you guys have a better understanding of what I'm talking about here is some code.

      float4 HPos vertexshader(float3 lPos : POSITION) : SV_POSITION
      {
      return mul( float4(lPos, 1.0f), WorldViewProjTransform);

      }

      On the other hand he is saying that it would be a lot better to pass in the float4 so you don't have to call the constructor for "every single" vertex that is pumped through the pipeline, like in the following example:

      float4 HPos vertexshader(float4 lPos : POSITION) : SV_POSITION
      {
      return mul(lPos, WorldViewProjTransform) ;
      }

      I'm not saying that his idea is totally wrong, I just think it's not worth copying all that data when we know the fourth component will always be 1.0f.

      Just looking for some unbiased opinions....
      Intel i7 3930k
      8GB Mushkin LP @ 2133 mhz
      GTX 680
      Asus Rampage IV Extreme
      Corsair 650w
    • RE: Quick question on HLSL code.....

      I'm not a graphics programmer by any stretch of the imagination, so I don't know the answer off-hand.

      What I can say is that if you're really concerned, you should do a performance test. Render a billion or so vertices through both versions of the shader and see what happens. Make sure you run it on ATI, nVidia, and Intel graphics cards for an accurate result. Shaders can have VERY different performance properties on different cards. What's fast on one can be slow on another and vice-versa.

      -Rez
    • RE: Quick question on HLSL code.....

      When verts are stored in meshes they are typically done so with three floats, not four - which will indeed save on memory both in external storage (important for consoles and mobile). It will also have a tendency to increase cache coherency, which in turn will absolutely increase performance.

      Converting from a float3 to a float4 in the shader is more expensive, but when compared to the benefits of the above it is an easy choice to make.

      And, I like Rez's idea - perform a side by side test to see how this really works in practice.
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • RE: Quick question on HLSL code.....

      Unless you have one of those rare "Board Stretchers!"
      Mr.Mike
      Author, Programmer, Brewer, Patriot