2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
32 #include "util/u_memory.h"
33 #include "util/u_math.h"
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"
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...
49 static int calculate_curbe_offsets( struct brw_context
*brw
)
51 /* CACHE_NEW_WM_PROG */
52 const GLuint nr_fp_regs
= brw
->wm
.prog_data
->curb_read_length
;
54 /* BRW_NEW_VERTEX_PROGRAM */
55 const GLuint nr_vp_regs
= brw
->vs
.prog_data
->curb_read_length
;
56 GLuint nr_clip_regs
= 0;
60 if (brw
->curr
.ucp
.nr
) {
61 GLuint nr_planes
= 6 + brw
->curr
.ucp
.nr
;
62 nr_clip_regs
= (nr_planes
* 4 + 15) / 16;
66 total_regs
= nr_fp_regs
+ nr_vp_regs
+ nr_clip_regs
;
68 /* When this is > 32, want to use a true constant buffer to hold
69 * the extra constants.
71 assert(total_regs
<= 32);
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)) {
83 /* Calculate a new layout:
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
;
94 if (BRW_DEBUG
& DEBUG_CURBE
)
95 debug_printf("curbe wm %d+%d clip %d+%d vs %d+%d\n",
98 brw
->curbe
.clip_start
,
101 brw
->curbe
.vs_size
);
103 brw
->state
.dirty
.brw
|= BRW_NEW_CURBE_OFFSETS
;
110 const struct brw_tracked_state brw_curbe_offsets
= {
112 .mesa
= PIPE_NEW_CLIP
,
113 .brw
= BRW_NEW_VERTEX_PROGRAM
,
114 .cache
= CACHE_NEW_WM_PROG
116 .prepare
= calculate_curbe_offsets
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.
127 int brw_upload_cs_urb_state(struct brw_context
*brw
)
129 struct brw_cs_urb_state cs_urb
;
130 memset(&cs_urb
, 0, sizeof(cs_urb
));
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
136 cs_urb
.header
.opcode
= CMD_CS_URB_STATE
;
137 cs_urb
.header
.length
= sizeof(cs_urb
)/4 - 2;
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;
143 assert(brw
->urb
.nr_cs_entries
);
144 BRW_CACHED_BATCH_STRUCT(brw
, &cs_urb
);
148 static GLfloat fixed_plane
[6][4] = {
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.
161 static enum pipe_error
prepare_curbe_buffer(struct brw_context
*brw
)
163 const GLuint sz
= brw
->curbe
.total_size
;
164 const GLuint bufsz
= sz
* 16 * sizeof(GLfloat
);
170 if (brw
->curbe
.last_buf
) {
171 FREE(brw
->curbe
.last_buf
);
172 brw
->curbe
.last_buf
= NULL
;
173 brw
->curbe
.last_bufsz
= 0;
178 buf
= (GLfloat
*) CALLOC(bufsz
, 1);
180 /* fragment shader constants */
181 if (brw
->curbe
.wm_size
) {
182 const struct brw_fragment_shader
*fs
= brw
->curr
.fragment_shader
;
183 GLuint offset
= brw
->curbe
.wm_start
* 16;
184 GLuint nr_immediate
, nr_const
;
186 nr_immediate
= fs
->immediates
.nr
;
190 nr_immediate
* 4 * sizeof(float));
192 offset
+= nr_immediate
* 4;
195 nr_const
= fs
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
196 /* nr_const = brw->wm.prog_data->nr_params; */
198 pipe_buffer_read( &brw
->base
,
199 brw
->curr
.fragment_constants
,
201 nr_const
* 4 * sizeof(float),
207 /* The clipplanes are actually delivered to both CLIP and VS units.
208 * VS uses them to calculate the outcode bitmasks.
210 if (brw
->curbe
.clip_size
) {
211 GLuint offset
= brw
->curbe
.clip_start
* 16;
214 /* If any planes are going this way, send them all this way:
216 for (i
= 0; i
< 6; i
++) {
217 buf
[offset
+ i
* 4 + 0] = fixed_plane
[i
][0];
218 buf
[offset
+ i
* 4 + 1] = fixed_plane
[i
][1];
219 buf
[offset
+ i
* 4 + 2] = fixed_plane
[i
][2];
220 buf
[offset
+ i
* 4 + 3] = fixed_plane
[i
][3];
225 assert(brw
->curr
.ucp
.nr
<= 6);
226 for (j
= 0; j
< brw
->curr
.ucp
.nr
; j
++) {
227 buf
[offset
+ i
* 4 + 0] = brw
->curr
.ucp
.ucp
[j
][0];
228 buf
[offset
+ i
* 4 + 1] = brw
->curr
.ucp
.ucp
[j
][1];
229 buf
[offset
+ i
* 4 + 2] = brw
->curr
.ucp
.ucp
[j
][2];
230 buf
[offset
+ i
* 4 + 3] = brw
->curr
.ucp
.ucp
[j
][3];
235 /* vertex shader constants */
236 if (brw
->curbe
.vs_size
) {
237 GLuint offset
= brw
->curbe
.vs_start
* 16;
238 const struct brw_vertex_shader
*vs
= brw
->curr
.vertex_shader
;
239 GLuint nr_immediate
, nr_const
;
241 nr_immediate
= vs
->immediates
.nr
;
245 nr_immediate
* 4 * sizeof(float));
247 offset
+= nr_immediate
* 4;
250 nr_const
= vs
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
252 /* XXX: note that constant buffers are currently *already* in
253 * buffer objects. If we want to keep on putting them into the
254 * curbe, makes sense to treat constbuf's specially with malloc.
257 /* XXX: what if user's constant buffer is too small?
259 pipe_buffer_read(&brw
->base
,
260 brw
->curr
.vertex_constants
,
262 nr_const
* 4 * sizeof(float),
267 if (BRW_DEBUG
& DEBUG_CURBE
) {
268 for (i
= 0; i
< sz
*16; i
+=4)
269 debug_printf("curbe %d.%d: %f %f %f %f\n", i
/8, i
&4,
270 buf
[i
+0], buf
[i
+1], buf
[i
+2], buf
[i
+3]);
272 debug_printf("last_buf %p buf %p sz %d/%d cmp %d\n",
273 (void *)brw
->curbe
.last_buf
, (void *)buf
,
274 bufsz
, brw
->curbe
.last_bufsz
,
275 brw
->curbe
.last_buf
? memcmp(buf
, brw
->curbe
.last_buf
, bufsz
) : -1);
278 if (brw
->curbe
.curbe_bo
!= NULL
&&
279 brw
->curbe
.last_buf
&&
280 bufsz
== brw
->curbe
.last_bufsz
&&
281 memcmp(buf
, brw
->curbe
.last_buf
, bufsz
) == 0) {
282 /* constants have not changed */
286 /* constants have changed */
287 FREE(brw
->curbe
.last_buf
);
289 brw
->curbe
.last_buf
= buf
;
290 brw
->curbe
.last_bufsz
= bufsz
;
292 if (brw
->curbe
.curbe_bo
!= NULL
&&
293 (brw
->curbe
.need_new_bo
||
294 brw
->curbe
.curbe_next_offset
+ bufsz
> brw
->curbe
.curbe_bo
->size
))
296 bo_reference(&brw
->curbe
.curbe_bo
, NULL
);
299 if (brw
->curbe
.curbe_bo
== NULL
) {
300 /* Allocate a single page for CURBE entries for this
301 * batchbuffer. They're generally around 64b. We will
302 * discard the curbe buffer after the batch is flushed to
303 * avoid synchronous updates.
305 ret
= brw
->sws
->bo_alloc(brw
->sws
,
306 BRW_BUFFER_TYPE_CURBE
,
308 &brw
->curbe
.curbe_bo
);
312 brw
->curbe
.curbe_next_offset
= 0;
315 brw
->curbe
.curbe_offset
= brw
->curbe
.curbe_next_offset
;
316 brw
->curbe
.curbe_next_offset
+= bufsz
;
317 brw
->curbe
.curbe_next_offset
= align(brw
->curbe
.curbe_next_offset
, 64);
319 /* Copy data to the buffer:
321 brw
->sws
->bo_subdata(brw
->curbe
.curbe_bo
,
322 BRW_DATA_CONSTANT_BUFFER
,
323 brw
->curbe
.curbe_offset
,
329 brw_add_validated_bo(brw
, brw
->curbe
.curbe_bo
);
331 /* Because this provokes an action (ie copy the constants into the
332 * URB), it shouldn't be shortcircuited if identical to the
333 * previous time - because eg. the urb destination may have
334 * changed, or the urb contents different to last time.
336 * Note that the data referred to is actually copied internally,
337 * not just used in place according to passed pointer.
339 * It appears that the CS unit takes care of using each available
340 * URB entry (Const URB Entry == CURBE) in turn, and issuing
341 * flushes as necessary when doublebuffering of CURBEs isn't
348 static enum pipe_error
emit_curbe_buffer(struct brw_context
*brw
)
350 GLuint sz
= brw
->curbe
.total_size
;
352 BEGIN_BATCH(2, IGNORE_CLIPRECTS
);
354 OUT_BATCH((CMD_CONST_BUFFER
<< 16) | (2 - 2));
357 OUT_BATCH((CMD_CONST_BUFFER
<< 16) | (1 << 8) | (2 - 2));
358 OUT_RELOC(brw
->curbe
.curbe_bo
,
360 (sz
- 1) + brw
->curbe
.curbe_offset
);
366 const struct brw_tracked_state brw_curbe_buffer
= {
368 .mesa
= (PIPE_NEW_FRAGMENT_CONSTANTS
|
369 PIPE_NEW_VERTEX_CONSTANTS
|
371 .brw
= (BRW_NEW_FRAGMENT_PROGRAM
|
372 BRW_NEW_VERTEX_PROGRAM
|
373 BRW_NEW_URB_FENCE
| /* Implicit - hardware requires this, not used above */
374 BRW_NEW_PSP
| /* Implicit - hardware requires this, not used above */
375 BRW_NEW_CURBE_OFFSETS
|
377 .cache
= (CACHE_NEW_WM_PROG
)
379 .prepare
= prepare_curbe_buffer
,
380 .emit
= emit_curbe_buffer
,