Page 1 of 1

HLSL2GLSL issue with accessing sampler in Cg shader - lowp

Posted: Tue Feb 04, 2014 11:51 pm
by simedj
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?

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

Posted: Wed Feb 12, 2014 11:40 am
by dbtx
Will it let you simply cast before multiplying?

Code: Select all

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