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 struct pipe_screen
*screen
= brw
->base
.screen
;
164 const GLuint sz
= brw
->curbe
.total_size
;
165 const GLuint bufsz
= sz
* 16 * sizeof(GLfloat
);
171 if (brw
->curbe
.last_buf
) {
172 free(brw
->curbe
.last_buf
);
173 brw
->curbe
.last_buf
= NULL
;
174 brw
->curbe
.last_bufsz
= 0;
179 buf
= (GLfloat
*) CALLOC(bufsz
, 1);
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
;
187 nr_immediate
= fs
->immediates
.nr
;
191 nr_immediate
* 4 * sizeof(float));
193 offset
+= nr_immediate
* 4;
196 nr_const
= fs
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
197 /* nr_const = brw->wm.prog_data->nr_params; */
199 const GLfloat
*value
= screen
->buffer_map( screen
,
200 brw
->curr
.fragment_constants
,
201 PIPE_BUFFER_USAGE_CPU_READ
);
203 memcpy(&buf
[offset
], value
,
204 nr_const
* 4 * sizeof(float));
206 screen
->buffer_unmap( screen
,
207 brw
->curr
.fragment_constants
);
212 /* The clipplanes are actually delivered to both CLIP and VS units.
213 * VS uses them to calculate the outcode bitmasks.
215 if (brw
->curbe
.clip_size
) {
216 GLuint offset
= brw
->curbe
.clip_start
* 16;
219 /* If any planes are going this way, send them all this way:
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];
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];
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
;
246 nr_immediate
= vs
->immediates
.nr
;
250 nr_immediate
* 4 * sizeof(float));
252 offset
+= nr_immediate
* 4;
255 nr_const
= vs
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
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.
261 const GLfloat
*value
= screen
->buffer_map( screen
,
262 brw
->curr
.vertex_constants
,
263 PIPE_BUFFER_USAGE_CPU_READ
);
265 /* XXX: what if user's constant buffer is too small?
267 memcpy(&buf
[offset
], value
, nr_const
* 4 * sizeof(float));
269 screen
->buffer_unmap( screen
, brw
->curr
.vertex_constants
);
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]);
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);
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 */
292 /* constants have changed */
293 FREE(brw
->curbe
.last_buf
);
295 brw
->curbe
.last_buf
= buf
;
296 brw
->curbe
.last_bufsz
= bufsz
;
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
))
302 bo_reference(&brw
->curbe
.curbe_bo
, NULL
);
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.
311 ret
= brw
->sws
->bo_alloc(brw
->sws
,
312 BRW_BUFFER_TYPE_CURBE
,
314 &brw
->curbe
.curbe_bo
);
318 brw
->curbe
.curbe_next_offset
= 0;
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);
325 /* Copy data to the buffer:
327 brw
->sws
->bo_subdata(brw
->curbe
.curbe_bo
,
328 BRW_DATA_CONSTANT_BUFFER
,
329 brw
->curbe
.curbe_offset
,
335 brw_add_validated_bo(brw
, brw
->curbe
.curbe_bo
);
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.
342 * Note that the data referred to is actually copied internally,
343 * not just used in place according to passed pointer.
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
354 static enum pipe_error
emit_curbe_buffer(struct brw_context
*brw
)
356 GLuint sz
= brw
->curbe
.total_size
;
358 BEGIN_BATCH(2, IGNORE_CLIPRECTS
);
360 OUT_BATCH((CMD_CONST_BUFFER
<< 16) | (2 - 2));
363 OUT_BATCH((CMD_CONST_BUFFER
<< 16) | (1 << 8) | (2 - 2));
364 OUT_RELOC(brw
->curbe
.curbe_bo
,
366 (sz
- 1) + brw
->curbe
.curbe_offset
);
372 const struct brw_tracked_state brw_curbe_buffer
= {
374 .mesa
= (PIPE_NEW_FRAGMENT_CONSTANTS
|
375 PIPE_NEW_VERTEX_CONSTANTS
|
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
|
383 .cache
= (CACHE_NEW_WM_PROG
)
385 .prepare
= prepare_curbe_buffer
,
386 .emit
= emit_curbe_buffer
,