GLSL to HLSL Translation issue

I’m trying to implement a shader in HLSL that I found on shadertoy (GLSL).

I feel I’ve translated/mapped everything I could find-but the result is a bit funky.

This is what the original shader looks like:

and my “translated” result looks like:

code here: #if OPENGL #define SV_POSITION POSITION #define VS_SHADERMODEL vs_3_0 #def -

My shader does slowly kind of translate from right to left. It has some semblance of motion. But I feel there is something that I’ve missed, some functionality or assumption thats incorrect or different between the two. Any ideas?

Attempting to find the problem with your translation is too much work for me, but maybe this automatic translation can be useful. It combines glslang and SPIRV-Cross.

It’s not as nice looking as manually converted code, but at least it should be free of translation errors. I changed the main function to

void main() {

without the parameters, and added this on top to make it compile:

#version 430

uniform float iTime;
uniform vec2 iResolution;
uniform vec2 iMouse;

in vec2 fragCoord;
out vec4 fragColor;

EDIT: The HLSL output shader model is set to 5.0 right now, but you can change that if you want.

Yeap, works perfectly. This is exactly the resource I needed. I guess I will compare the two and try to see whats different.


I found that I was missing keywords up top for some of the constant definitions.

If I have the global constants defined as:

    float3 ABSORPTION_INTENSITY = float3(.5, .8, .7) * .25;

	float3 sunDirection = normalize(float3(.5, .8, .5));
	float3 lightColor = float3(.3, .3, .2);
	float3 ambientLightDir = normalize(float3(0., 1., 0.));
	float3 ambientLightColor = float3(.9, .7, .3);

	float3 cloudColor = float3(.92, .92, .92);    
	float3x3 m = float3x3( 0.00,  0.80,  0.60,
	                         -0.80,  0.36, -0.48,
	                         -0.60, -0.48,  0.64);

I get my first result, if I use them as static consts:

    static const float3 ABSORPTION_INTENSITY = float3(.5, .8, .7) * .25;

	static const float3 sunDirection = normalize(float3(.5, .8, .5));
	static const float3 lightColor = float3(.3, .3, .2);
	static const float3 ambientLightDir = normalize(float3(0., 1., 0.));
	static const float3 ambientLightColor = float3(.9, .7, .3);

	static const float3 cloudColor = float3(.92, .92, .92);    
	static const float3x3 m = float3x3( 0.00,  0.80,  0.60,
	                         -0.80,  0.36, -0.48,
	                         -0.60, -0.48,  0.64);

I get the desired result.

You must be using OpenGL then, because MojoShader can’t handle default values for shader parameters, so all those parameters will be zero. In DirectX default values work.

You can get the first version working for OpenGL too, you just have to set the parameters from the CPU. They are probably meant to be dynamic anyway.

This reminds me, fixing default values for OpenGL should be easy with my ShaderConductor fork.

1 Like