<div dir="ltr">On 17 November 2013 00:24, Victor Luchitz <span dir="ltr"><<a href="mailto:vluchits@gmail.com" target="_blank">vluchits@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Hello,<br><br></div>in my opinion GLSL compiler in mesa is too restrictive when it comes to sampler arrays. The following code can not be compiled due to the "sampler arrays indexed with non-constant expressions is forbidden in GLSL 1.30 and later":<br>
<br></div>#define MAX_SHADOWS 2<br><div><br>varying vec4 v_ShadowProjVector[MAX_SHADOWS];<br><br>uniform sampler2DShadow u_ShadowmapTexture[MAX_SHADOWS];<br># define qfShadow2D(t,v) float(shadow2D(t,v))<br><br>uniform float u_ShadowAlpha;<br>
uniform float u_ShadowProjDistance[MAX_SHADOWS];<br>uniform vec4 u_ShadowmapTextureParams[MAX_SHADOWS];<br><br>void main(void)<br>{<br> float finalcolor = 1.0;<br><br> for (int i = 0; i < MAX_SHADOWS; i++)<br> {<br>
vec3 shadowmaptc = vec3(v_ShadowProjVector[i].xyz / v_ShadowProjVector[i].w);<br><br> // this keeps shadows from appearing on surfaces behind frustum's nearplane<br> float d = step(v_ShadowProjVector[i].w, 0.0);<br>
<br> //shadowmaptc = (shadowmaptc + vec3 (1.0)) * vec3 (0.5);<br> shadowmaptc.xy = shadowmaptc.xy * u_ShadowmapTextureParams[i].xy; // .x - texture width<br> shadowmaptc.z = clamp(shadowmaptc.z, 0.0, 1.0);<br>
shadowmaptc.xy = vec2(clamp(shadowmaptc.x, 0.0, u_ShadowmapTextureParams[i].x), clamp(shadowmaptc.y, 0.0, u_ShadowmapTextureParams[i].y));<br><br> vec2 ShadowMap_TextureScale = u_ShadowmapTextureParams[i].zw;<br>
<br> float f;<br><br> f = qfShadow2D(u_ShadowmapTexture[i], vec3(shadowmaptc.xy * ShadowMap_TextureScale, shadowmaptc.z));<br> <br> finalcolor *= clamp(max(max(f, d), u_ShadowAlpha), 0.0, 1.0);<br>
}<br><br> gl_FragColor = vec4(vec3(finalcolor),1.0);<br>}<br><br clear="all"><div><div><br></div><div>Lines 159-136 of src/glsl/ast_array_index.cpp say:<br><br> * This restriction was added in GLSL 1.30. Shaders using earlier version<br>
* of the language should not be rejected by the compiler front-end for<br> * using this construct. This allows useful things such as using a loop<br> * counter as the index to an array of samplers. If the loop in unrolled,<br>
* the code should compile correctly. Instead, emit a warning.<br></div><div><br></div><div>If compiler actually attempted to unroll the loop above, it would notice that the does compile correctly. Instead it just emits and error and in my opinion, contradicts the comment above but not allowing the "useful thing" in question.<br>
<br></div><div>Can the compiler be changed to _first_ attempt to unroll the loop and then check for sampler array indices being constants?<span><font color="#888888"><br></font></span></div></div></div></div>
</blockquote><div><br></div><div>Thanks for the feedback, Victor. We are always interested in hearing suggestions about how to improve Mesa.<br></div><div><br></div>Unfortunately, in this case, your suggestion would make Mesa non-conformant with the GLSL specs because it would allow shaders that are prohibited by the spec. Generally we try to avoid this sort of non-spec-conformance because it leads to portability problems for developers like you (e.g. your shader works with Mesa, but it fails with other GL implementations). A further problem is that Mesa has heuristics to decide whether to unroll loops; if we followed your suggestion, then your shader might work today, but fail tomorrow if you make a small change to the shader that makes Mesa decide not to unroll the loop (or we make a change to the heuristics in a future release of Mesa).<br>
<br></div><div>Note that once we finish implementing ARB_gpu_shader5 (which we hope to finish soon), you will be able to use that--ARB_gpu_shader5 lifts the restriction that sampler array indices must be constant.<br></div>
<div class="gmail_quote"></div></div></div>