Shader compatibility

Last month, I wrote my first shader in HLSL. I managed to make it work on my android device, but I noticed that it didn’t appeared when I tried it on other device than the one I use to debug. I’m a big beginner with HLSL so the answer might be obvious. My shader starts like this :

 #if OPENGL
 #define SV_POSITION POSITION
 #define VS_SHADERMODEL vs_3_0
 #define PS_SHADERMODEL ps_3_0
 #else
 #define VS_SHADERMODEL vs_4_0_level_9_1
 #define PS_SHADERMODEL ps_4_0_level_9_1
 #endif`

Does my problem have to do with the versions of VS_SHADERMODEL and PS_SHADERMODEL used? Or is it unrelated?

If both devices are OpenGL based and you’re using the openGL build, then it should work on both so I’d say probably not related to that. What’s the other device? Perhaps too there’s a clue in other aspects of the code.

In your shader you are using shader mode v3 for GL and Level9_1 for DX.
The models are more or less like this:

(GL) = (DX)
2_0 = 4_0_level_9_1
3_0 = 4_0_level_9_3
4_0 = 4_0

what’s the card on the other mechine? is it possible to have a very old GPU or doesn’t have up-to-date gl drivers?
is your shader too long or use many data?

You can change the version to vs_2_0/ps_2_0 but the processor doesn’t do any validation for GL shaders.
You can try to change the target platform to Windows, and see if the directX compiler gives any errors.

The shader didn’t work on both older and newer device, so I don’t think it’s linked to old GL drivers.
Thanks for the GL/DX equivalent sum-up, I’ll make sure I use equivalent shaders mode (but it doesn’t fix my issue).

Here’s the full code if it can help but I 'm not sure I see how it would have an impact on device compatibility

#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
    #define PS_SHADERMODEL ps_3_0
#else
    #define VS_SHADERMODEL vs_4_0_level_9_1
    #define PS_SHADERMODEL ps_4_0_level_9_1
#endif

Texture2D SpriteTexture;
sampler2D SpriteTextureSampler = sampler_state
{
    Texture = <SpriteTexture>;
};
float spritesheet_offset_x;
float spritesheet_offset_y;
float spritesheet_width;
float spritesheet_height;
float frame_number_x;
float frame_number_y;
float scroll;

Texture2D jam_texture;
sampler2D jam_sampler = sampler_state
{
    Texture = <jam_texture>;
};

struct VertexShaderOutput
{
    float4 Position : SV_POSITION;
    float4 Color : COLOR0;
    float2 TextureCoordinates : TEXCOORD0;
};

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR
{
    float4 pixel_color = tex2D(SpriteTextureSampler, input.TextureCoordinates);
    if (pixel_color.b * 255 != float(62))
        return pixel_color;
    else
    {
        float2 textureposition = input.TextureCoordinates;
        textureposition.x *= frame_number_x;
        textureposition.y *= frame_number_y;
        textureposition.x -= (spritesheet_offset_x / spritesheet_width) * frame_number_x;
        textureposition.y -= (spritesheet_offset_y / spritesheet_height) * frame_number_y;
        textureposition.y -= scroll;
        float4 jam_color = tex2D(jam_sampler, textureposition);
        return jam_color;
    }
}


technique SpriteDrawing
{
    pass
    {
        PixelShader = compile PS_SHADERMODEL PixelShaderFunction();
    }
};
1 Like