[Mesa-dev] sampler arrays indexed with non-constant expressions

Paul Berry stereotype441 at gmail.com
Mon Nov 18 12:04:57 PST 2013


On 17 November 2013 00:24, Victor Luchitz <vluchits at gmail.com> wrote:

> Hello,
>
> 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":
>
> #define MAX_SHADOWS 2
>
> varying vec4 v_ShadowProjVector[MAX_SHADOWS];
>
> uniform sampler2DShadow u_ShadowmapTexture[MAX_SHADOWS];
> # define qfShadow2D(t,v) float(shadow2D(t,v))
>
> uniform float u_ShadowAlpha;
> uniform float u_ShadowProjDistance[MAX_SHADOWS];
> uniform vec4 u_ShadowmapTextureParams[MAX_SHADOWS];
>
> void main(void)
> {
>     float finalcolor = 1.0;
>
>     for (int i = 0; i < MAX_SHADOWS; i++)
>     {
>         vec3 shadowmaptc = vec3(v_ShadowProjVector[i].xyz /
> v_ShadowProjVector[i].w);
>
>         // this keeps shadows from appearing on surfaces behind frustum's
> nearplane
>         float d = step(v_ShadowProjVector[i].w, 0.0);
>
>         //shadowmaptc = (shadowmaptc + vec3 (1.0)) * vec3 (0.5);
>         shadowmaptc.xy = shadowmaptc.xy * u_ShadowmapTextureParams[i].xy;
> // .x - texture width
>         shadowmaptc.z = clamp(shadowmaptc.z, 0.0, 1.0);
>         shadowmaptc.xy = vec2(clamp(shadowmaptc.x, 0.0,
> u_ShadowmapTextureParams[i].x), clamp(shadowmaptc.y, 0.0,
> u_ShadowmapTextureParams[i].y));
>
>         vec2 ShadowMap_TextureScale = u_ShadowmapTextureParams[i].zw;
>
>         float f;
>
>         f = qfShadow2D(u_ShadowmapTexture[i], vec3(shadowmaptc.xy *
> ShadowMap_TextureScale, shadowmaptc.z));
>
>         finalcolor *= clamp(max(max(f, d), u_ShadowAlpha), 0.0, 1.0);
>     }
>
>     gl_FragColor = vec4(vec3(finalcolor),1.0);
> }
>
>
> Lines 159-136 of src/glsl/ast_array_index.cpp say:
>
>     * This restriction was added in GLSL 1.30.  Shaders using earlier
> version
>     * of the language should not be rejected by the compiler front-end for
>     * using this construct.  This allows useful things such as using a loop
>     * counter as the index to an array of samplers.  If the loop in
> unrolled,
>     * the code should compile correctly.  Instead, emit a warning.
>
> 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.
>
> Can the compiler be changed to _first_ attempt to unroll the loop and then
> check for sampler array indices being constants?
>

Thanks for the feedback, Victor.  We are always interested in hearing
suggestions about how to improve Mesa.

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).

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20131118/8de860be/attachment.html>


More information about the mesa-dev mailing list