i965: Set Broadwell MOCS values everywhere it's possible.
[mesa.git] / src / mesa / drivers / dri / i965 / gen8_draw_upload.c
1 /*
2 * Copyright © 2012 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
24 #include "main/glheader.h"
25 #include "main/bufferobj.h"
26 #include "main/context.h"
27 #include "main/enums.h"
28 #include "main/macros.h"
29
30 #include "brw_draw.h"
31 #include "brw_defines.h"
32 #include "brw_context.h"
33 #include "brw_state.h"
34
35 #include "intel_batchbuffer.h"
36 #include "intel_buffer_objects.h"
37
38 static void
39 gen8_emit_vertices(struct brw_context *brw)
40 {
41 struct gl_context *ctx = &brw->ctx;
42
43 brw_prepare_vertices(brw);
44
45 if (brw->vs.prog_data->uses_vertexid) {
46 unsigned vue = brw->vb.nr_enabled;
47
48 WARN_ONCE(brw->vs.prog_data->inputs_read & VERT_BIT_EDGEFLAG,
49 "Using VID/IID with edgeflags, need to reorder the "
50 "vertex attributes");
51 WARN_ONCE(vue >= 33,
52 "Trying to insert VID/IID past 33rd vertex element, "
53 "need to reorder the vertex attrbutes.");
54
55 BEGIN_BATCH(2);
56 OUT_BATCH(_3DSTATE_VF_SGVS << 16 | (2 - 2));
57 OUT_BATCH(GEN8_SGVS_ENABLE_VERTEX_ID |
58 (0 << GEN8_SGVS_VERTEX_ID_COMPONENT_SHIFT) | /* .x channel */
59 (vue << GEN8_SGVS_VERTEX_ID_ELEMENT_OFFSET_SHIFT) |
60 GEN8_SGVS_ENABLE_INSTANCE_ID |
61 (1 << GEN8_SGVS_INSTANCE_ID_COMPONENT_SHIFT) | /* .y channel */
62 (vue << GEN8_SGVS_INSTANCE_ID_ELEMENT_OFFSET_SHIFT));
63 ADVANCE_BATCH();
64 } else {
65 BEGIN_BATCH(2);
66 OUT_BATCH(_3DSTATE_VF_SGVS << 16 | (2 - 2));
67 OUT_BATCH(0);
68 ADVANCE_BATCH();
69 }
70
71 /* If the VS doesn't read any inputs (calculating vertex position from
72 * a state variable for some reason, for example), emit a single pad
73 * VERTEX_ELEMENT struct and bail.
74 *
75 * The stale VB state stays in place, but they don't do anything unless
76 * a VE loads from them.
77 */
78 if (brw->vb.nr_enabled == 0) {
79 BEGIN_BATCH(3);
80 OUT_BATCH((_3DSTATE_VERTEX_ELEMENTS << 16) | (3 - 2));
81 OUT_BATCH((0 << GEN6_VE0_INDEX_SHIFT) |
82 GEN6_VE0_VALID |
83 (BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_VE0_FORMAT_SHIFT) |
84 (0 << BRW_VE0_SRC_OFFSET_SHIFT));
85 OUT_BATCH((BRW_VE1_COMPONENT_STORE_0 << BRW_VE1_COMPONENT_0_SHIFT) |
86 (BRW_VE1_COMPONENT_STORE_0 << BRW_VE1_COMPONENT_1_SHIFT) |
87 (BRW_VE1_COMPONENT_STORE_0 << BRW_VE1_COMPONENT_2_SHIFT) |
88 (BRW_VE1_COMPONENT_STORE_1_FLT << BRW_VE1_COMPONENT_3_SHIFT));
89 ADVANCE_BATCH();
90 return;
91 }
92
93 /* Now emit 3DSTATE_VERTEX_BUFFERS and 3DSTATE_VERTEX_ELEMENTS packets. */
94 if (brw->vb.nr_buffers) {
95 assert(brw->vb.nr_buffers <= 33);
96
97 perf_debug("Missing MOCS setup for 3DSTATE_VERTEX_BUFFERS.");
98
99 BEGIN_BATCH(1 + 4*brw->vb.nr_buffers);
100 OUT_BATCH((_3DSTATE_VERTEX_BUFFERS << 16) | (4*brw->vb.nr_buffers - 1));
101 for (unsigned i = 0; i < brw->vb.nr_buffers; i++) {
102 struct brw_vertex_buffer *buffer = &brw->vb.buffers[i];
103 uint32_t dw0 = 0;
104
105 dw0 |= i << GEN6_VB0_INDEX_SHIFT;
106 dw0 |= GEN7_VB0_ADDRESS_MODIFYENABLE;
107 dw0 |= buffer->stride << BRW_VB0_PITCH_SHIFT;
108 dw0 |= BDW_MOCS_WB << 16;
109
110 OUT_BATCH(dw0);
111 OUT_RELOC64(buffer->bo, I915_GEM_DOMAIN_VERTEX, 0, buffer->offset);
112 OUT_BATCH(buffer->bo->size);
113 }
114 ADVANCE_BATCH();
115 }
116
117 unsigned nr_elements = brw->vb.nr_enabled;
118
119 /* The hardware allows one more VERTEX_ELEMENTS than VERTEX_BUFFERS,
120 * presumably for VertexID/InstanceID.
121 */
122 assert(nr_elements <= 34);
123
124 struct brw_vertex_element *gen6_edgeflag_input = NULL;
125
126 BEGIN_BATCH(1 + nr_elements * 2);
127 OUT_BATCH((_3DSTATE_VERTEX_ELEMENTS << 16) | (2 * nr_elements - 1));
128 for (unsigned i = 0; i < brw->vb.nr_enabled; i++) {
129 struct brw_vertex_element *input = brw->vb.enabled[i];
130 uint32_t format = brw_get_vertex_surface_type(brw, input->glarray);
131 uint32_t comp0 = BRW_VE1_COMPONENT_STORE_SRC;
132 uint32_t comp1 = BRW_VE1_COMPONENT_STORE_SRC;
133 uint32_t comp2 = BRW_VE1_COMPONENT_STORE_SRC;
134 uint32_t comp3 = BRW_VE1_COMPONENT_STORE_SRC;
135
136 /* The gen4 driver expects edgeflag to come in as a float, and passes
137 * that float on to the tests in the clipper. Mesa's current vertex
138 * attribute value for EdgeFlag is stored as a float, which works out.
139 * glEdgeFlagPointer, on the other hand, gives us an unnormalized
140 * integer ubyte. Just rewrite that to convert to a float.
141 */
142 if (input->attrib == VERT_ATTRIB_EDGEFLAG) {
143 /* Gen6+ passes edgeflag as sideband along with the vertex, instead
144 * of in the VUE. We have to upload it sideband as the last vertex
145 * element according to the B-Spec.
146 */
147 gen6_edgeflag_input = input;
148 continue;
149 }
150
151 switch (input->glarray->Size) {
152 case 0: comp0 = BRW_VE1_COMPONENT_STORE_0;
153 case 1: comp1 = BRW_VE1_COMPONENT_STORE_0;
154 case 2: comp2 = BRW_VE1_COMPONENT_STORE_0;
155 case 3: comp3 = input->glarray->Integer ? BRW_VE1_COMPONENT_STORE_1_INT
156 : BRW_VE1_COMPONENT_STORE_1_FLT;
157 break;
158 }
159
160 OUT_BATCH((input->buffer << GEN6_VE0_INDEX_SHIFT) |
161 GEN6_VE0_VALID |
162 (format << BRW_VE0_FORMAT_SHIFT) |
163 (input->offset << BRW_VE0_SRC_OFFSET_SHIFT));
164
165 OUT_BATCH((comp0 << BRW_VE1_COMPONENT_0_SHIFT) |
166 (comp1 << BRW_VE1_COMPONENT_1_SHIFT) |
167 (comp2 << BRW_VE1_COMPONENT_2_SHIFT) |
168 (comp3 << BRW_VE1_COMPONENT_3_SHIFT));
169 }
170
171 if (gen6_edgeflag_input) {
172 uint32_t format =
173 brw_get_vertex_surface_type(brw, gen6_edgeflag_input->glarray);
174
175 OUT_BATCH((gen6_edgeflag_input->buffer << GEN6_VE0_INDEX_SHIFT) |
176 GEN6_VE0_VALID |
177 GEN6_VE0_EDGE_FLAG_ENABLE |
178 (format << BRW_VE0_FORMAT_SHIFT) |
179 (gen6_edgeflag_input->offset << BRW_VE0_SRC_OFFSET_SHIFT));
180 OUT_BATCH((BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_0_SHIFT) |
181 (BRW_VE1_COMPONENT_STORE_0 << BRW_VE1_COMPONENT_1_SHIFT) |
182 (BRW_VE1_COMPONENT_STORE_0 << BRW_VE1_COMPONENT_2_SHIFT) |
183 (BRW_VE1_COMPONENT_STORE_0 << BRW_VE1_COMPONENT_3_SHIFT));
184 }
185 ADVANCE_BATCH();
186
187 for (unsigned i = 0; i < brw->vb.nr_enabled; i++) {
188 const struct brw_vertex_element *input = brw->vb.enabled[i];
189 const struct brw_vertex_buffer *buffer = &brw->vb.buffers[input->buffer];
190
191 BEGIN_BATCH(3);
192 OUT_BATCH(_3DSTATE_VF_INSTANCING << 16 | (3 - 2));
193 OUT_BATCH(i | (buffer->step_rate ? GEN8_VF_INSTANCING_ENABLE : 0));
194 OUT_BATCH(buffer->step_rate);
195 ADVANCE_BATCH();
196 }
197 }
198
199 const struct brw_tracked_state gen8_vertices = {
200 .dirty = {
201 .mesa = _NEW_POLYGON,
202 .brw = BRW_NEW_BATCH | BRW_NEW_VERTICES,
203 .cache = CACHE_NEW_VS_PROG,
204 },
205 .emit = gen8_emit_vertices,
206 };
207
208 static void
209 gen8_emit_index_buffer(struct brw_context *brw)
210 {
211 const struct _mesa_index_buffer *index_buffer = brw->ib.ib;
212
213 if (index_buffer == NULL)
214 return;
215
216 perf_debug("Missing MOCS setup for 3DSTATE_INDEX_BUFFER.");
217
218 BEGIN_BATCH(5);
219 OUT_BATCH(CMD_INDEX_BUFFER << 16 | (5 - 2));
220 OUT_BATCH(brw_get_index_type(index_buffer->type) << 8);
221 OUT_RELOC64(brw->ib.bo, I915_GEM_DOMAIN_VERTEX, 0, 0);
222 OUT_BATCH(brw->ib.bo->size);
223 ADVANCE_BATCH();
224 }
225
226 const struct brw_tracked_state gen8_index_buffer = {
227 .dirty = {
228 .mesa = 0,
229 .brw = BRW_NEW_BATCH | BRW_NEW_INDEX_BUFFER,
230 .cache = 0,
231 },
232 .emit = gen8_emit_index_buffer,
233 };
234
235 static void
236 gen8_emit_vf_topology(struct brw_context *brw)
237 {
238 BEGIN_BATCH(2);
239 OUT_BATCH(_3DSTATE_VF_TOPOLOGY << 16 | (2 - 2));
240 OUT_BATCH(brw->primitive);
241 ADVANCE_BATCH();
242 }
243
244 const struct brw_tracked_state gen8_vf_topology = {
245 .dirty = {
246 .mesa = 0,
247 .brw = BRW_NEW_PRIMITIVE,
248 .cache = 0,
249 },
250 .emit = gen8_emit_vf_topology,
251 };