i965/vec4: Simplify opt_reduce_swizzle() using the swizzle utils.
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_gs_state.c
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
31 #include "intel_batchbuffer.h"
32
33 static void
34 gen6_upload_gs_push_constants(struct brw_context *brw)
35 {
36 struct brw_stage_state *stage_state = &brw->gs.base;
37
38 /* BRW_NEW_GEOMETRY_PROGRAM */
39 const struct brw_geometry_program *gp =
40 (struct brw_geometry_program *) brw->geometry_program;
41
42 if (gp) {
43 /* BRW_NEW_GS_PROG_DATA */
44 struct brw_stage_prog_data *prog_data = &brw->gs.prog_data->base.base;
45
46 gen6_upload_push_constants(brw, &gp->program.Base, prog_data,
47 stage_state, AUB_TRACE_VS_CONSTANTS);
48 }
49
50 if (brw->gen >= 7)
51 gen7_upload_constant_state(brw, stage_state, gp, _3DSTATE_CONSTANT_GS);
52 }
53
54 const struct brw_tracked_state gen6_gs_push_constants = {
55 .dirty = {
56 .mesa = _NEW_PROGRAM_CONSTANTS |
57 _NEW_TRANSFORM,
58 .brw = BRW_NEW_BATCH |
59 BRW_NEW_GEOMETRY_PROGRAM |
60 BRW_NEW_GS_PROG_DATA |
61 BRW_NEW_PUSH_CONSTANT_ALLOCATION,
62 },
63 .emit = gen6_upload_gs_push_constants,
64 };
65
66 static void
67 upload_gs_state_for_tf(struct brw_context *brw)
68 {
69 BEGIN_BATCH(7);
70 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
71 OUT_BATCH(brw->ff_gs.prog_offset);
72 OUT_BATCH(GEN6_GS_SPF_MODE | GEN6_GS_VECTOR_MASK_ENABLE);
73 OUT_BATCH(0); /* no scratch space */
74 OUT_BATCH((2 << GEN6_GS_DISPATCH_START_GRF_SHIFT) |
75 (brw->ff_gs.prog_data->urb_read_length << GEN6_GS_URB_READ_LENGTH_SHIFT));
76 OUT_BATCH(((brw->max_gs_threads - 1) << GEN6_GS_MAX_THREADS_SHIFT) |
77 GEN6_GS_STATISTICS_ENABLE |
78 GEN6_GS_SO_STATISTICS_ENABLE |
79 GEN6_GS_RENDERING_ENABLE);
80 OUT_BATCH(GEN6_GS_SVBI_PAYLOAD_ENABLE |
81 GEN6_GS_SVBI_POSTINCREMENT_ENABLE |
82 (brw->ff_gs.prog_data->svbi_postincrement_value <<
83 GEN6_GS_SVBI_POSTINCREMENT_VALUE_SHIFT) |
84 GEN6_GS_ENABLE);
85 ADVANCE_BATCH();
86 }
87
88 static void
89 upload_gs_state(struct brw_context *brw)
90 {
91 /* BRW_NEW_GEOMETRY_PROGRAM */
92 bool active = brw->geometry_program;
93 /* BRW_NEW_GS_PROG_DATA */
94 const struct brw_vue_prog_data *prog_data = &brw->gs.prog_data->base;
95 const struct brw_stage_state *stage_state = &brw->gs.base;
96
97 if (!active || stage_state->push_const_size == 0) {
98 /* Disable the push constant buffers. */
99 BEGIN_BATCH(5);
100 OUT_BATCH(_3DSTATE_CONSTANT_GS << 16 | (5 - 2));
101 OUT_BATCH(0);
102 OUT_BATCH(0);
103 OUT_BATCH(0);
104 OUT_BATCH(0);
105 ADVANCE_BATCH();
106 } else {
107 BEGIN_BATCH(5);
108 OUT_BATCH(_3DSTATE_CONSTANT_GS << 16 |
109 GEN6_CONSTANT_BUFFER_0_ENABLE |
110 (5 - 2));
111 /* Pointer to the GS constant buffer. Covered by the set of
112 * state flags from gen6_upload_vs_constants
113 */
114 OUT_BATCH(stage_state->push_const_offset +
115 stage_state->push_const_size - 1);
116 OUT_BATCH(0);
117 OUT_BATCH(0);
118 OUT_BATCH(0);
119 ADVANCE_BATCH();
120 }
121
122 if (active) {
123 BEGIN_BATCH(7);
124 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
125 OUT_BATCH(stage_state->prog_offset);
126
127 /* GEN6_GS_SPF_MODE and GEN6_GS_VECTOR_MASK_ENABLE are enabled as it
128 * was previously done for gen6.
129 *
130 * TODO: test with both disabled to see if the HW is behaving
131 * as expected, like in gen7.
132 */
133 OUT_BATCH(GEN6_GS_SPF_MODE | GEN6_GS_VECTOR_MASK_ENABLE |
134 ((ALIGN(stage_state->sampler_count, 4)/4) <<
135 GEN6_GS_SAMPLER_COUNT_SHIFT) |
136 ((prog_data->base.binding_table.size_bytes / 4) <<
137 GEN6_GS_BINDING_TABLE_ENTRY_COUNT_SHIFT));
138
139 if (prog_data->base.total_scratch) {
140 OUT_RELOC(stage_state->scratch_bo,
141 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
142 ffs(prog_data->base.total_scratch) - 11);
143 } else {
144 OUT_BATCH(0); /* no scratch space */
145 }
146
147 OUT_BATCH((prog_data->urb_read_length <<
148 GEN6_GS_URB_READ_LENGTH_SHIFT) |
149 (0 << GEN6_GS_URB_ENTRY_READ_OFFSET_SHIFT) |
150 (prog_data->base.dispatch_grf_start_reg <<
151 GEN6_GS_DISPATCH_START_GRF_SHIFT));
152
153 OUT_BATCH(((brw->max_gs_threads - 1) << GEN6_GS_MAX_THREADS_SHIFT) |
154 GEN6_GS_STATISTICS_ENABLE |
155 GEN6_GS_SO_STATISTICS_ENABLE |
156 GEN6_GS_RENDERING_ENABLE);
157
158 if (brw->gs.prog_data->gen6_xfb_enabled) {
159 /* GEN6_GS_REORDER is equivalent to GEN7_GS_REORDER_TRAILING
160 * in gen7. SNB and IVB specs are the same regarding the reordering of
161 * TRISTRIP/TRISTRIP_REV vertices and triangle orientation, so we do
162 * the same thing in both generations. For more details, see the
163 * comment in gen7_gs_state.c
164 */
165 OUT_BATCH(GEN6_GS_REORDER |
166 GEN6_GS_SVBI_PAYLOAD_ENABLE |
167 GEN6_GS_ENABLE);
168 } else {
169 OUT_BATCH(GEN6_GS_REORDER | GEN6_GS_ENABLE);
170 }
171 ADVANCE_BATCH();
172 } else if (brw->ff_gs.prog_active) {
173 /* In gen6, transform feedback for the VS stage is done with an ad-hoc GS
174 * program. This function provides the needed 3DSTATE_GS for this.
175 */
176 upload_gs_state_for_tf(brw);
177 } else {
178 /* No GS function required */
179 BEGIN_BATCH(7);
180 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
181 OUT_BATCH(0); /* prog_bo */
182 OUT_BATCH((0 << GEN6_GS_SAMPLER_COUNT_SHIFT) |
183 (0 << GEN6_GS_BINDING_TABLE_ENTRY_COUNT_SHIFT));
184 OUT_BATCH(0); /* scratch space base offset */
185 OUT_BATCH((1 << GEN6_GS_DISPATCH_START_GRF_SHIFT) |
186 (0 << GEN6_GS_URB_READ_LENGTH_SHIFT) |
187 (0 << GEN6_GS_URB_ENTRY_READ_OFFSET_SHIFT));
188 OUT_BATCH((0 << GEN6_GS_MAX_THREADS_SHIFT) |
189 GEN6_GS_STATISTICS_ENABLE |
190 GEN6_GS_RENDERING_ENABLE);
191 OUT_BATCH(0);
192 ADVANCE_BATCH();
193 }
194 brw->gs.enabled = active;
195 }
196
197 const struct brw_tracked_state gen6_gs_state = {
198 .dirty = {
199 .mesa = _NEW_PROGRAM_CONSTANTS |
200 _NEW_TRANSFORM,
201 .brw = BRW_NEW_BATCH |
202 BRW_NEW_CONTEXT |
203 BRW_NEW_FF_GS_PROG_DATA |
204 BRW_NEW_GEOMETRY_PROGRAM |
205 BRW_NEW_GS_PROG_DATA |
206 BRW_NEW_PUSH_CONSTANT_ALLOCATION,
207 },
208 .emit = upload_gs_state,
209 };