i965: Stop looking at NewDriverState when emitting 3DSTATE_URB
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_urb.c
1 /*
2 * Copyright © 2011 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/macros.h"
25 #include "intel_batchbuffer.h"
26 #include "brw_context.h"
27 #include "brw_state.h"
28 #include "brw_defines.h"
29
30 #include "common/gen_l3_config.h"
31
32 /**
33 * The following diagram shows how we partition the URB:
34 *
35 * 16kB or 32kB Rest of the URB space
36 * __________-__________ _________________-_________________
37 * / \ / \
38 * +-------------------------------------------------------------+
39 * | VS/HS/DS/GS/FS Push | VS/HS/DS/GS URB |
40 * | Constants | Entries |
41 * +-------------------------------------------------------------+
42 *
43 * Notably, push constants must be stored at the beginning of the URB
44 * space, while entries can be stored anywhere. Ivybridge and Haswell
45 * GT1/GT2 have a maximum constant buffer size of 16kB, while Haswell GT3
46 * doubles this (32kB).
47 *
48 * Ivybridge and Haswell GT1/GT2 allow push constants to be located (and
49 * sized) in increments of 1kB. Haswell GT3 requires them to be located and
50 * sized in increments of 2kB.
51 *
52 * Currently we split the constant buffer space evenly among whatever stages
53 * are active. This is probably not ideal, but simple.
54 *
55 * Ivybridge GT1 and Haswell GT1 have 128kB of URB space.
56 * Ivybridge GT2 and Haswell GT2 have 256kB of URB space.
57 * Haswell GT3 has 512kB of URB space.
58 *
59 * See "Volume 2a: 3D Pipeline," section 1.8, "Volume 1b: Configurations",
60 * and the documentation for 3DSTATE_PUSH_CONSTANT_ALLOC_xS.
61 */
62 static void
63 gen7_allocate_push_constants(struct brw_context *brw)
64 {
65 /* BRW_NEW_GEOMETRY_PROGRAM */
66 bool gs_present = brw->geometry_program;
67
68 /* BRW_NEW_TESS_PROGRAMS */
69 bool tess_present = brw->tess_eval_program;
70
71 unsigned avail_size = 16;
72 unsigned multiplier =
73 (brw->gen >= 8 || (brw->is_haswell && brw->gt == 3)) ? 2 : 1;
74
75 int stages = 2 + gs_present + 2 * tess_present;
76
77 /* Divide up the available space equally between stages. Because we
78 * round down (using floor division), there may be some left over
79 * space. We allocate that to the pixel shader stage.
80 */
81 unsigned size_per_stage = avail_size / stages;
82
83 unsigned vs_size = size_per_stage;
84 unsigned hs_size = tess_present ? size_per_stage : 0;
85 unsigned ds_size = tess_present ? size_per_stage : 0;
86 unsigned gs_size = gs_present ? size_per_stage : 0;
87 unsigned fs_size = avail_size - size_per_stage * (stages - 1);
88
89 gen7_emit_push_constant_state(brw, multiplier * vs_size,
90 multiplier * hs_size, multiplier * ds_size,
91 multiplier * gs_size, multiplier * fs_size);
92
93 /* From p115 of the Ivy Bridge PRM (3.2.1.4 3DSTATE_PUSH_CONSTANT_ALLOC_VS):
94 *
95 * Programming Restriction:
96 *
97 * The 3DSTATE_CONSTANT_VS must be reprogrammed prior to the next
98 * 3DPRIMITIVE command after programming the
99 * 3DSTATE_PUSH_CONSTANT_ALLOC_VS.
100 *
101 * Similar text exists for the other 3DSTATE_PUSH_CONSTANT_ALLOC_*
102 * commands.
103 */
104 brw->vs.base.push_constants_dirty = true;
105 brw->tcs.base.push_constants_dirty = true;
106 brw->tes.base.push_constants_dirty = true;
107 brw->gs.base.push_constants_dirty = true;
108 brw->wm.base.push_constants_dirty = true;
109 }
110
111 void
112 gen7_emit_push_constant_state(struct brw_context *brw, unsigned vs_size,
113 unsigned hs_size, unsigned ds_size,
114 unsigned gs_size, unsigned fs_size)
115 {
116 unsigned offset = 0;
117
118 BEGIN_BATCH(10);
119 OUT_BATCH(_3DSTATE_PUSH_CONSTANT_ALLOC_VS << 16 | (2 - 2));
120 OUT_BATCH(vs_size | offset << GEN7_PUSH_CONSTANT_BUFFER_OFFSET_SHIFT);
121 offset += vs_size;
122
123 OUT_BATCH(_3DSTATE_PUSH_CONSTANT_ALLOC_HS << 16 | (2 - 2));
124 OUT_BATCH(hs_size | offset << GEN7_PUSH_CONSTANT_BUFFER_OFFSET_SHIFT);
125 offset += hs_size;
126
127 OUT_BATCH(_3DSTATE_PUSH_CONSTANT_ALLOC_DS << 16 | (2 - 2));
128 OUT_BATCH(ds_size | offset << GEN7_PUSH_CONSTANT_BUFFER_OFFSET_SHIFT);
129 offset += ds_size;
130
131 OUT_BATCH(_3DSTATE_PUSH_CONSTANT_ALLOC_GS << 16 | (2 - 2));
132 OUT_BATCH(gs_size | offset << GEN7_PUSH_CONSTANT_BUFFER_OFFSET_SHIFT);
133 offset += gs_size;
134
135 OUT_BATCH(_3DSTATE_PUSH_CONSTANT_ALLOC_PS << 16 | (2 - 2));
136 OUT_BATCH(fs_size | offset << GEN7_PUSH_CONSTANT_BUFFER_OFFSET_SHIFT);
137 ADVANCE_BATCH();
138
139 /* From p292 of the Ivy Bridge PRM (11.2.4 3DSTATE_PUSH_CONSTANT_ALLOC_PS):
140 *
141 * A PIPE_CONTROL command with the CS Stall bit set must be programmed
142 * in the ring after this instruction.
143 *
144 * No such restriction exists for Haswell or Baytrail.
145 */
146 if (brw->gen < 8 && !brw->is_haswell && !brw->is_baytrail)
147 gen7_emit_cs_stall_flush(brw);
148 }
149
150 const struct brw_tracked_state gen7_push_constant_space = {
151 .dirty = {
152 .mesa = 0,
153 .brw = BRW_NEW_CONTEXT |
154 BRW_NEW_GEOMETRY_PROGRAM |
155 BRW_NEW_TESS_PROGRAMS,
156 },
157 .emit = gen7_allocate_push_constants,
158 };
159
160 static void
161 upload_urb(struct brw_context *brw)
162 {
163 /* BRW_NEW_VS_PROG_DATA */
164 const struct brw_vue_prog_data *vs_vue_prog_data =
165 brw_vue_prog_data(brw->vs.base.prog_data);
166 const unsigned vs_size = MAX2(vs_vue_prog_data->urb_entry_size, 1);
167 /* BRW_NEW_GS_PROG_DATA */
168 const bool gs_present = brw->gs.base.prog_data;
169 /* BRW_NEW_TES_PROG_DATA */
170 const bool tess_present = brw->tes.base.prog_data;
171
172 gen7_upload_urb(brw, vs_size, gs_present, tess_present);
173 }
174
175 void
176 gen7_upload_urb(struct brw_context *brw, unsigned vs_size,
177 bool gs_present, bool tess_present)
178 {
179 const struct gen_device_info *devinfo = &brw->screen->devinfo;
180 const int push_size_kB =
181 (brw->gen >= 8 || (brw->is_haswell && brw->gt == 3)) ? 32 : 16;
182
183 /* BRW_NEW_{VS,TCS,TES,GS}_PROG_DATA */
184 struct brw_vue_prog_data *prog_data[4] = {
185 [MESA_SHADER_VERTEX] =
186 brw_vue_prog_data(brw->vs.base.prog_data),
187 [MESA_SHADER_TESS_CTRL] =
188 tess_present ? brw_vue_prog_data(brw->tcs.base.prog_data) : NULL,
189 [MESA_SHADER_TESS_EVAL] =
190 tess_present ? brw_vue_prog_data(brw->tes.base.prog_data) : NULL,
191 [MESA_SHADER_GEOMETRY] =
192 gs_present ? brw_vue_prog_data(brw->gs.base.prog_data) : NULL,
193 };
194
195 unsigned entry_size[4];
196 entry_size[MESA_SHADER_VERTEX] = vs_size;
197 for (int i = MESA_SHADER_TESS_CTRL; i <= MESA_SHADER_GEOMETRY; i++) {
198 entry_size[i] = prog_data[i] ? prog_data[i]->urb_entry_size : 1;
199 }
200
201 /* If we're just switching between programs with the same URB requirements,
202 * skip the rest of the logic.
203 */
204 if (brw->urb.vsize == entry_size[MESA_SHADER_VERTEX] &&
205 brw->urb.gs_present == gs_present &&
206 brw->urb.gsize == entry_size[MESA_SHADER_GEOMETRY] &&
207 brw->urb.tess_present == tess_present &&
208 brw->urb.hsize == entry_size[MESA_SHADER_TESS_CTRL] &&
209 brw->urb.dsize == entry_size[MESA_SHADER_TESS_EVAL]) {
210 return;
211 }
212 brw->urb.vsize = entry_size[MESA_SHADER_VERTEX];
213 brw->urb.gs_present = gs_present;
214 brw->urb.gsize = entry_size[MESA_SHADER_GEOMETRY];
215 brw->urb.tess_present = tess_present;
216 brw->urb.hsize = entry_size[MESA_SHADER_TESS_CTRL];
217 brw->urb.dsize = entry_size[MESA_SHADER_TESS_EVAL];
218
219 unsigned entries[4];
220 unsigned start[4];
221 gen_get_urb_config(devinfo, 1024 * push_size_kB, 1024 * brw->urb.size,
222 tess_present, gs_present, entry_size, entries, start);
223
224 if (brw->gen == 7 && !brw->is_haswell && !brw->is_baytrail)
225 gen7_emit_vs_workaround_flush(brw);
226
227 BEGIN_BATCH(8);
228 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
229 assert(brw->gen != 10 || entry_size[i] % 3);
230 OUT_BATCH((_3DSTATE_URB_VS + i) << 16 | (2 - 2));
231 OUT_BATCH(entries[i] |
232 ((entry_size[i] - 1) << GEN7_URB_ENTRY_SIZE_SHIFT) |
233 (start[i] << GEN7_URB_STARTING_ADDRESS_SHIFT));
234 }
235 ADVANCE_BATCH();
236 }
237
238 const struct brw_tracked_state gen7_urb = {
239 .dirty = {
240 .mesa = 0,
241 .brw = BRW_NEW_BLORP |
242 BRW_NEW_CONTEXT |
243 BRW_NEW_URB_SIZE |
244 BRW_NEW_GS_PROG_DATA |
245 BRW_NEW_TCS_PROG_DATA |
246 BRW_NEW_TES_PROG_DATA |
247 BRW_NEW_VS_PROG_DATA,
248 },
249 .emit = upload_urb,
250 };