4b215a001c4cf1bce1d00277c0c2a9bda35b40ca
[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_debug.h"
40
41
42 /**
43 * Partition the CURBE between the various users of constant values:
44 * Note that vertex and fragment shaders can now fetch constants out
45 * of constant buffers. We no longer allocatea block of the GRF for
46 * constants. That greatly reduces the demand for space in the CURBE.
47 * Some of the comments within are dated...
48 */
49 static int calculate_curbe_offsets( struct brw_context *brw )
50 {
51 /* CACHE_NEW_WM_PROG */
52 const GLuint nr_fp_regs = brw->wm.prog_data->curb_read_length;
53
54 /* BRW_NEW_VERTEX_PROGRAM */
55 const GLuint nr_vp_regs = brw->vs.prog_data->curb_read_length;
56 GLuint nr_clip_regs = 0;
57 GLuint total_regs;
58
59 /* PIPE_NEW_CLIP */
60 if (brw->curr.ucp.nr) {
61 GLuint nr_planes = 6 + brw->curr.ucp.nr;
62 nr_clip_regs = (nr_planes * 4 + 15) / 16;
63 }
64
65
66 total_regs = nr_fp_regs + nr_vp_regs + nr_clip_regs;
67
68 /* When this is > 32, want to use a true constant buffer to hold
69 * the extra constants.
70 */
71 assert(total_regs <= 32);
72
73 /* Lazy resize:
74 */
75 if (nr_fp_regs > brw->curbe.wm_size ||
76 nr_vp_regs > brw->curbe.vs_size ||
77 nr_clip_regs != brw->curbe.clip_size ||
78 (total_regs < brw->curbe.total_size / 4 &&
79 brw->curbe.total_size > 16)) {
80
81 GLuint reg = 0;
82
83 /* Calculate a new layout:
84 */
85 reg = 0;
86 brw->curbe.wm_start = reg;
87 brw->curbe.wm_size = nr_fp_regs; reg += nr_fp_regs;
88 brw->curbe.clip_start = reg;
89 brw->curbe.clip_size = nr_clip_regs; reg += nr_clip_regs;
90 brw->curbe.vs_start = reg;
91 brw->curbe.vs_size = nr_vp_regs; reg += nr_vp_regs;
92 brw->curbe.total_size = reg;
93
94 if (BRW_DEBUG & DEBUG_CURBE)
95 debug_printf("curbe wm %d+%d clip %d+%d vs %d+%d\n",
96 brw->curbe.wm_start,
97 brw->curbe.wm_size,
98 brw->curbe.clip_start,
99 brw->curbe.clip_size,
100 brw->curbe.vs_start,
101 brw->curbe.vs_size );
102
103 brw->state.dirty.brw |= BRW_NEW_CURBE_OFFSETS;
104 }
105
106 return 0;
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 int 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 return 0;
146 }
147
148 static GLfloat fixed_plane[6][4] = {
149 { 0, 0, -1, 1 },
150 { 0, 0, 1, 1 },
151 { 0, -1, 0, 1 },
152 { 0, 1, 0, 1 },
153 {-1, 0, 0, 1 },
154 { 1, 0, 0, 1 }
155 };
156
157 /* Upload a new set of constants. Too much variability to go into the
158 * cache mechanism, but maybe would benefit from a comparison against
159 * the current uploaded set of constants.
160 */
161 static enum pipe_error prepare_curbe_buffer(struct brw_context *brw)
162 {
163 struct pipe_screen *screen = brw->base.screen;
164 const GLuint sz = brw->curbe.total_size;
165 const GLuint bufsz = sz * 16 * sizeof(GLfloat);
166 enum pipe_error ret;
167 GLfloat *buf;
168 GLuint i;
169
170 if (sz == 0) {
171 if (brw->curbe.last_buf) {
172 free(brw->curbe.last_buf);
173 brw->curbe.last_buf = NULL;
174 brw->curbe.last_bufsz = 0;
175 }
176 return 0;
177 }
178
179 buf = (GLfloat *) CALLOC(bufsz, 1);
180
181 /* fragment shader constants */
182 if (brw->curbe.wm_size) {
183 const struct brw_fragment_shader *fs = brw->curr.fragment_shader;
184 GLuint offset = brw->curbe.wm_start * 16;
185 GLuint nr_immediate, nr_const;
186
187 nr_immediate = fs->immediates.nr;
188 if (nr_immediate) {
189 memcpy(&buf[offset],
190 fs->immediates.data,
191 nr_immediate * 4 * sizeof(float));
192
193 offset += nr_immediate * 4;
194 }
195
196 nr_const = fs->info.file_max[TGSI_FILE_CONSTANT] + 1;
197 /* nr_const = brw->wm.prog_data->nr_params; */
198 if (nr_const) {
199 const GLfloat *value = screen->buffer_map( screen,
200 brw->curr.fragment_constants,
201 PIPE_BUFFER_USAGE_CPU_READ);
202
203 memcpy(&buf[offset], value,
204 nr_const * 4 * sizeof(float));
205
206 screen->buffer_unmap( screen,
207 brw->curr.fragment_constants );
208 }
209 }
210
211
212 /* The clipplanes are actually delivered to both CLIP and VS units.
213 * VS uses them to calculate the outcode bitmasks.
214 */
215 if (brw->curbe.clip_size) {
216 GLuint offset = brw->curbe.clip_start * 16;
217 GLuint j;
218
219 /* If any planes are going this way, send them all this way:
220 */
221 for (i = 0; i < 6; i++) {
222 buf[offset + i * 4 + 0] = fixed_plane[i][0];
223 buf[offset + i * 4 + 1] = fixed_plane[i][1];
224 buf[offset + i * 4 + 2] = fixed_plane[i][2];
225 buf[offset + i * 4 + 3] = fixed_plane[i][3];
226 }
227
228 /* Clip planes:
229 */
230 assert(brw->curr.ucp.nr <= 6);
231 for (j = 0; j < brw->curr.ucp.nr; j++) {
232 buf[offset + i * 4 + 0] = brw->curr.ucp.ucp[j][0];
233 buf[offset + i * 4 + 1] = brw->curr.ucp.ucp[j][1];
234 buf[offset + i * 4 + 2] = brw->curr.ucp.ucp[j][2];
235 buf[offset + i * 4 + 3] = brw->curr.ucp.ucp[j][3];
236 i++;
237 }
238 }
239
240 /* vertex shader constants */
241 if (brw->curbe.vs_size) {
242 GLuint offset = brw->curbe.vs_start * 16;
243 const struct brw_vertex_shader *vs = brw->curr.vertex_shader;
244 GLuint nr_immediate, nr_const;
245
246 nr_immediate = vs->immediates.nr;
247 if (nr_immediate) {
248 memcpy(&buf[offset],
249 vs->immediates.data,
250 nr_immediate * 4 * sizeof(float));
251
252 offset += nr_immediate * 4;
253 }
254
255 nr_const = vs->info.file_max[TGSI_FILE_CONSTANT] + 1;
256 if (nr_const) {
257 /* XXX: note that constant buffers are currently *already* in
258 * buffer objects. If we want to keep on putting them into the
259 * curbe, makes sense to treat constbuf's specially with malloc.
260 */
261 const GLfloat *value = screen->buffer_map( screen,
262 brw->curr.vertex_constants,
263 PIPE_BUFFER_USAGE_CPU_READ);
264
265 /* XXX: what if user's constant buffer is too small?
266 */
267 memcpy(&buf[offset], value, nr_const * 4 * sizeof(float));
268
269 screen->buffer_unmap( screen, brw->curr.vertex_constants );
270 }
271 }
272
273 if (BRW_DEBUG & DEBUG_CURBE) {
274 for (i = 0; i < sz*16; i+=4)
275 debug_printf("curbe %d.%d: %f %f %f %f\n", i/8, i&4,
276 buf[i+0], buf[i+1], buf[i+2], buf[i+3]);
277
278 debug_printf("last_buf %p buf %p sz %d/%d cmp %d\n",
279 (void *)brw->curbe.last_buf, (void *)buf,
280 bufsz, brw->curbe.last_bufsz,
281 brw->curbe.last_buf ? memcmp(buf, brw->curbe.last_buf, bufsz) : -1);
282 }
283
284 if (brw->curbe.curbe_bo != NULL &&
285 brw->curbe.last_buf &&
286 bufsz == brw->curbe.last_bufsz &&
287 memcmp(buf, brw->curbe.last_buf, bufsz) == 0) {
288 /* constants have not changed */
289 FREE(buf);
290 }
291 else {
292 /* constants have changed */
293 FREE(brw->curbe.last_buf);
294
295 brw->curbe.last_buf = buf;
296 brw->curbe.last_bufsz = bufsz;
297
298 if (brw->curbe.curbe_bo != NULL &&
299 (brw->curbe.need_new_bo ||
300 brw->curbe.curbe_next_offset + bufsz > brw->curbe.curbe_bo->size))
301 {
302 bo_reference(&brw->curbe.curbe_bo, NULL);
303 }
304
305 if (brw->curbe.curbe_bo == NULL) {
306 /* Allocate a single page for CURBE entries for this
307 * batchbuffer. They're generally around 64b. We will
308 * discard the curbe buffer after the batch is flushed to
309 * avoid synchronous updates.
310 */
311 ret = brw->sws->bo_alloc(brw->sws,
312 BRW_BUFFER_TYPE_CURBE,
313 4096, 1 << 6,
314 &brw->curbe.curbe_bo);
315 if (ret)
316 return ret;
317
318 brw->curbe.curbe_next_offset = 0;
319 }
320
321 brw->curbe.curbe_offset = brw->curbe.curbe_next_offset;
322 brw->curbe.curbe_next_offset += bufsz;
323 brw->curbe.curbe_next_offset = align(brw->curbe.curbe_next_offset, 64);
324
325 /* Copy data to the buffer:
326 */
327 brw->sws->bo_subdata(brw->curbe.curbe_bo,
328 BRW_DATA_CONSTANT_BUFFER,
329 brw->curbe.curbe_offset,
330 bufsz,
331 buf,
332 NULL, 0);
333 }
334
335 brw_add_validated_bo(brw, brw->curbe.curbe_bo);
336
337 /* Because this provokes an action (ie copy the constants into the
338 * URB), it shouldn't be shortcircuited if identical to the
339 * previous time - because eg. the urb destination may have
340 * changed, or the urb contents different to last time.
341 *
342 * Note that the data referred to is actually copied internally,
343 * not just used in place according to passed pointer.
344 *
345 * It appears that the CS unit takes care of using each available
346 * URB entry (Const URB Entry == CURBE) in turn, and issuing
347 * flushes as necessary when doublebuffering of CURBEs isn't
348 * possible.
349 */
350
351 return 0;
352 }
353
354 static enum pipe_error emit_curbe_buffer(struct brw_context *brw)
355 {
356 GLuint sz = brw->curbe.total_size;
357
358 BEGIN_BATCH(2, IGNORE_CLIPRECTS);
359 if (sz == 0) {
360 OUT_BATCH((CMD_CONST_BUFFER << 16) | (2 - 2));
361 OUT_BATCH(0);
362 } else {
363 OUT_BATCH((CMD_CONST_BUFFER << 16) | (1 << 8) | (2 - 2));
364 OUT_RELOC(brw->curbe.curbe_bo,
365 BRW_USAGE_STATE,
366 (sz - 1) + brw->curbe.curbe_offset);
367 }
368 ADVANCE_BATCH();
369 return 0;
370 }
371
372 const struct brw_tracked_state brw_curbe_buffer = {
373 .dirty = {
374 .mesa = (PIPE_NEW_FRAGMENT_CONSTANTS |
375 PIPE_NEW_VERTEX_CONSTANTS |
376 PIPE_NEW_CLIP),
377 .brw = (BRW_NEW_FRAGMENT_PROGRAM |
378 BRW_NEW_VERTEX_PROGRAM |
379 BRW_NEW_URB_FENCE | /* Implicit - hardware requires this, not used above */
380 BRW_NEW_PSP | /* Implicit - hardware requires this, not used above */
381 BRW_NEW_CURBE_OFFSETS |
382 BRW_NEW_BATCH),
383 .cache = (CACHE_NEW_WM_PROG)
384 },
385 .prepare = prepare_curbe_buffer,
386 .emit = emit_curbe_buffer,
387 };
388