i965/gen6/gs: Setup constant push buffers for gen6 geometry shaders.
[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 /* BRW_NEW_GEOMETRY_PROGRAM */
37 const struct brw_geometry_program *gp =
38 (struct brw_geometry_program *) brw->geometry_program;
39
40 if (gp) {
41 /* CACHE_NEW_GS_PROG */
42 struct brw_stage_state *stage_state = &brw->gs.base;
43 struct brw_stage_prog_data *prog_data = &brw->gs.prog_data->base.base;
44
45 gen6_upload_push_constants(brw, &gp->program.Base, prog_data,
46 stage_state, AUB_TRACE_VS_CONSTANTS);
47 }
48 }
49
50 const struct brw_tracked_state gen6_gs_push_constants = {
51 .dirty = {
52 .mesa = _NEW_TRANSFORM | _NEW_PROGRAM_CONSTANTS,
53 .brw = (BRW_NEW_BATCH |
54 BRW_NEW_GEOMETRY_PROGRAM |
55 BRW_NEW_PUSH_CONSTANT_ALLOCATION),
56 .cache = CACHE_NEW_GS_PROG,
57 },
58 .emit = gen6_upload_gs_push_constants,
59 };
60
61 static void
62 upload_gs_state_for_tf(struct brw_context *brw)
63 {
64 BEGIN_BATCH(7);
65 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
66 OUT_BATCH(brw->ff_gs.prog_offset);
67 OUT_BATCH(GEN6_GS_SPF_MODE | GEN6_GS_VECTOR_MASK_ENABLE);
68 OUT_BATCH(0); /* no scratch space */
69 OUT_BATCH((2 << GEN6_GS_DISPATCH_START_GRF_SHIFT) |
70 (brw->ff_gs.prog_data->urb_read_length << GEN6_GS_URB_READ_LENGTH_SHIFT));
71 OUT_BATCH(((brw->max_gs_threads - 1) << GEN6_GS_MAX_THREADS_SHIFT) |
72 GEN6_GS_STATISTICS_ENABLE |
73 GEN6_GS_SO_STATISTICS_ENABLE |
74 GEN6_GS_RENDERING_ENABLE);
75 OUT_BATCH(GEN6_GS_SVBI_PAYLOAD_ENABLE |
76 GEN6_GS_SVBI_POSTINCREMENT_ENABLE |
77 (brw->ff_gs.prog_data->svbi_postincrement_value <<
78 GEN6_GS_SVBI_POSTINCREMENT_VALUE_SHIFT) |
79 GEN6_GS_ENABLE);
80 ADVANCE_BATCH();
81 }
82
83 static void
84 upload_gs_state(struct brw_context *brw)
85 {
86 /* BRW_NEW_GEOMETRY_PROGRAM */
87 bool active = brw->geometry_program;
88 /* CACHE_NEW_GS_PROG */
89 const struct brw_vec4_prog_data *prog_data = &brw->gs.prog_data->base;
90 const struct brw_stage_state *stage_state = &brw->gs.base;
91
92 if (!active || stage_state->push_const_size == 0) {
93 /* Disable the push constant buffers. */
94 BEGIN_BATCH(5);
95 OUT_BATCH(_3DSTATE_CONSTANT_GS << 16 | (5 - 2));
96 OUT_BATCH(0);
97 OUT_BATCH(0);
98 OUT_BATCH(0);
99 OUT_BATCH(0);
100 ADVANCE_BATCH();
101 } else {
102 BEGIN_BATCH(5);
103 OUT_BATCH(_3DSTATE_CONSTANT_GS << 16 |
104 GEN6_CONSTANT_BUFFER_0_ENABLE |
105 (5 - 2));
106 /* Pointer to the GS constant buffer. Covered by the set of
107 * state flags from gen6_upload_vs_constants
108 */
109 OUT_BATCH(stage_state->push_const_offset +
110 stage_state->push_const_size - 1);
111 OUT_BATCH(0);
112 OUT_BATCH(0);
113 OUT_BATCH(0);
114 ADVANCE_BATCH();
115 }
116
117 if (active) {
118 BEGIN_BATCH(7);
119 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
120 OUT_BATCH(stage_state->prog_offset);
121
122 /* GEN6_GS_SPF_MODE and GEN6_GS_VECTOR_MASK_ENABLE are enabled as it
123 * was previously done for gen6.
124 *
125 * TODO: test with both disabled to see if the HW is behaving
126 * as expected, like in gen7.
127 */
128 OUT_BATCH(GEN6_GS_SPF_MODE | GEN6_GS_VECTOR_MASK_ENABLE |
129 ((ALIGN(stage_state->sampler_count, 4)/4) <<
130 GEN6_GS_SAMPLER_COUNT_SHIFT) |
131 ((prog_data->base.binding_table.size_bytes / 4) <<
132 GEN6_GS_BINDING_TABLE_ENTRY_COUNT_SHIFT));
133
134 if (prog_data->base.total_scratch) {
135 OUT_RELOC(stage_state->scratch_bo,
136 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
137 ffs(prog_data->base.total_scratch) - 11);
138 } else {
139 OUT_BATCH(0); /* no scratch space */
140 }
141
142 OUT_BATCH((prog_data->urb_read_length <<
143 GEN6_GS_URB_READ_LENGTH_SHIFT) |
144 (0 << GEN6_GS_URB_ENTRY_READ_OFFSET_SHIFT) |
145 (prog_data->base.dispatch_grf_start_reg <<
146 GEN6_GS_DISPATCH_START_GRF_SHIFT));
147
148 OUT_BATCH(((brw->max_gs_threads - 1) << GEN6_GS_MAX_THREADS_SHIFT) |
149 GEN6_GS_STATISTICS_ENABLE |
150 GEN6_GS_SO_STATISTICS_ENABLE |
151 GEN6_GS_RENDERING_ENABLE);
152
153 /* FIXME: Enable SVBI payload only when TF is enable in SNB for
154 * user-provided GS.
155 */
156 if (0) {
157 /* GEN6_GS_REORDER is equivalent to GEN7_GS_REORDER_TRAILING
158 * in gen7. SNB and IVB specs are the same regarding the reordering of
159 * TRISTRIP/TRISTRIP_REV vertices and triangle orientation, so we do
160 * the same thing in both generations. For more details, see the
161 * comment in gen7_gs_state.c
162 */
163 OUT_BATCH(GEN6_GS_REORDER |
164 GEN6_GS_SVBI_PAYLOAD_ENABLE |
165 GEN6_GS_SVBI_POSTINCREMENT_ENABLE |
166 /* FIXME: prog_data->svbi_postincrement_value instead of 0 */
167 (0 << GEN6_GS_SVBI_POSTINCREMENT_VALUE_SHIFT) |
168 GEN6_GS_ENABLE);
169 } else {
170 OUT_BATCH(GEN6_GS_REORDER | GEN6_GS_ENABLE);
171 }
172 ADVANCE_BATCH();
173 } else if (brw->ff_gs.prog_active) {
174 /* In gen6, transform feedback for the VS stage is done with an ad-hoc GS
175 * program. This function provides the needed 3DSTATE_GS for this.
176 */
177 upload_gs_state_for_tf(brw);
178 } else {
179 /* No GS function required */
180 BEGIN_BATCH(7);
181 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
182 OUT_BATCH(0); /* prog_bo */
183 OUT_BATCH((0 << GEN6_GS_SAMPLER_COUNT_SHIFT) |
184 (0 << GEN6_GS_BINDING_TABLE_ENTRY_COUNT_SHIFT));
185 OUT_BATCH(0); /* scratch space base offset */
186 OUT_BATCH((1 << GEN6_GS_DISPATCH_START_GRF_SHIFT) |
187 (0 << GEN6_GS_URB_READ_LENGTH_SHIFT) |
188 (0 << GEN6_GS_URB_ENTRY_READ_OFFSET_SHIFT));
189 OUT_BATCH((0 << GEN6_GS_MAX_THREADS_SHIFT) |
190 GEN6_GS_STATISTICS_ENABLE |
191 GEN6_GS_RENDERING_ENABLE);
192 OUT_BATCH(0);
193 ADVANCE_BATCH();
194 }
195 brw->gs.enabled = active;
196 }
197
198 const struct brw_tracked_state gen6_gs_state = {
199 .dirty = {
200 .mesa = _NEW_TRANSFORM | _NEW_PROGRAM_CONSTANTS,
201 .brw = BRW_NEW_CONTEXT | BRW_NEW_PUSH_CONSTANT_ALLOCATION,
202 .cache = (CACHE_NEW_GS_PROG | CACHE_NEW_FF_GS_PROG)
203 },
204 .emit = upload_gs_state,
205 };