Glsl mod vs Hlsl fmod

I’ve implemented the spiral GLSL shader described in the a stackoverflow thread How to implement this rotating spiral in WebGL? in HLSL but the results are not the same and I think it’s because of the mod function in GLSL that I’ve translated to fmod in HLSL. I’ve tried to replace the call to mod by a custom call to a function that I’ve made which does what is described in the GSLS documentation (http://www.opengl.org/sdk/docs/manglsl/xhtml/mod.xml) and it works:

mod returns the value of x modulo y. This is computed as x – y * floor(x/y).

The fmod function according to msdn documentation http://msdn.microsoft.com/en-us/library/windows/desktop/bb509601(v=vs.85).aspx, says the following:

The floating-point remainder is calculated such that x = i * y + f, where i is an integer, f has the same sign as x, and the absolute value of f is less than the absolute value of y.

I’ve used the HLSL to GLSL converter hosted in http://sourceforge.net/projects/hlsl2glsl and the fmod function is translated as mod, however I don’t know if I can assume that mod translates to fmod.

My question is, what are the differences between the two functions and how can I translate the msdn cryptic description of fmod to a pseudo-code implementation.

I suspect that this problem only happens when we have negative numbers in the input of the fmod function

GLSL Shader:

uniform float time;
uniform vec2 resolution;
uniform vec2 aspect;

void main( void ) {
  vec2 position = -aspect.xy + 2.0 * gl_FragCoord.xy / resolution.xy * aspect.xy;
  float angle = 0.0 ;
  float radius = length(position) ;
  if (position.x != 0.0 && position.y != 0.0){
    angle = degrees(atan(position.y,position.x)) ;
  }
  float amod = mod(angle+30.0*time-120.0*log(radius), 30.0) ;
  if (amod<15.0){
    gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 );
  } else{
    gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );                    
  }
}

HLSL Shader:

struct Psl_VertexShaderInput
{
    float3 pos : POSITION;
};

struct Psl_VertexShaderOutput
{
    float4 pos : POSITION;

};

struct Psl_PixelShaderOutput
{
    float4 Output0 : COLOR0;
};

float3 psl_positionOffset;
float2 psl_dimension;

Psl_VertexShaderOutput Psl_VertexShaderFunction(Psl_VertexShaderInput psl_input)
{
    Psl_VertexShaderOutput psl_output = (Psl_VertexShaderOutput)0;

    psl_output.pos = float4(psl_input.pos + psl_positionOffset, 1);


    return psl_output;
}

float time : TIME;
float2 resolution : DIMENSION;


Psl_PixelShaderOutput Psl_PixelShaderFunction(float2 pos : VPOS)
{
    Psl_PixelShaderOutput psl_output = (Psl_PixelShaderOutput)0;

    float2 aspect = float2(resolution.x / resolution.y, 1.0);
    float2 position = -aspect.xy + 2.0 * pos.xy / resolution.xy * aspect.xy;
    float angle = 0.0;
    float radius = length(position);
    if (position.x != 0.0 && position.y != 0.0)
    {
        angle = degrees(atan2(position.y, position.x));
    }
    float amod = fmod((angle + 30.0 * time - 120.0 * log(radius)), 30.0);
    if (amod < 15.0)
    {
        psl_output.Output0 = float4(0.0, 0.0, 0.0, 1.0);
        return psl_output;
    }

    else
    {
        psl_output.Output0 = float4(1.0, 1.0, 1.0, 1.0);
        return psl_output;
    }

}

technique Default
{
    pass P0
    {
        VertexShader = compile vs_3_0 Psl_VertexShaderFunction();
        PixelShader = compile ps_3_0 Psl_PixelShaderFunction();
    }
}

I’ve no problem if instead of fmod I call the following function (per definition of the GLSL mod)

float mod(float x, float y)
{
  return x - y * floor(x/y)
}

GLSL equivalent of HLSL clip()?

The HLSL clip() function is described here. I intend to use this for alpha cutoff, in OpenGL. Would the equivalent in GLSL simply be if (gl_FragColor.a < cutoff) { discard; } Or is there some more

Is fmod equivalient to the % operator in HLSL

The following statement is valid HLSL: float3(300.0f,200.0f,100.0f) % 256.0f Apparently te % operator works on floating point numbers? How is that different from fmod? Is the following statement equi

Modelview Difference between GLSL and HLSL?

I’m wondering if there is a difference between GLSL and HLSL Mathematics. I’m using a selfmade Engine which works with openTK fine. My SharpDx Implementation gets everyday a bit further. I’m currently

glsl vector*matrix different to hlsl

I have two (identical) shaders, one in hlsl and one in glsl. In the pixel shader, I am multiplying a vector by a matrix for normal transformations. The code is essentially: HLSL float3 v = …; float3

converting hybrid cg/hlsl shaders to glsl es

I am looking for a tool that can convert cg/hlsl hybrid shaders to glsl es shaders. I have tried hlsl2glsl which doesnt understand the keyword ‘extern’ although it is a viable hlsl keyword and have lo

HLSL/GLSL Find range for integer

Assuming I have a few hundred varying size adjoining ranges 0-100,101-300,301-1000,1001-2000 etc. What would be the fastest way for me to find which range a given integer falls into using HLSL/GLSL? T

math.fmod always euqls math.mod?

I tried to figure out the difference between math.fmod and math.mod with the following code: a={9.5 ,-9.5,-9.5,9.5} b={-3.5, 3.5,-3.5,3.5} for i=1,#a do if math.fmod(a[i],b[i])~=math.mod(a[i],b[i]) th

GLSL Shader Ported From HLSL Is Not Working

I have this HLSL Shader for blur: struct VS_INPUT { float4 Position : POSITION0; float2 TexCoord : TEXCOORD0; float4 Color : TEXCOORD1; }; struct VS_OUTPUT { float4 Position : POSITION0; float4 Color

VS2010 – HLSL Intellisense?

Are there any free components out there that will give highlighting and/or intellisense for HLSL? I’ve seen mentions of something called InteliSense.Net, but the author’s site is down and I can’t fi

HLSL: predefined variables

does HLSL have predefined variables for common matrices like GLSL has? i am looking for gl_ProjectionMatrix and gl_ModelViewMatrix respectively? thanks!

Answers

As you’ve noted, they’re different. The GLSL mod will always have the same sign as y rather than x. Otherwise its the same — a value f such that x = i*y + f where i is an integer and |f| < |y|. If you’re trying to make a repeating pattern of some kind, the GLSL mod is generally what you want.

For comparison, the HLSL fmod is equivalent to x – y * trunc(x/y). They’re the same when x/y is positive, different when x/y is negative.