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>
34 #include "main/glheader.h"
35 #include "main/context.h"
36 #include "main/macros.h"
37 #include "main/enums.h"
38 #include "program/prog_parameter.h"
39 #include "program/prog_print.h"
40 #include "program/prog_statevars.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_regions.h"
43 #include "brw_context.h"
44 #include "brw_defines.h"
45 #include "brw_state.h"
50 * Partition the CURBE between the various users of constant values:
51 * Note that vertex and fragment shaders can now fetch constants out
52 * of constant buffers. We no longer allocatea block of the GRF for
53 * constants. That greatly reduces the demand for space in the CURBE.
54 * Some of the comments within are dated...
56 static void calculate_curbe_offsets( struct brw_context
*brw
)
58 struct gl_context
*ctx
= &brw
->intel
.ctx
;
59 /* CACHE_NEW_WM_PROG */
60 const GLuint nr_fp_regs
= (brw
->wm
.prog_data
->nr_params
+ 15) / 16;
62 /* BRW_NEW_VERTEX_PROGRAM */
63 const GLuint nr_vp_regs
= (brw
->vs
.prog_data
->nr_params
+ 15) / 16;
64 GLuint nr_clip_regs
= 0;
68 if (ctx
->Transform
.ClipPlanesEnabled
) {
69 GLuint nr_planes
= 6 + _mesa_bitcount_64(ctx
->Transform
.ClipPlanesEnabled
);
70 nr_clip_regs
= (nr_planes
* 4 + 15) / 16;
74 total_regs
= nr_fp_regs
+ nr_vp_regs
+ nr_clip_regs
;
76 /* This can happen - what to do? Probably rather than falling
77 * back, the best thing to do is emit programs which code the
78 * constants as immediate values. Could do this either as a static
79 * cap on WM and VS, or adaptively.
81 * Unfortunately, this is currently dependent on the results of the
82 * program generation process (in the case of wm), so this would
83 * introduce the need to re-generate programs in the event of a
84 * curbe allocation failure.
86 /* Max size is 32 - just large enough to
87 * hold the 128 parameters allowed by
88 * the fragment and vertex program
89 * api's. It's not clear what happens
90 * when both VP and FP want to use 128
93 assert(total_regs
<= 32);
97 if (nr_fp_regs
> brw
->curbe
.wm_size
||
98 nr_vp_regs
> brw
->curbe
.vs_size
||
99 nr_clip_regs
!= brw
->curbe
.clip_size
||
100 (total_regs
< brw
->curbe
.total_size
/ 4 &&
101 brw
->curbe
.total_size
> 16)) {
105 /* Calculate a new layout:
108 brw
->curbe
.wm_start
= reg
;
109 brw
->curbe
.wm_size
= nr_fp_regs
; reg
+= nr_fp_regs
;
110 brw
->curbe
.clip_start
= reg
;
111 brw
->curbe
.clip_size
= nr_clip_regs
; reg
+= nr_clip_regs
;
112 brw
->curbe
.vs_start
= reg
;
113 brw
->curbe
.vs_size
= nr_vp_regs
; reg
+= nr_vp_regs
;
114 brw
->curbe
.total_size
= reg
;
117 printf("curbe wm %d+%d clip %d+%d vs %d+%d\n",
120 brw
->curbe
.clip_start
,
121 brw
->curbe
.clip_size
,
123 brw
->curbe
.vs_size
);
125 brw
->state
.dirty
.brw
|= BRW_NEW_CURBE_OFFSETS
;
130 const struct brw_tracked_state brw_curbe_offsets
= {
132 .mesa
= _NEW_TRANSFORM
,
133 .brw
= BRW_NEW_VERTEX_PROGRAM
| BRW_NEW_CONTEXT
,
134 .cache
= CACHE_NEW_WM_PROG
136 .emit
= calculate_curbe_offsets
142 /* Define the number of curbes within CS's urb allocation. Multiple
143 * urb entries -> multiple curbes. These will be used by
144 * fixed-function hardware in a double-buffering scheme to avoid a
145 * pipeline stall each time the contents of the curbe is changed.
147 void brw_upload_cs_urb_state(struct brw_context
*brw
)
149 struct intel_context
*intel
= &brw
->intel
;
152 /* It appears that this is the state packet for the CS unit, ie. the
153 * urb entries detailed here are housed in the CS range from the
156 OUT_BATCH(CMD_CS_URB_STATE
<< 16 | (2-2));
158 /* BRW_NEW_URB_FENCE */
159 if (brw
->urb
.csize
== 0) {
162 /* BRW_NEW_URB_FENCE */
163 assert(brw
->urb
.nr_cs_entries
);
164 OUT_BATCH((brw
->urb
.csize
- 1) << 4 | brw
->urb
.nr_cs_entries
);
169 static GLfloat fixed_plane
[6][4] = {
178 /* Upload a new set of constants. Too much variability to go into the
179 * cache mechanism, but maybe would benefit from a comparison against
180 * the current uploaded set of constants.
183 brw_upload_constant_buffer(struct brw_context
*brw
)
185 struct intel_context
*intel
= &brw
->intel
;
186 struct gl_context
*ctx
= &intel
->ctx
;
187 const struct brw_vertex_program
*vp
=
188 brw_vertex_program_const(brw
->vertex_program
);
189 const GLuint sz
= brw
->curbe
.total_size
;
190 const GLuint bufsz
= sz
* 16 * sizeof(GLfloat
);
193 gl_clip_plane
*clip_planes
;
196 brw
->curbe
.last_bufsz
= 0;
200 buf
= brw
->curbe
.next_buf
;
202 /* fragment shader constants */
203 if (brw
->curbe
.wm_size
) {
204 GLuint offset
= brw
->curbe
.wm_start
* 16;
206 /* copy float constants */
207 for (i
= 0; i
< brw
->wm
.prog_data
->nr_params
; i
++) {
208 buf
[offset
+ i
] = convert_param(brw
->wm
.prog_data
->param_convert
[i
],
209 brw
->wm
.prog_data
->param
[i
]);
214 /* When using the old VS backend, the clipplanes are actually delivered to
215 * both CLIP and VS units. VS uses them to calculate the outcode bitmasks.
217 * When using the new VS backend, it is responsible for setting up its own
218 * clipplane constants if it needs them. This results in a slight waste of
219 * of curbe space, but the advantage is that the new VS backend can use its
220 * general-purpose uniform layout code to store the clipplanes.
222 if (brw
->curbe
.clip_size
) {
223 GLuint offset
= brw
->curbe
.clip_start
* 16;
226 /* If any planes are going this way, send them all this way:
228 for (i
= 0; i
< 6; i
++) {
229 buf
[offset
+ i
* 4 + 0] = fixed_plane
[i
][0];
230 buf
[offset
+ i
* 4 + 1] = fixed_plane
[i
][1];
231 buf
[offset
+ i
* 4 + 2] = fixed_plane
[i
][2];
232 buf
[offset
+ i
* 4 + 3] = fixed_plane
[i
][3];
235 /* Clip planes: _NEW_TRANSFORM plus _NEW_PROJECTION to get to
238 clip_planes
= brw_select_clip_planes(ctx
);
239 for (j
= 0; j
< MAX_CLIP_PLANES
; j
++) {
240 if (ctx
->Transform
.ClipPlanesEnabled
& (1<<j
)) {
241 buf
[offset
+ i
* 4 + 0] = clip_planes
[j
][0];
242 buf
[offset
+ i
* 4 + 1] = clip_planes
[j
][1];
243 buf
[offset
+ i
* 4 + 2] = clip_planes
[j
][2];
244 buf
[offset
+ i
* 4 + 3] = clip_planes
[j
][3];
250 /* vertex shader constants */
251 if (brw
->curbe
.vs_size
) {
252 GLuint offset
= brw
->curbe
.vs_start
* 16;
253 GLuint nr
= brw
->vs
.prog_data
->nr_params
/ 4;
255 if (brw
->vs
.prog_data
->uses_new_param_layout
) {
256 for (i
= 0; i
< brw
->vs
.prog_data
->nr_params
; i
++) {
257 buf
[offset
+ i
] = *brw
->vs
.prog_data
->param
[i
];
260 /* Load the subset of push constants that will get used when
261 * we also have a pull constant buffer.
263 for (i
= 0; i
< vp
->program
.Base
.Parameters
->NumParameters
; i
++) {
264 if (brw
->vs
.constant_map
[i
] != -1) {
265 assert(brw
->vs
.constant_map
[i
] <= nr
);
266 memcpy(buf
+ offset
+ brw
->vs
.constant_map
[i
] * 4,
267 vp
->program
.Base
.Parameters
->ParameterValues
[i
],
275 for (i
= 0; i
< sz
*16; i
+=4)
276 printf("curbe %d.%d: %f %f %f %f\n", i
/8, i
&4,
277 buf
[i
+0], buf
[i
+1], buf
[i
+2], buf
[i
+3]);
279 printf("last_buf %p buf %p sz %d/%d cmp %d\n",
280 brw
->curbe
.last_buf
, buf
,
281 bufsz
, brw
->curbe
.last_bufsz
,
282 brw
->curbe
.last_buf
? memcmp(buf
, brw
->curbe
.last_buf
, bufsz
) : -1);
285 if (brw
->curbe
.curbe_bo
!= NULL
&&
286 bufsz
== brw
->curbe
.last_bufsz
&&
287 memcmp(buf
, brw
->curbe
.last_buf
, bufsz
) == 0) {
288 /* constants have not changed */
290 /* Update the record of what our last set of constants was. We
291 * don't just flip the pointers because we don't fill in the
292 * data in the padding between the entries.
294 memcpy(brw
->curbe
.last_buf
, buf
, bufsz
);
295 brw
->curbe
.last_bufsz
= bufsz
;
297 if (brw
->curbe
.curbe_bo
!= NULL
&&
298 brw
->curbe
.curbe_next_offset
+ bufsz
> brw
->curbe
.curbe_bo
->size
)
300 drm_intel_gem_bo_unmap_gtt(brw
->curbe
.curbe_bo
);
301 drm_intel_bo_unreference(brw
->curbe
.curbe_bo
);
302 brw
->curbe
.curbe_bo
= NULL
;
305 if (brw
->curbe
.curbe_bo
== NULL
) {
306 /* Allocate a single page for CURBE entries for this batchbuffer.
307 * They're generally around 64b.
309 brw
->curbe
.curbe_bo
= drm_intel_bo_alloc(brw
->intel
.bufmgr
, "CURBE",
311 brw
->curbe
.curbe_next_offset
= 0;
312 drm_intel_gem_bo_map_gtt(brw
->curbe
.curbe_bo
);
313 assert(bufsz
< 4096);
316 brw
->curbe
.curbe_offset
= brw
->curbe
.curbe_next_offset
;
317 brw
->curbe
.curbe_next_offset
+= bufsz
;
318 brw
->curbe
.curbe_next_offset
= ALIGN(brw
->curbe
.curbe_next_offset
, 64);
320 /* Copy data to the buffer:
322 memcpy(brw
->curbe
.curbe_bo
->virtual + brw
->curbe
.curbe_offset
,
327 /* Because this provokes an action (ie copy the constants into the
328 * URB), it shouldn't be shortcircuited if identical to the
329 * previous time - because eg. the urb destination may have
330 * changed, or the urb contents different to last time.
332 * Note that the data referred to is actually copied internally,
333 * not just used in place according to passed pointer.
335 * It appears that the CS unit takes care of using each available
336 * URB entry (Const URB Entry == CURBE) in turn, and issuing
337 * flushes as necessary when doublebuffering of CURBEs isn't
343 if (brw
->curbe
.total_size
== 0) {
344 OUT_BATCH((CMD_CONST_BUFFER
<< 16) | (2 - 2));
347 OUT_BATCH((CMD_CONST_BUFFER
<< 16) | (1 << 8) | (2 - 2));
348 OUT_RELOC(brw
->curbe
.curbe_bo
,
349 I915_GEM_DOMAIN_INSTRUCTION
, 0,
350 (brw
->curbe
.total_size
- 1) + brw
->curbe
.curbe_offset
);
355 /* This tracked state is unique in that the state it monitors varies
356 * dynamically depending on the parameters tracked by the fragment and
357 * vertex programs. This is the template used as a starting point,
358 * each context will maintain a copy of this internally and update as
361 const struct brw_tracked_state brw_constant_buffer
= {
363 .mesa
= _NEW_PROGRAM_CONSTANTS
,
364 .brw
= (BRW_NEW_FRAGMENT_PROGRAM
|
365 BRW_NEW_VERTEX_PROGRAM
|
366 BRW_NEW_URB_FENCE
| /* Implicit - hardware requires this, not used above */
367 BRW_NEW_PSP
| /* Implicit - hardware requires this, not used above */
368 BRW_NEW_CURBE_OFFSETS
|
370 .cache
= (CACHE_NEW_WM_PROG
)
372 .emit
= brw_upload_constant_buffer
,