glsl: Change do_set_program_inouts' is_fragment_shader arg to shader_type.

This will allow us to add geometry shader support without having to
add another boolean argument.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Move count_attribute_slots() out of the linker and into glsl_type.

Our previous justification for leaving this function out of glsl_type
was that it implemented counting rules that were specific to GLSL
1.50.  However, these counting rules also describe the number of
varying slots that Mesa will assign to a varying in the absence of
varying packing.  That's useful to be able to compute from outside of
the linker code (a future patch will use it from
ir_set_program_inouts.cpp).  So go ahead and move it to glsl_type.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl: Expand count_attribute_slots() to cover structs.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl: Use count_attribute_slots() in ir_set_program_inouts.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl: Extract marking functions from ir_set_program_inouts.

This patch extracts the functions mark_whole_variable() and
try_mark_partial_variable() from the ir_set_program_inouts visitor
functions.  This will make the code easier to follow when we add
geometry shader support.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl: Fallback gracefully if ir_set_program_inouts sees unexpected indexing.

The code in ir_set_program_inouts that marks just a portion of a
variable as used (rather than the whole variable) only works on a few
kinds of indexing operations:

- Indexing into matrices
- Indexing into arrays of matrices, vectors, or scalars.

Fortunately these are the only kinds of indexing operations that we
expect to see; everything else is either handled by a
previously-executed lowering pass or prohibited by GLSL.

However, that could conceivably change in the future (the GLSL rules
might change, or we might modify the lowering passes).  To avoid
mysterious bugs in the future, let's have ir_set_program_inouts report
an assertion failure if it ever encounters an unexpected kind of
indexing operation (and in release builds, fall back to just marking
the whole variable as used).

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl: In ir_set_program_inouts, handle indexing outside array/matrix bounds.

According to GLSL, indexing into an array or matrix with an
out-of-range constant results in a compile error.  However, indexing
with an out-of-range value that isn't constant merely results in
undefined results.

Since optimization passes (e.g. loop unrolling) can convert
non-constant array indices into constant array indices, it's possible
that ir_set_program_inouts will encounter a constant array index that
is out of range; if this happens, just mark the whole array as used.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl: Modify ir_set_program_inouts to handle geometry shaders.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl/linker: Make update_array_sizes apply to just uniforms.

Commit 586b4b5 (glsl: Also update implicit sizes of varyings at link
time) extended update_array_sizes() to apply to both uniforms and
shader ins/outs.  However, doing creates problems for geometry
shaders, because update_array_sizes() assumes that variables with
matching names in different parts of the pipeline should have the same
sizes.  With the addition of geometry shaders, this is no longer true
(e.g. both vertex and geometry shaders have a gl_ClipDistance output
variable, but there's no reason these variables should have the same
sizes).

The original reason for commit 586b4b5 (avoid problems with
gl_TexCoord being 0 length) has since been addressed by commit 6f53921
(linker: Ensure that unsized arrays have a size after linking).  So go
ahead and switch update_array_sizes() back to only acting on uniforms.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl: Add EXT_texture_array support for geometry shaders.

We can't just use a ".glsl" file since the Lod variants are only
available in vertex and geometry shaders, while the bias variants are
only available in the fragment shader.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
main: Fix geometry shader error messages (missing right paren)

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
main: Allow for the possibility of GL 3.2 without ARB_geometry_shader4.

Previously, we assumed that the only way Mesa would expose geometry
shader support was via the ARB_geometry_shader4 extension.  But this
extension has some extra complications over GL 3.2 (interactions with
compatibility-only features, and link-time initialization of the
constant gl_VerticesIn).  So we want to allow for the possibility of
supporting GL 3.2 (with GLSL 1.50 style geometry shaders) even if
ctx->Extensions.ARB_geometry_shader4 is false.

This patch adds a new function, _mesa_has_geometry_shaders(), which
returns true if either ARB_geometry_shader4 is supported or the GL
version is at least 3.2 desktop.  Since compute_version() only enables
GL 3.2 functionality when GLSL 1.50 support is present, a sufficient
way for a back-end to advertise geometry shader support is to set
ctx->Const.GLSLVersion >= 150.

v2: Remove unnecessary ctx->Const.GeometryShaders150 constant.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
draw/gs: fix allocation of buffer for GS output vertices

Before, it accounted for the size of the vertices but not the other fields
in the vertex_header struct, which caused memory corruption.
mesa: account for geometry shader texture fetches in update_texture_state

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
mesa/st: Add VARYING_SLOT_TEX[1-7] to st_translate_geometry_program().

v2 (Paul Berry <stereotype441@gmail.com>: Split out to separate patch
(previously this was part of "glsl: add builtins for geometry
shaders.")
glsl: add ir_emit_vertex and ir_end_primitive instruction types

These correspond to the EmitVertex and EndPrimitive functions in GLSL.

v2 (Paul Berry <stereotype441@gmail.com>): Add stub implementations of
new pure visitor functions to i965's vec4_visitor and fs_visitor
classes.

v3 (Paul Berry <stereotype441@gmail.com>): Rename classes to be more
consistent with the names used in the GL spec.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl: add builtins for geometry shaders.

v2 (Paul Berry <stereotype441@gmail.com>): Account for rework of
builtin_variables.cpp.  Use INTERP_QUALIFIER_FLAT for gl_PrimitiveID
so that it will obey provoking vertex conventions.  Convert to GLSL
1.50 style geometry shaders.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>

v3 (Paul Berry <stereotype441@gmail.com>): Be less obscure about
setting interpolation field of gl_Primitive variables.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
glsl/linker: Make separate allocations to track vertex and fragment shaders.

There's no reason to be clever about this.  By making separate
allocations for vertex and fragment shaders, we'll allow geometry
shaders to be added without introducing any complication.
glsl: support compilation of geometry shaders

This commit adds all of the parsing and semantics for GLSL 150 style
geometry shaders.

v2 (Paul Berry <stereotype441@gmail.com>): Add a few missing calls to
get_pipeline_stage().  Fix some signed/unsigned comparison warnings.
Fix handling of NULL consumer in assign_varying_locations().

v3 (Bryan Cain <bryancain3@gmail.com>): fix indexing order of 2D
arrays.  Also, allow interpolation qualifiers in geometry shaders.

v4 (Paul Berry <stereotype441@gmail.com>): Eliminate
get_pipeline_stage()--it is no longer needed thanks to 030ca23 (mesa:
renumber shader indices according to their placement in pipeline).
Remove 2D stuff.  Move vertices_per_prim() to ir.h, so that it will be
accessible from outside the linker.  Remove
inject_num_vertices_visitor.  Rework for GLSL 1.50.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>

v5 (Paul Berry <stereotype441@gmail.com>): Split out
do_set_program_inouts() argument refactoring to a separate patch.
Move geom_array_resizing_visitor to later in the series.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
mesa: Refactor copying of linked program data.

This patch creates a single function to copy the the UsesClipDistance
flag from gl_shader_program.Vert to gl_vertex_program.  Previously
this logic was duplicated in the i965-specific function
brw_link_shader() and the core mesa function _mesa_ir_link_shader().

This logic will have to be expanded to support geometry shaders, and I
don't want to have to update it in two separate places.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>