Search code examples
openglglsldirectxhlsldirect3d

GLSL to HLSL issues


I'm trying to import many transitions from GL Transitions into my video sequencer by converting GLSL to HLSL.

For example, this simple cross fade:

vec4 transition (vec2 uv) {
  return mix(
    getFromColor(uv),
    getToColor(uv),
    progress
  );
}

is correctly translated in my HLSL code:

#define D2D_INPUT_COUNT 2
#define D2D_INPUT0_SIMPLE
#define D2D_INPUT1_SIMPLE
#define D2D_REQUIRES_SCENE_POSITION // The pixel shader requires the SCENE_POSITION input.
#include "d2d1effecthelpers.hlsli"

cbuffer constants : register(b0)
{
    float progress : packoffset(c0.x);
...
}


float4 crossfade(float4 v1,float4 v2)
{
    return lerp(v1, v2, progress);    
}



D2D_PS_ENTRY(main)
{

   float4 v1 = D2DGetInput(0);
   float4 v2 = D2DGetInput(1);
   return crossfade(v1,v2);
}

The same doesn't work for Wind effect:

// Custom parameters
uniform float size; // = 0.2

float rand (vec2 co) {
  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

vec4 transition (vec2 uv) {
  float r = rand(vec2(0, uv.y));
  float m = smoothstep(0.0, -size, uv.x*(1.0-size) + size*r - (progress * (1.0 + size)));
  return mix(
    getFromColor(uv),
    getToColor(uv),
    m
  );
}

This time HLSL is this:

float fract(float x)
{
    return x - floor(x);
}

float rand(float2 co)
{
    return fract(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
}


float4 wind(float4 v1, float4 v2,float2 uv)
{
  float r = rand(float2(0, uv.y));
    p1 = 0.2f;
    progress = 0.5f; // hardcoded variables for testing, they will be taken from the buffer
  float m = smoothstep(0.0f, -p1, uv.x*(1.0f-p1) + p1*r - (progress * (1.0f + p1)));
    return lerp(v1, v2, m);
}


D2D_PS_ENTRY(main)
{
  
        float4 v1 = D2DGetInput(0);
        float4 v2 = D2DGetInput(1);
        return wind(v1,v2,D2DGetScenePosition().xy);
}

Have I misunderstood the OpenGL's mix and fract and rand stuff? I only get the second image pixels in my HLSL version without mixing.

EDIT: I 've hardcoded size to 0.992 and multiplied progress by 4 in the HLSL. Now it seems to work, do I miss some bounds-related issues? Is the smoothstep function working as expected?


Solution

  • I found it.

    It would need in main entry the usage of D2DGetInputCoordinate instead of D2DGetScenePosition

    After doing that, the transitions run fine.