i965g: more work on compiling
[mesa.git] / src / gallium / drivers / i965 / brw_curbe.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include "util/u_memory.h"
33 #include "util/u_math.h"
34
35 #include "brw_batchbuffer.h"
36 #include "brw_context.h"
37 #include "brw_defines.h"
38 #include "brw_state.h"
39 #include "brw_util.h"
40 #include "brw_debug.h"
41 #include "brw_screen.h"
42
43
44 /**
45 * Partition the CURBE between the various users of constant values:
46 * Note that vertex and fragment shaders can now fetch constants out
47 * of constant buffers. We no longer allocatea block of the GRF for
48 * constants. That greatly reduces the demand for space in the CURBE.
49 * Some of the comments within are dated...
50 */
51 static void calculate_curbe_offsets( struct brw_context *brw )
52 {
53 /* CACHE_NEW_WM_PROG */
54 const GLuint nr_fp_regs = (brw->wm.prog_data->nr_params + 15) / 16;
55
56 /* BRW_NEW_VERTEX_PROGRAM */
57 const GLuint nr_vp_regs = (brw->vs.prog_data->nr_params + 15) / 16;
58 GLuint nr_clip_regs = 0;
59 GLuint total_regs;
60
61 /* PIPE_NEW_CLIP */
62 if (brw->curr.ucp.nr) {
63 GLuint nr_planes = 6 + brw->curr.ucp.nr;
64 nr_clip_regs = (nr_planes * 4 + 15) / 16;
65 }
66
67
68 total_regs = nr_fp_regs + nr_vp_regs + nr_clip_regs;
69
70 /* When this is > 32, want to use a true constant buffer to hold
71 * the extra constants.
72 */
73 assert(total_regs <= 32);
74
75 /* Lazy resize:
76 */
77 if (nr_fp_regs > brw->curbe.wm_size ||
78 nr_vp_regs > brw->curbe.vs_size ||
79 nr_clip_regs != brw->curbe.clip_size ||
80 (total_regs < brw->curbe.total_size / 4 &&
81 brw->curbe.total_size > 16)) {
82
83 GLuint reg = 0;
84
85 /* Calculate a new layout:
86 */
87 reg = 0;
88 brw->curbe.wm_start = reg;
89 brw->curbe.wm_size = nr_fp_regs; reg += nr_fp_regs;
90 brw->curbe.clip_start = reg;
91 brw->curbe.clip_size = nr_clip_regs; reg += nr_clip_regs;
92 brw->curbe.vs_start = reg;
93 brw->curbe.vs_size = nr_vp_regs; reg += nr_vp_regs;
94 brw->curbe.total_size = reg;
95
96 if (BRW_DEBUG & DEBUG_CURBE)
97 debug_printf("curbe wm %d+%d clip %d+%d vs %d+%d\n",
98 brw->curbe.wm_start,
99 brw->curbe.wm_size,
100 brw->curbe.clip_start,
101 brw->curbe.clip_size,
102 brw->curbe.vs_start,
103 brw->curbe.vs_size );
104
105 brw->state.dirty.brw |= BRW_NEW_CURBE_OFFSETS;
106 }
107 }
108
109
110 const struct brw_tracked_state brw_curbe_offsets = {
111 .dirty = {
112 .mesa = PIPE_NEW_CLIP,
113 .brw = BRW_NEW_VERTEX_PROGRAM,
114 .cache = CACHE_NEW_WM_PROG
115 },
116 .prepare = calculate_curbe_offsets
117 };
118
119
120
121
122 /* Define the number of curbes within CS's urb allocation. Multiple
123 * urb entries -> multiple curbes. These will be used by
124 * fixed-function hardware in a double-buffering scheme to avoid a
125 * pipeline stall each time the contents of the curbe is changed.
126 */
127 void brw_upload_cs_urb_state(struct brw_context *brw)
128 {
129 struct brw_cs_urb_state cs_urb;
130 memset(&cs_urb, 0, sizeof(cs_urb));
131
132 /* It appears that this is the state packet for the CS unit, ie. the
133 * urb entries detailed here are housed in the CS range from the
134 * URB_FENCE command.
135 */
136 cs_urb.header.opcode = CMD_CS_URB_STATE;
137 cs_urb.header.length = sizeof(cs_urb)/4 - 2;
138
139 /* BRW_NEW_URB_FENCE */
140 cs_urb.bits0.nr_urb_entries = brw->urb.nr_cs_entries;
141 cs_urb.bits0.urb_entry_size = brw->urb.csize - 1;
142
143 assert(brw->urb.nr_cs_entries);
144 BRW_CACHED_BATCH_STRUCT(brw, &cs_urb);
145 }
146
147 static GLfloat fixed_plane[6][4] = {
148 { 0, 0, -1, 1 },
149 { 0, 0, 1, 1 },
150 { 0, -1, 0, 1 },
151 { 0, 1, 0, 1 },
152 {-1, 0, 0, 1 },
153 { 1, 0, 0, 1 }
154 };
155
156 /* Upload a new set of constants. Too much variability to go into the
157 * cache mechanism, but maybe would benefit from a comparison against
158 * the current uploaded set of constants.
159 */
160 static void prepare_constant_buffer(struct brw_context *brw)
161 {
162 const GLuint sz = brw->curbe.total_size;
163 const GLuint bufsz = sz * 16 * sizeof(GLfloat);
164 GLfloat *buf;
165 GLuint i;
166
167 if (sz == 0) {
168 if (brw->curbe.last_buf) {
169 free(brw->curbe.last_buf);
170 brw->curbe.last_buf = NULL;
171 brw->curbe.last_bufsz = 0;
172 }
173 return;
174 }
175
176 buf = (GLfloat *) CALLOC(bufsz, 1);
177
178 /* fragment shader constants */
179 if (brw->curbe.wm_size) {
180 GLuint offset = brw->curbe.wm_start * 16;
181
182 /* map fs constant buffer */
183
184 /* copy float constants */
185 for (i = 0; i < brw->wm.prog_data->nr_params; i++)
186 buf[offset + i] = *brw->wm.prog_data->param[i];
187
188 /* unmap fs constant buffer */
189 }
190
191
192 /* The clipplanes are actually delivered to both CLIP and VS units.
193 * VS uses them to calculate the outcode bitmasks.
194 */
195 if (brw->curbe.clip_size) {
196 GLuint offset = brw->curbe.clip_start * 16;
197 GLuint j;
198
199 /* If any planes are going this way, send them all this way:
200 */
201 for (i = 0; i < 6; i++) {
202 buf[offset + i * 4 + 0] = fixed_plane[i][0];
203 buf[offset + i * 4 + 1] = fixed_plane[i][1];
204 buf[offset + i * 4 + 2] = fixed_plane[i][2];
205 buf[offset + i * 4 + 3] = fixed_plane[i][3];
206 }
207
208 /* Clip planes:
209 */
210 assert(brw->curr.ucp.nr <= 6);
211 for (j = 0; j < brw->curr.ucp.nr; j++) {
212 buf[offset + i * 4 + 0] = brw->curr.ucp.ucp[j][0];
213 buf[offset + i * 4 + 1] = brw->curr.ucp.ucp[j][1];
214 buf[offset + i * 4 + 2] = brw->curr.ucp.ucp[j][2];
215 buf[offset + i * 4 + 3] = brw->curr.ucp.ucp[j][3];
216 i++;
217 }
218 }
219
220 /* vertex shader constants */
221 if (brw->curbe.vs_size) {
222 GLuint offset = brw->curbe.vs_start * 16;
223 GLuint nr = brw->curr.vertex_shader->info.file_max[TGSI_FILE_CONSTANT];
224 struct pipe_screen *screen = &brw->brw_screen->base;
225
226 const GLfloat *value = screen->buffer_map( screen,
227 brw->curr.vertex_constants,
228 PIPE_BUFFER_USAGE_CPU_READ);
229
230 /* XXX: what if user's constant buffer is too small?
231 */
232 memcpy(&buf[offset], value, nr * 4 * sizeof(float));
233
234 screen->buffer_unmap( screen, brw->curr.vertex_constants );
235 }
236
237 if (BRW_DEBUG & DEBUG_CURBE) {
238 for (i = 0; i < sz*16; i+=4)
239 debug_printf("curbe %d.%d: %f %f %f %f\n", i/8, i&4,
240 buf[i+0], buf[i+1], buf[i+2], buf[i+3]);
241
242 debug_printf("last_buf %p buf %p sz %d/%d cmp %d\n",
243 brw->curbe.last_buf, buf,
244 bufsz, brw->curbe.last_bufsz,
245 brw->curbe.last_buf ? memcmp(buf, brw->curbe.last_buf, bufsz) : -1);
246 }
247
248 if (brw->curbe.curbe_bo != NULL &&
249 brw->curbe.last_buf &&
250 bufsz == brw->curbe.last_bufsz &&
251 memcmp(buf, brw->curbe.last_buf, bufsz) == 0) {
252 /* constants have not changed */
253 FREE(buf);
254 }
255 else {
256 /* constants have changed */
257 if (brw->curbe.last_buf)
258 FREE(brw->curbe.last_buf);
259
260 brw->curbe.last_buf = buf;
261 brw->curbe.last_bufsz = bufsz;
262
263 if (brw->curbe.curbe_bo != NULL &&
264 (brw->curbe.need_new_bo ||
265 brw->curbe.curbe_next_offset + bufsz > brw->curbe.curbe_bo->size))
266 {
267 brw->sws->bo_unreference(brw->curbe.curbe_bo);
268 brw->curbe.curbe_bo = NULL;
269 }
270
271 if (brw->curbe.curbe_bo == NULL) {
272 /* Allocate a single page for CURBE entries for this batchbuffer.
273 * They're generally around 64b.
274 */
275 brw->curbe.curbe_bo = brw->sws->bo_alloc(brw->sws,
276 BRW_BUFFER_TYPE_CURBE,
277 4096, 1 << 6);
278 brw->curbe.curbe_next_offset = 0;
279 }
280
281 brw->curbe.curbe_offset = brw->curbe.curbe_next_offset;
282 brw->curbe.curbe_next_offset += bufsz;
283 brw->curbe.curbe_next_offset = align(brw->curbe.curbe_next_offset, 64);
284
285 /* Copy data to the buffer:
286 */
287 brw->sws->bo_subdata(brw->curbe.curbe_bo,
288 brw->curbe.curbe_offset,
289 bufsz,
290 buf);
291 }
292
293 brw_add_validated_bo(brw, brw->curbe.curbe_bo);
294
295 /* Because this provokes an action (ie copy the constants into the
296 * URB), it shouldn't be shortcircuited if identical to the
297 * previous time - because eg. the urb destination may have
298 * changed, or the urb contents different to last time.
299 *
300 * Note that the data referred to is actually copied internally,
301 * not just used in place according to passed pointer.
302 *
303 * It appears that the CS unit takes care of using each available
304 * URB entry (Const URB Entry == CURBE) in turn, and issuing
305 * flushes as necessary when doublebuffering of CURBEs isn't
306 * possible.
307 */
308 }
309
310 static void emit_constant_buffer(struct brw_context *brw)
311 {
312 GLuint sz = brw->curbe.total_size;
313
314 BEGIN_BATCH(2, IGNORE_CLIPRECTS);
315 if (sz == 0) {
316 OUT_BATCH((CMD_CONST_BUFFER << 16) | (2 - 2));
317 OUT_BATCH(0);
318 } else {
319 OUT_BATCH((CMD_CONST_BUFFER << 16) | (1 << 8) | (2 - 2));
320 OUT_RELOC(brw->curbe.curbe_bo,
321 I915_GEM_DOMAIN_INSTRUCTION, 0,
322 (sz - 1) + brw->curbe.curbe_offset);
323 }
324 ADVANCE_BATCH();
325 }
326
327 const struct brw_tracked_state brw_constant_buffer = {
328 .dirty = {
329 .mesa = (PIPE_NEW_FRAGMENT_CONSTANTS |
330 PIPE_NEW_VERTEX_CONSTANTS |
331 PIPE_NEW_CLIP),
332 .brw = (BRW_NEW_FRAGMENT_PROGRAM |
333 BRW_NEW_VERTEX_PROGRAM |
334 BRW_NEW_URB_FENCE | /* Implicit - hardware requires this, not used above */
335 BRW_NEW_PSP | /* Implicit - hardware requires this, not used above */
336 BRW_NEW_CURBE_OFFSETS |
337 BRW_NEW_BATCH),
338 .cache = (CACHE_NEW_WM_PROG)
339 },
340 .prepare = prepare_constant_buffer,
341 .emit = emit_constant_buffer,
342 };
343