Convert HLSL to GLSL

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Jibreel Yahya
Posts: 10
Joined: Fri Oct 23, 2015 7:59 pm

Convert HLSL to GLSL

Post by Jibreel Yahya »

Hi, I'm trying convert two shaders to GLSL, but I'm having problems.
I tried using the ATI HLSL2GLSL, but my exemple app does not work with the GLSL generated by HLSL2GLSL.

HLSL pixel shader

Code: Select all

 
sampler fire_base       : register(s0); 
sampler fire_distortion : register(s1); 
sampler fire_opacity    : register(s2); 
 
float4 bx2(float x) 
{ 
   return 2.0f * x - 1.0f; 
} 
 
float4 pixelMain (float4 tc0 : TEXCOORD0, 
                  float4 tc1 : TEXCOORD1, 
                  float4 tc2 : TEXCOORD2, 
                  float4 tc3 : TEXCOORD3) : COLOR 
{ 
   float distortion_amount0  = 0.092; 
   float distortion_amount1  = 0.092; 
   float distortion_amount2  = 0.092; 
    
   float4 height_attenuation = float4(0.3, 0.39, 0.0, 1.0); 
    
   float4 noise0 = tex2D(fire_distortion, tc1); 
   float4 noise1 = tex2D(fire_distortion, tc2); 
   float4 noise2 = tex2D(fire_distortion, tc3); 
 
   float4 noiseSum = bx2(noise0) * distortion_amount0 + 
          bx2(noise1) * distortion_amount1 + bx2(noise2) * distortion_amount2; 
          
   float4 perturbedBaseCoords = tc0 + noiseSum * (tc0.y * 
         height_attenuation.x + height_attenuation.y); 
 
   float4 base    = tex2D(fire_base, perturbedBaseCoords) * 2; 
   float4 opacity = tex2D(fire_opacity, perturbedBaseCoords); 
 
   return base * opacity; 
}
 
HLSL vertex shader

Code: Select all

 
float4x4 wvp; 
float time; 
 
struct VS_OUTPUT 
{ 
   float4 Pos       : POSITION; 
   float2 TexCoord0 : TEXCOORD0; 
   float2 TexCoord1 : TEXCOORD1; 
   float2 TexCoord2 : TEXCOORD2; 
   float2 TexCoord3 : TEXCOORD3; 
}; 
 
VS_OUTPUT vertexMain (float4 pos: POSITION, 
                      float2 texCoord : TEXCOORD0) 
{ 
   VS_OUTPUT Out; 
   Out.Pos = mul(pos, wvp); 
 
   float4 layer_speed = float4(0.2, 0.52, 0.1, 1.0); 
    
   Out.TexCoord0 = texCoord; 
   Out.TexCoord0.y += 0.2; 
 
   Out.TexCoord1.x = texCoord.x; 
   Out.TexCoord1.y = texCoord.y + layer_speed.x * time / 1000.0; 
 
   Out.TexCoord2.x = texCoord.x; 
   Out.TexCoord2.y = texCoord.y + layer_speed.y * time / 1000.0; 
 
   Out.TexCoord3.x = texCoord.x; 
   Out.TexCoord3.y = texCoord.y + layer_speed.z * time / 1000.0; 
 
   return Out; 
}
 
Generated GLSL pixel shader

Code: Select all

 
//
// Global variable definitions
//
 
uniform sampler2D fire_base;
uniform sampler2D fire_distortion;
uniform sampler2D fire_opacity;
 
//
// Function declarations
//
 
vec4 pixelMain( in vec4 tc0, in vec4 tc1, in vec4 tc2, in vec4 tc3 );
vec4 bx2( in float x );
 
//
// Function definitions
//
 
vec4 pixelMain( in vec4 tc0, in vec4 tc1, in vec4 tc2, in vec4 tc3 ) {
    float distortion_amount0 = 0.092;
    float distortion_amount1 = 0.092;
    float distortion_amount2 = 0.092;
    vec4 height_attenuation = vec4( 0.3, 0.39, 0.0, 1.0);
    vec4 noise0;
    vec4 noise1;
    vec4 noise2;
    vec4 noiseSum;
    vec4 perturbedBaseCoords;
    vec4 base;
    vec4 opacity;
 
    noise0 = texture2D( fire_distortion, vec2( tc1));
    noise1 = texture2D( fire_distortion, vec2( tc2));
    noise2 = texture2D( fire_distortion, vec2( tc3));
    noiseSum = (((bx2( float( noise0)) * distortion_amount0) + (bx2( float( noise1)) * distortion_amount1)) + (bx2( float( noise2)) * distortion_amount2));
    perturbedBaseCoords = (tc0 + (noiseSum * ((tc0.y  * height_attenuation.x ) + height_attenuation.y )));
    base = (texture2D( fire_base, vec2( perturbedBaseCoords)) * 2);
    opacity = texture2D( fire_opacity, vec2( perturbedBaseCoords));
    return (base * opacity);
}
 
vec4 bx2( in float x ) {
 
    return vec4( ((2.0f * x) - 1.0f));
}
 
//
// User varying
//
varying vec4 xlat_varying_TEXCOORD0;
varying vec4 xlat_varying_TEXCOORD1;
varying vec4 xlat_varying_TEXCOORD2;
varying vec4 xlat_varying_TEXCOORD3;
 
//
// Translator's entry point
//
void main()
{
    vec4 xlat_retVal;
 
    xlat_retVal = pixelMain(xlat_varying_TEXCOORD0, xlat_varying_TEXCOORD1, xlat_varying_TEXCOORD2, xlat_varying_TEXCOORD3);
 
    gl_FragColor = xlat_retVal;
}
 
Generated GLSL vertex shader

Code: Select all

 
 
//
// Structure definitions
//
 
struct VS_OUTPUT {
    vec4 Pos;
    vec2 TexCoord0;
    vec2 TexCoord1;
    vec2 TexCoord2;
    vec2 TexCoord3;
};
 
//
// Global variable definitions
//
 
uniform float time;
uniform mat4 wvp;
 
//
// Function declarations
//
 
VS_OUTPUT vertexMain( in vec4 pos, in vec2 texCoord );
 
//
// Function definitions
//
 
VS_OUTPUT vertexMain( in vec4 pos, in vec2 texCoord ) {
    VS_OUTPUT Out;
    vec4 layer_speed = vec4( 0.2, 0.52, 0.1, 1.0);
 
    Out.Pos = ( pos * wvp );
    Out.TexCoord0 = texCoord;
    Out.TexCoord0.y  += 0.2;
    Out.TexCoord1.x  = texCoord.x ;
    Out.TexCoord1.y  = (texCoord.y  + ((layer_speed.x  * time) / 1000.0));
    Out.TexCoord2.x  = texCoord.x ;
    Out.TexCoord2.y  = (texCoord.y  + ((layer_speed.y  * time) / 1000.0));
    Out.TexCoord3.x  = texCoord.x ;
    Out.TexCoord3.y  = (texCoord.y  + ((layer_speed.z  * time) / 1000.0));
    return Out;
}
 
//
// User varying
//
varying vec4 xlat_varying_TEXCOORD0;
varying vec4 xlat_varying_TEXCOORD1;
varying vec4 xlat_varying_TEXCOORD2;
varying vec4 xlat_varying_TEXCOORD3;
 
//
// Translator's entry point
//
void main() {
    VS_OUTPUT xlat_retVal;
 
    xlat_retVal = vertexMain( vec4(gl_Vertex), vec2(gl_MultiTexCoord0));
 
    gl_Position = vec4( xlat_retVal.Pos);
    xlat_varying_TEXCOORD0 = vec4( xlat_retVal.TexCoord0, 0.0, 0.0);
    xlat_varying_TEXCOORD1 = vec4( xlat_retVal.TexCoord1, 0.0, 0.0);
    xlat_varying_TEXCOORD2 = vec4( xlat_retVal.TexCoord2, 0.0, 0.0);
    xlat_varying_TEXCOORD3 = vec4( xlat_retVal.TexCoord3, 0.0, 0.0);
}
 
main.cpp

Code: Select all

 
#include <irrlicht.h>
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
 
const s32 winWidth = 800;
const s32 winHeight = 600;
 
IrrlichtDevice* device = 0;
 
class MyEventReceiver : public IEventReceiver
{
public:
    bool OnEvent(const SEvent& event) override
    {
        if (event.EventType == EET_KEY_INPUT_EVENT &&
            event.KeyInput.Key == KEY_ESCAPE &&
            !event.KeyInput.PressedDown)
        {
            device->closeDevice();
            return true;
        }
        return false;
    }
};
 
class CFireMaterial : public video::IShaderConstantSetCallBack
{
public:
    virtual void OnSetConstants(video::IMaterialRendererServices* services, irr::s32 userData)
    {
        video::IVideoDriver* driver = services->getVideoDriver();
 
        core::matrix4 worldViewProj;
        worldViewProj = driver->getTransform(video::ETS_PROJECTION);
        worldViewProj *= driver->getTransform(video::ETS_VIEW);
        worldViewProj *= driver->getTransform(video::ETS_WORLD);
        services->setVertexShaderConstant("wvp", &worldViewProj[0], 16);
 
        float time = (float)device->getTimer()->getTime();
        services->setVertexShaderConstant("time", &time, 1);
    }
};
 
int main()
{
    MyEventReceiver receiver;
    SIrrlichtCreationParameters param;
    param.AntiAlias = false;
    param.Bits = 32;
    param.DriverType = video::EDT_OPENGL;
    param.EventReceiver = &receiver;
    param.Fullscreen = false;
    //param.HighPrecisionFPU = true;
    param.Stencilbuffer = true;
    param.Vsync = false;
    param.WindowSize.Width = winWidth;
    param.WindowSize.Height = winHeight;
    device = createDeviceEx(param);
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
 
    CFireMaterial* fire = new CFireMaterial();
    video::IGPUProgrammingServices* gpu = driver->getGPUProgrammingServices();
    //c8* shaderFireFilename = "MyData/fire.hlsl";
    c8* PSFireFilename = "MyData/PSFire.glsl";
    c8* VSFireFilename = "MyData/VSFire.glsl";
    s32 shaderFireMaterial = gpu->addHighLevelShaderMaterialFromFiles(
        VSFireFilename, "main", video::EVST_VS_1_1,
        PSFireFilename, "main", video::EPST_PS_1_1,
        fire, video::EMT_TRANSPARENT_ADD_COLOR);
    fire->drop();
 
    scene::ISceneNode* bill = smgr->addBillboardSceneNode(
        0, core::dimension2d<f32>(100.0f, 100.0f));
    bill->setMaterialTexture(0, driver->getTexture("MyData/diffuse.jpg"));
    bill->setMaterialTexture(1, driver->getTexture("MyData/distortion.jpg"));
    bill->setMaterialTexture(2, driver->getTexture("MyData/transparency.jpg"));
    bill->setMaterialType((video::E_MATERIAL_TYPE)shaderFireMaterial);
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(0, 100.0f, 150.0f);
    cam->setPosition(core::vector3df(0.0f, 0.0f, -100.0f));
    cam->setTarget(bill->getPosition());
 
    scene::IAnimatedMesh* m = smgr->getMesh("../../media/room.3ds");
    smgr->getMeshManipulator()->makePlanarTextureMapping(m->getMesh(0), 0.004f);
    scene::IAnimatedMeshSceneNode* am = smgr->addAnimatedMeshSceneNode(m);
    am->getMaterial(0).setTexture(0, driver->getTexture("../../media/rockwall.jpg"));
    am->getMaterial(0).Lighting = false;
    am->setPosition(core::vector3df(0.0f, -150.0f, 0.0f));
 
    device->getCursorControl()->setVisible(false);
 
    while (device->run())
    {
        driver->beginScene(true, true, video::SColor(255, 128, 128, 128));
        smgr->drawAll();
        driver->endScene();
 
        core::stringw str;
        str = L"FPS ";
        str += driver->getFPS();
        str += L" ";
        device->setWindowCaption(str.c_str());
    }
    device->drop();
    return 0;
}
 
Thanks.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Convert HLSL to GLSL

Post by mongoose7 »

You didn't set any samplers in OnSetConstants. Your texture reads are undefined.
Jibreel Yahya
Posts: 10
Joined: Fri Oct 23, 2015 7:59 pm

Re: Convert HLSL to GLSL

Post by Jibreel Yahya »

@mongoose7, thank you for your reply.
The original shaders work fine with HLSL, but doesn't in GLSL.
In HLSL the method OnSetConstants doesn't set any sampler and works fine.
Only the GLSL shaders generated by HLSL2GLSL doesn't work. :(
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Convert HLSL to GLSL

Post by mongoose7 »

Like, maybe HLSL and GLSL are different, like, maybe!!!

YOU HAVE TO SET THE SAMPLERS IN ON-SET-CONSTANTS IF YOU USE GLSL
Jibreel Yahya
Posts: 10
Joined: Fri Oct 23, 2015 7:59 pm

Re: Convert HLSL to GLSL

Post by Jibreel Yahya »

Thanks, @mongoose7.
It works fine now.
Post Reply