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