[HLSL] Decreasing Shader Model from SM5 to SM2 (vs_5_0 to vs_4_0_level_9_3)

Started by
26 comments, last by dmtuan 9 years ago


This post shows what the various transformation matrices look like. You can see the bottom row is always (0, 0, 0, 1).

So it looks like uniform scaling or not doesn't really matter in this case.

Ok... so it seems I just have to take the first 3 rows, send them as 3 float4's to the vertex shader.. and in the shader I will reconstruct the original matrix, since I know the last row is always (0, 0, 0, 1)...

Advertisement

Ah, I have another issue. If you look at the Vertex Shader I posted in the 1st post here.. can any of you think of any functions or instructions, that are not valid in Shader Model 2? Namely, Shader Model vs_4_0_level_9_3.

I am experiencing this strange thing. When I run the program in the emulator, the character loads fine and animates fine. But when I do it in a real device, the character is not animating. I suspect the problem is in the SkinVertex() function.. but I cannot pin point exact problem. In the device, the character is moving in space in a rotation-like trajectory, but it stays in TPose. It is suppose to be standing and waving his hand.

Debugging in the emulator is not accurate, since it might support some high level functions, even thou the engine is set on only feature level 9_3, shader model 2.. at least I read so on msdn.

Have no experience with real WP devices, so I can't tell you for sure, but maybe you're allowed to use only one constant buffers, see if MSDN cuold give us something on that topic?


What's the error? SV_Position is a DX10 and above thing, maybe that's the problem? My guess is that something in your layout is not supported in the feature level you're using.

Hi, could u please help me to rewrite my Vertex Shader and Pixel Shader, so that they do not use SV_Position and SV_Target? It seems to be like you said. SV_Position and SV_Target are not supported in Shader Model 2.

Here is the snippet from my hlsl code:


// Vertex Shader input structure (from Application)
struct VertexShaderInput
{
    float4 Position : SV_Position;// Position - xyzw
    float3 Normal : NORMAL;    // Normal - for lighting and mapping operations
    float4 Color : COLOR0;     // Color - vertex color, used to generate a diffuse color
    float2 TextureUV: TEXCOORD0; // UV - texture coordinate
    uint4 SkinIndices : BLENDINDICES0; // blend indices
    float4 SkinWeights : BLENDWEIGHT0; // blend weights
};

// Pixel Shader input structure (from Vertex Shader)
struct PixelShaderInput
{
    float4 Position : SV_Position;
    // Interpolation of combined vertex and material diffuse
    float4 Diffuse : COLOR;
    // Interpolation of vertex UV texture coordinate
    float2 TextureUV: TEXCOORD0;

    // We need the World Position and normal for light calculations
    float3 WorldNormal : NORMAL;
    float3 WorldPosition : WORLDPOS;
};

PixelShaderInput VSMain(VertexShaderInput vertex)
{
    PixelShaderInput result = (PixelShaderInput)0;

    // Apply vertex skinning if any
    SkinVertex(vertex.SkinWeights, vertex.SkinIndices, vertex.Position, vertex.Normal);

    result.Position = mul(vertex.Position, WorldViewProjection);
    result.Diffuse = vertex.Color * MaterialDiffuse;
    // Apply material UV transformation
    result.TextureUV = mul(float4(vertex.TextureUV.x, vertex.TextureUV.y, 0, 1), (float4x2)UVTransform).xy;

    // We use the inverse transpose of the world so that if there is non uniform
    // scaling the normal is transformed correctly. We also use a 3x3 so that 
    // the normal is not affected by translation (i.e. a vector has the same direction
    // and magnitude regardless of translation)
    result.WorldNormal = mul(vertex.Normal, (float3x3)WorldInverseTranspose);
    
    result.WorldPosition = mul(vertex.Position, World).xyz;
    
    return result;

}

float4 PSMain(PixelShaderInput pixel) : SV_Target
{
    // Normalize our vectors as they are not 
    // guaranteed to be unit vectors after interpolation
    float3 normal = normalize(pixel.WorldNormal);
    float3 toEye = normalize(CameraPosition - pixel.WorldPosition);
    float3 toLight = normalize(-Light.Direction);

    // Texture sample here (use white if no texture)
    float4 sample = (float4)1.0f;
    if (HasTexture)
        sample = Texture0.Sample(Sampler, pixel.TextureUV);

    float3 ambient = MaterialAmbient.rgb;
    float3 emissive = MaterialEmissive.rgb;
    float3 diffuse = Lambert(pixel.Diffuse, normal, toLight);
    float3 specular = SpecularBlinnPhong(normal, toLight, toEye);

    // Calculate final color component
    float3 color = (saturate(ambient+diffuse) * sample.rgb + specular) * Light.Color.rgb + emissive;
    // We saturate ambient+diffuse to ensure there is no over-
    // brightness on the texture sample if the sum is greater than 1
    
    // Calculate final alpha value
    float alpha = pixel.Diffuse.a * sample.a;

    // Return result
    return float4(color, alpha);

}

I am not sure if it's ok just to replace SV_position with POSITION or POSITION0 or if I need to make any other adjusments...

I tried to change line 4 to float4 Position : POSITION;// Position - xyzw

Line 15 to float4 Position : VPOS;

And line 50 to float4 PSMain(PixelShaderInput pixel) : COLOR

But when I try to compile these shaders with fxc.exe, I get this error:

error X4541: veretex shader must minimally write all four components of SV_position

I don't know how to deal with this...


Hi, could u please help me to rewrite my Vertex Shader and Pixel Shader, so that they do not use SV_Position and SV_Target?

The DX9 equivalent for SV_POSITION as an input to the pixel shader is VPOS:

https://msdn.microsoft.com/en-us/library/windows/desktop/bb509647%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396#VPOS


error X4541: veretex shader must minimally write all four components of SV_position

I'm confused... if you're getting errors about SV_POSITION, that implies you're compiling your shaders for DX10 and above? What parameters are you passing to fxc?

I'm confused... if you're getting errors about SV_POSITION, that implies you're compiling your shaders for DX10 and above? What parameters are you passing to fxc?

fxc VS.hlsl /Fo D:\VS.fxo /E VSMain /T vs_4_0_level_9_3

I think this is ok, isn't it?

Anyway, I have been digging a littlebit more and I found out, that SV_Position is actually supported in vs_4_0_level_9_3, as they say here:

https://msdn.microsoft.com/en-us/library/windows/apps/jj714072(v=vs.105).aspx

Now I am really confused. I have this problem. I tested the engine in an emulator and in a real device. The same code, but the result is not the same. Which is really confusing and I don't know where else to look for a mistake. When I run the engine in an emulator, the character is animating right. There is a man, he is standing and waving his hand (see Screenshot_3). But when I run the exact same code in the device, I get something different. The character is not actually skinning, it's not doing the animation. It stays in TPose and moving in space in a rotation-like movement (see Screenshot_4).

I was going through the Shader code, hoping I would find the reason why it's behaving like this. If it was doing the same in the emulator and the device, I would have been understandable a little... but this? I really hit the dead end. I've been stuck at this for a week and I am starting to get really desperate sad.png.

This topic is closed to new replies.

Advertisement