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"
40 #include "brw_debug.h"
41 #include "brw_screen.h"
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...
51 static void calculate_curbe_offsets( struct brw_context
*brw
)
53 /* CACHE_NEW_WM_PROG */
54 const GLuint nr_fp_regs
= (brw
->wm
.prog_data
->nr_params
+ 15) / 16;
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;
62 if (brw
->curr
.ucp
.nr
) {
63 GLuint nr_planes
= 6 + brw
->curr
.ucp
.nr
;
64 nr_clip_regs
= (nr_planes
* 4 + 15) / 16;
68 total_regs
= nr_fp_regs
+ nr_vp_regs
+ nr_clip_regs
;
70 /* When this is > 32, want to use a true constant buffer to hold
71 * the extra constants.
73 assert(total_regs
<= 32);
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)) {
85 /* Calculate a new layout:
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
;
96 if (BRW_DEBUG
& DEBUG_CURBE
)
97 debug_printf("curbe wm %d+%d clip %d+%d vs %d+%d\n",
100 brw
->curbe
.clip_start
,
101 brw
->curbe
.clip_size
,
103 brw
->curbe
.vs_size
);
105 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 void 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
);
147 static GLfloat fixed_plane
[6][4] = {
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.
160 static void prepare_constant_buffer(struct brw_context
*brw
)
162 const GLuint sz
= brw
->curbe
.total_size
;
163 const GLuint bufsz
= sz
* 16 * sizeof(GLfloat
);
168 if (brw
->curbe
.last_buf
) {
169 free(brw
->curbe
.last_buf
);
170 brw
->curbe
.last_buf
= NULL
;
171 brw
->curbe
.last_bufsz
= 0;
176 buf
= (GLfloat
*) CALLOC(bufsz
, 1);
178 /* fragment shader constants */
179 if (brw
->curbe
.wm_size
) {
180 GLuint offset
= brw
->curbe
.wm_start
* 16;
182 /* map fs constant buffer */
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
];
188 /* unmap fs constant buffer */
192 /* The clipplanes are actually delivered to both CLIP and VS units.
193 * VS uses them to calculate the outcode bitmasks.
195 if (brw
->curbe
.clip_size
) {
196 GLuint offset
= brw
->curbe
.clip_start
* 16;
199 /* If any planes are going this way, send them all this way:
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];
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];
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
;
226 const GLfloat
*value
= screen
->buffer_map( screen
,
227 brw
->curr
.vertex_constants
,
228 PIPE_BUFFER_USAGE_CPU_READ
);
230 /* XXX: what if user's constant buffer is too small?
232 memcpy(&buf
[offset
], value
, nr
* 4 * sizeof(float));
234 screen
->buffer_unmap( screen
, brw
->curr
.vertex_constants
);
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]);
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);
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 */
256 /* constants have changed */
257 if (brw
->curbe
.last_buf
)
258 FREE(brw
->curbe
.last_buf
);
260 brw
->curbe
.last_buf
= buf
;
261 brw
->curbe
.last_bufsz
= bufsz
;
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
))
267 brw
->sws
->bo_unreference(brw
->curbe
.curbe_bo
);
268 brw
->curbe
.curbe_bo
= NULL
;
271 if (brw
->curbe
.curbe_bo
== NULL
) {
272 /* Allocate a single page for CURBE entries for this batchbuffer.
273 * They're generally around 64b.
275 brw
->curbe
.curbe_bo
= brw
->sws
->bo_alloc(brw
->sws
,
276 BRW_BUFFER_TYPE_CURBE
,
278 brw
->curbe
.curbe_next_offset
= 0;
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);
285 /* Copy data to the buffer:
287 brw
->sws
->bo_subdata(brw
->curbe
.curbe_bo
,
288 brw
->curbe
.curbe_offset
,
293 brw_add_validated_bo(brw
, brw
->curbe
.curbe_bo
);
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.
300 * Note that the data referred to is actually copied internally,
301 * not just used in place according to passed pointer.
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
310 static void emit_constant_buffer(struct brw_context
*brw
)
312 GLuint sz
= brw
->curbe
.total_size
;
314 BEGIN_BATCH(2, IGNORE_CLIPRECTS
);
316 OUT_BATCH((CMD_CONST_BUFFER
<< 16) | (2 - 2));
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
);
327 const struct brw_tracked_state brw_constant_buffer
= {
329 .mesa
= (PIPE_NEW_FRAGMENT_CONSTANTS
|
330 PIPE_NEW_VERTEX_CONSTANTS
|
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
|
338 .cache
= (CACHE_NEW_WM_PROG
)
340 .prepare
= prepare_constant_buffer
,
341 .emit
= emit_constant_buffer
,