HLSL2GLSL issue with accessing sampler in Cg shader - lowp

Discussion of issues specific to mobile platforms such as iOS, Android, Symbian and Meego.
Post Reply
simedj
Goblin
Posts: 262
Joined: Fri Nov 18, 2011 6:50 pm

HLSL2GLSL issue with accessing sampler in Cg shader - lowp

Post by simedj » Tue Feb 04, 2014 11:51 pm

This simple shader is causing problems in iOS after conversion to GLSL ES 2.0:

Cg version:

Code: Select all

float4 xray_tissue_fp(in float2 uv0 : TEXCOORD0, in float3 normal : TEXCOORD6, uniform sampler2D xraymap) : COLOR
{
	float cosTheta = -normal.z;
	float tissueThickness = tex2D(xraymap,uv0).r * 200.0;
	float tissueDistance = tissueThickness*cosTheta;
	float transmission = exp(-tissueAttenuation*tissueDistance);
	return float4(transmission,transmission,transmission,1);
}
ES 2.0 version:

Code: Select all

#version 100
precision mediump int;
precision mediump float;
uniform float tissueAttenuation;
uniform sampler2D xraymap;
vec4 xray_tissue_fp( in vec2 uv0, in vec3 normal, sampler2D xraymap );
#line 22
vec4 xray_tissue_fp( in vec2 uv0, in vec3 normal, sampler2D xraymap ) {
    float cosTheta;
    float tissueThickness;
    float tissueDistance;
    float transmission;
    cosTheta = (-normal.z);
    tissueThickness = (texture2D( xraymap, uv0).x * 200.0);
    #line 27
    tissueDistance = (tissueThickness * cosTheta);
    transmission = exp(((-tissueAttenuation) * tissueDistance));
    return vec4( transmission, transmission, transmission, 1.0);
}
uniform sampler2D xlu_xraymap;
varying vec2 xlv_TEXCOORD0;
varying vec3 xlv_TEXCOORD6;
void main() {
    vec4 xl_retval;
    xl_retval = xray_tissue_fp( vec2(xlv_TEXCOORD0), vec3(xlv_TEXCOORD6), xlu_xraymap);
    gl_FragData[0] = vec4( xl_retval);
}
Error:
WARNING: 0:29: Overflow in implicit constant conversion, minimum range for lowp float is (-2,2)
This SO link explains what is going on, but is there a way to make the converter smarter? I am resorting to splitting the line

Code: Select all

	float tissueThickness = tex2D(xraymap,uv0).r * 200.0;
To

Code: Select all

	float mapValue = tex2D(xraymap,uv0).r;
	float tissueThickness = mapValue * 200;
But is there a way to avoid this need?
0 x
Looking to find experienced Ogre & shader developers/artists. PM me with a contact email address if interested.

dbtx
Halfling
Posts: 66
Joined: Tue Nov 01, 2011 9:07 pm

Re: HLSL2GLSL issue with accessing sampler in Cg shader - lo

Post by dbtx » Wed Feb 12, 2014 11:40 am

Will it let you simply cast before multiplying?

Code: Select all

float tissueThickness = float(tex2D(xraymap,uv0).r) * 200.0;
0 x

Post Reply