+static inline void assign_vue_slot(struct brw_vue_map *vue_map,
+ int vert_result)
+{
+ /* Make sure this vert_result hasn't been assigned a slot already */
+ assert (vue_map->vert_result_to_slot[vert_result] == -1);
+
+ vue_map->vert_result_to_slot[vert_result] = vue_map->num_slots;
+ vue_map->slot_to_vert_result[vue_map->num_slots++] = vert_result;
+}
+
+/**
+ * Compute the VUE map for vertex shader program.
+ *
+ * Note that consumers of this map using cache keys must include
+ * prog_data->userclip and prog_data->outputs_written in their key
+ * (generated by CACHE_NEW_VS_PROG).
+ */
+static void
+brw_compute_vue_map(struct brw_vs_compile *c)
+{
+ struct brw_context *brw = c->func.brw;
+ const struct intel_context *intel = &brw->intel;
+ struct brw_vue_map *vue_map = &c->prog_data.vue_map;
+ GLbitfield64 outputs_written = c->prog_data.outputs_written;
+ int i;
+
+ vue_map->num_slots = 0;
+ for (i = 0; i < BRW_VERT_RESULT_MAX; ++i) {
+ vue_map->vert_result_to_slot[i] = -1;
+ vue_map->slot_to_vert_result[i] = BRW_VERT_RESULT_MAX;
+ }
+
+ /* VUE header: format depends on chip generation and whether clipping is
+ * enabled.
+ */
+ switch (intel->gen) {
+ case 4:
+ /* There are 8 dwords in VUE header pre-Ironlake:
+ * dword 0-3 is indices, point width, clip flags.
+ * dword 4-7 is ndc position
+ * dword 8-11 is the first vertex data.
+ */
+ assign_vue_slot(vue_map, VERT_RESULT_PSIZ);
+ assign_vue_slot(vue_map, BRW_VERT_RESULT_NDC);
+ assign_vue_slot(vue_map, VERT_RESULT_HPOS);
+ break;
+ case 5:
+ /* There are 20 DWs (D0-D19) in VUE header on Ironlake:
+ * dword 0-3 of the header is indices, point width, clip flags.
+ * dword 4-7 is the ndc position
+ * dword 8-11 of the vertex header is the 4D space position
+ * dword 12-19 of the vertex header is the user clip distance.
+ * dword 20-23 is a pad so that the vertex element data is aligned
+ * dword 24-27 is the first vertex data we fill.
+ *
+ * Note: future pipeline stages expect 4D space position to be
+ * contiguous with the other vert_results, so we make dword 24-27 a
+ * duplicate copy of the 4D space position.
+ */
+ assign_vue_slot(vue_map, VERT_RESULT_PSIZ);
+ assign_vue_slot(vue_map, BRW_VERT_RESULT_NDC);
+ assign_vue_slot(vue_map, BRW_VERT_RESULT_HPOS_DUPLICATE);
+ assign_vue_slot(vue_map, VERT_RESULT_CLIP_DIST0);
+ assign_vue_slot(vue_map, VERT_RESULT_CLIP_DIST1);
+ assign_vue_slot(vue_map, BRW_VERT_RESULT_PAD);
+ assign_vue_slot(vue_map, VERT_RESULT_HPOS);
+ break;
+ case 6:
+ case 7:
+ /* There are 8 or 16 DWs (D0-D15) in VUE header on Sandybridge:
+ * dword 0-3 of the header is indices, point width, clip flags.
+ * dword 4-7 is the 4D space position
+ * dword 8-15 of the vertex header is the user clip distance if
+ * enabled.
+ * dword 8-11 or 16-19 is the first vertex element data we fill.
+ */
+ assign_vue_slot(vue_map, VERT_RESULT_PSIZ);
+ assign_vue_slot(vue_map, VERT_RESULT_HPOS);
+ if (c->key.userclip_active) {
+ assign_vue_slot(vue_map, VERT_RESULT_CLIP_DIST0);
+ assign_vue_slot(vue_map, VERT_RESULT_CLIP_DIST1);
+ }
+ /* front and back colors need to be consecutive so that we can use
+ * ATTRIBUTE_SWIZZLE_INPUTATTR_FACING to swizzle them when doing
+ * two-sided color.
+ */
+ if (outputs_written & BITFIELD64_BIT(VERT_RESULT_COL0))
+ assign_vue_slot(vue_map, VERT_RESULT_COL0);
+ if (outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC0))
+ assign_vue_slot(vue_map, VERT_RESULT_BFC0);
+ if (outputs_written & BITFIELD64_BIT(VERT_RESULT_COL1))
+ assign_vue_slot(vue_map, VERT_RESULT_COL1);
+ if (outputs_written & BITFIELD64_BIT(VERT_RESULT_BFC1))
+ assign_vue_slot(vue_map, VERT_RESULT_BFC1);
+ break;
+ default:
+ assert (!"VUE map not known for this chip generation");
+ break;
+ }
+
+ /* The hardware doesn't care about the rest of the vertex outputs, so just
+ * assign them contiguously. Don't reassign outputs that already have a
+ * slot.
+ *
+ * Also, prior to Gen6, don't assign a slot for VERT_RESULT_CLIP_VERTEX,
+ * since it is unsupported. In Gen6 and above, VERT_RESULT_CLIP_VERTEX may
+ * be needed for transform feedback; since we don't want to have to
+ * recompute the VUE map (and everything that depends on it) when transform
+ * feedback is enabled or disabled, just go ahead and assign a slot for it.
+ */
+ for (int i = 0; i < VERT_RESULT_MAX; ++i) {
+ if (intel->gen < 6 && i == VERT_RESULT_CLIP_VERTEX)
+ continue;
+ if ((outputs_written & BITFIELD64_BIT(i)) &&
+ vue_map->vert_result_to_slot[i] == -1) {
+ assign_vue_slot(vue_map, i);
+ }
+ }
+}
+
+
+/**
+ * Decide which set of clip planes should be used when clipping via
+ * gl_Position or gl_ClipVertex.
+ */
+gl_clip_plane *brw_select_clip_planes(struct gl_context *ctx)
+{
+ if (ctx->Shader.CurrentVertexProgram) {
+ /* There is currently a GLSL vertex shader, so clip according to GLSL
+ * rules, which means compare gl_ClipVertex (or gl_Position, if
+ * gl_ClipVertex wasn't assigned) against the eye-coordinate clip planes
+ * that were stored in EyeUserPlane at the time the clip planes were
+ * specified.
+ */
+ return ctx->Transform.EyeUserPlane;
+ } else {
+ /* Either we are using fixed function or an ARB vertex program. In
+ * either case the clip planes are going to be compared against
+ * gl_Position (which is in clip coordinates) so we have to clip using
+ * _ClipUserPlane, which was transformed into clip coordinates by Mesa
+ * core.
+ */
+ return ctx->Transform._ClipUserPlane;
+ }
+}