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 "brw_context.h"
35 #include "brw_defines.h"
36 #include "brw_state.h"
37 #include "brw_batch.h"
40 #include "pipe/p_state.h"
41 #include "pipe/p_winsys.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
45 #define FILE_DEBUG_FLAG DEBUG_FALLBACKS
47 /* Partition the CURBE between the various users of constant values:
49 static void calculate_curbe_offsets( struct brw_context
*brw
)
51 /* CACHE_NEW_WM_PROG */
52 unsigned nr_fp_regs
= align(brw
->wm
.prog_data
->max_const
, 16);
54 /* BRW_NEW_VERTEX_PROGRAM */
55 unsigned nr_vp_regs
= align(brw
->vs
.prog_data
->max_const
, 16);
56 unsigned nr_clip_regs
= 0;
61 if (brw
->attribs
.Transform
->ClipPlanesEnabled
) {
62 unsigned nr_planes
= 6 + brw_count_bits(brw
->attribs
.Transform
->ClipPlanesEnabled
);
63 nr_clip_regs
= align(nr_planes
* 4, 16);
68 total_regs
= nr_fp_regs
+ nr_vp_regs
+ nr_clip_regs
;
70 /* This can happen - what to do? Probably rather than falling
71 * back, the best thing to do is emit programs which code the
72 * constants as immediate values. Could do this either as a static
73 * cap on WM and VS, or adaptively.
75 * Unfortunately, this is currently dependent on the results of the
76 * program generation process (in the case of wm), so this would
77 * introduce the need to re-generate programs in the event of a
78 * curbe allocation failure.
80 /* Max size is 32 - just large enough to
81 * hold the 128 parameters allowed by
82 * the fragment and vertex program
83 * api's. It's not clear what happens
84 * when both VP and FP want to use 128
87 assert(total_regs
<= 32);
91 if (nr_fp_regs
> brw
->curbe
.wm_size
||
92 nr_vp_regs
> brw
->curbe
.vs_size
||
93 nr_clip_regs
!= brw
->curbe
.clip_size
||
94 (total_regs
< brw
->curbe
.total_size
/ 4 &&
95 brw
->curbe
.total_size
> 16)) {
99 /* Calculate a new layout:
102 brw
->curbe
.wm_start
= reg
;
103 brw
->curbe
.wm_size
= nr_fp_regs
; reg
+= nr_fp_regs
;
104 brw
->curbe
.clip_start
= reg
;
105 brw
->curbe
.clip_size
= nr_clip_regs
; reg
+= nr_clip_regs
;
106 brw
->curbe
.vs_start
= reg
;
107 brw
->curbe
.vs_size
= nr_vp_regs
; reg
+= nr_vp_regs
;
108 brw
->curbe
.total_size
= reg
;
112 DBG("curbe wm %d+%d clip %d+%d vs %d+%d\n",
115 brw
->curbe
.clip_start
,
116 brw
->curbe
.clip_size
,
118 brw
->curbe
.vs_size
);
121 brw
->state
.dirty
.brw
|= BRW_NEW_CURBE_OFFSETS
;
126 const struct brw_tracked_state brw_curbe_offsets
= {
128 .brw
= (BRW_NEW_CLIP
|
130 .cache
= CACHE_NEW_WM_PROG
132 .update
= calculate_curbe_offsets
137 /* Define the number of curbes within CS's urb allocation. Multiple
138 * urb entries -> multiple curbes. These will be used by
139 * fixed-function hardware in a double-buffering scheme to avoid a
140 * pipeline stall each time the contents of the curbe is changed.
142 void brw_upload_constant_buffer_state(struct brw_context
*brw
)
144 struct brw_constant_buffer_state cbs
;
145 memset(&cbs
, 0, sizeof(cbs
));
147 /* It appears that this is the state packet for the CS unit, ie. the
148 * urb entries detailed here are housed in the CS range from the
151 cbs
.header
.opcode
= CMD_CONST_BUFFER_STATE
;
152 cbs
.header
.length
= sizeof(cbs
)/4 - 2;
154 /* BRW_NEW_URB_FENCE */
155 cbs
.bits0
.nr_urb_entries
= brw
->urb
.nr_cs_entries
;
156 cbs
.bits0
.urb_entry_size
= brw
->urb
.csize
- 1;
158 assert(brw
->urb
.nr_cs_entries
);
159 BRW_CACHED_BATCH_STRUCT(brw
, &cbs
);
163 static float fixed_plane
[6][4] = {
172 /* Upload a new set of constants. Too much variability to go into the
173 * cache mechanism, but maybe would benefit from a comparison against
174 * the current uploaded set of constants.
176 static void upload_constant_buffer(struct brw_context
*brw
)
178 struct brw_mem_pool
*pool
= &brw
->pool
[BRW_GS_POOL
];
179 unsigned sz
= brw
->curbe
.total_size
;
180 unsigned bufsz
= sz
* sizeof(float);
186 struct brw_constant_buffer cb
;
187 cb
.header
.opcode
= CMD_CONST_BUFFER
;
188 cb
.header
.length
= sizeof(cb
)/4 - 2;
190 cb
.bits0
.buffer_length
= 0;
191 cb
.bits0
.buffer_address
= 0;
192 BRW_BATCH_STRUCT(brw
, &cb
);
194 if (brw
->curbe
.last_buf
) {
195 free(brw
->curbe
.last_buf
);
196 brw
->curbe
.last_buf
= NULL
;
197 brw
->curbe
.last_bufsz
= 0;
203 buf
= (float *)malloc(bufsz
);
205 memset(buf
, 0, bufsz
);
207 if (brw
->curbe
.wm_size
) {
208 unsigned offset
= brw
->curbe
.wm_start
* 16;
210 /* First the constant buffer constants:
213 /* Then any internally generated constants:
215 for (i
= 0; i
< brw
->wm
.prog_data
->nr_internal_consts
; i
++)
216 buf
[offset
+ i
] = brw
->wm
.prog_data
->internal_const
[i
];
218 assert(brw
->wm
.prog_data
->max_const
==
219 brw
->wm
.prog_data
->nr_internal_consts
);
223 /* The clipplanes are actually delivered to both CLIP and VS units.
224 * VS uses them to calculate the outcode bitmasks.
226 if (brw
->curbe
.clip_size
) {
227 unsigned offset
= brw
->curbe
.clip_start
* 16;
230 /* If any planes are going this way, send them all this way:
232 for (i
= 0; i
< 6; i
++) {
233 buf
[offset
+ i
* 4 + 0] = fixed_plane
[i
][0];
234 buf
[offset
+ i
* 4 + 1] = fixed_plane
[i
][1];
235 buf
[offset
+ i
* 4 + 2] = fixed_plane
[i
][2];
236 buf
[offset
+ i
* 4 + 3] = fixed_plane
[i
][3];
239 /* Clip planes: BRW_NEW_CLIP:
241 for (j
= 0; j
< brw
->attribs
.Clip
.nr
; j
++) {
242 buf
[offset
+ i
* 4 + 0] = brw
->attribs
.Clip
.ucp
[j
][0];
243 buf
[offset
+ i
* 4 + 1] = brw
->attribs
.Clip
.ucp
[j
][1];
244 buf
[offset
+ i
* 4 + 2] = brw
->attribs
.Clip
.ucp
[j
][2];
245 buf
[offset
+ i
* 4 + 3] = brw
->attribs
.Clip
.ucp
[j
][3];
251 if (brw
->curbe
.vs_size
) {
252 unsigned offset
= brw
->curbe
.vs_start
* 16;
253 /*unsigned nr = vp->max_const;*/
254 const struct pipe_constant_buffer
*cbuffer
= brw
->attribs
.Constants
[0];
255 struct pipe_winsys
*ws
= brw
->pipe
.winsys
;
256 /* FIXME: buffer size is num_consts + num_immediates */
257 if (brw
->vs
.prog_data
->num_consts
) {
258 /* map the vertex constant buffer and copy to curbe: */
259 void *data
= ws
->buffer_map(ws
, cbuffer
->buffer
, 0);
260 /* FIXME: this is wrong. the cbuffer->size currently
261 * represents size of consts + immediates. so if we'll
262 * have both we'll copy over the end of the buffer
263 * with the subsequent memcpy */
264 memcpy(&buf
[offset
], data
, cbuffer
->size
);
265 ws
->buffer_unmap(ws
, cbuffer
->buffer
);
266 offset
+= cbuffer
->size
;
269 if (brw
->vs
.prog_data
->num_imm
) {
270 memcpy(&buf
[offset
], brw
->vs
.prog_data
->imm_buf
,
271 brw
->vs
.prog_data
->num_imm
* 4 * sizeof(float));
276 for (i
= 0; i
< sz
; i
+=4)
277 debug_printf("curbe %d.%d: %f %f %f %f\n", i
/8, i
&4,
278 buf
[i
+0], buf
[i
+1], buf
[i
+2], buf
[i
+3]);
280 debug_printf("last_buf %p buf %p sz %d/%d cmp %d\n",
281 brw
->curbe
.last_buf
, buf
,
282 bufsz
, brw
->curbe
.last_bufsz
,
283 brw
->curbe
.last_buf
? memcmp(buf
, brw
->curbe
.last_buf
, bufsz
) : -1);
286 if (brw
->curbe
.last_buf
&&
287 bufsz
== brw
->curbe
.last_bufsz
&&
288 memcmp(buf
, brw
->curbe
.last_buf
, bufsz
) == 0) {
293 if (brw
->curbe
.last_buf
)
294 free(brw
->curbe
.last_buf
);
295 brw
->curbe
.last_buf
= buf
;
296 brw
->curbe
.last_bufsz
= bufsz
;
299 if (!brw_pool_alloc(pool
,
302 &brw
->curbe
.gs_offset
)) {
303 debug_printf("out of GS memory for curbe\n");
309 /* Copy data to the buffer:
311 brw
->winsys
->buffer_subdata_typed(brw
->winsys
,
313 brw
->curbe
.gs_offset
,
316 BRW_CONSTANT_BUFFER
);
319 /* TODO: only emit the constant_buffer packet when necessary, ie:
320 - contents have changed
322 - hw requirements due to other packets emitted.
325 struct brw_constant_buffer cb
;
327 memset(&cb
, 0, sizeof(cb
));
329 cb
.header
.opcode
= CMD_CONST_BUFFER
;
330 cb
.header
.length
= sizeof(cb
)/4 - 2;
332 cb
.bits0
.buffer_length
= sz
- 1;
333 cb
.bits0
.buffer_address
= brw
->curbe
.gs_offset
>> 6;
335 /* Because this provokes an action (ie copy the constants into the
336 * URB), it shouldn't be shortcircuited if identical to the
337 * previous time - because eg. the urb destination may have
338 * changed, or the urb contents different to last time.
340 * Note that the data referred to is actually copied internally,
341 * not just used in place according to passed pointer.
343 * It appears that the CS unit takes care of using each available
344 * URB entry (Const URB Entry == CURBE) in turn, and issuing
345 * flushes as necessary when doublebuffering of CURBEs isn't
348 BRW_BATCH_STRUCT(brw
, &cb
);
352 /* This tracked state is unique in that the state it monitors varies
353 * dynamically depending on the parameters tracked by the fragment and
354 * vertex programs. This is the template used as a starting point,
355 * each context will maintain a copy of this internally and update as
358 const struct brw_tracked_state brw_constant_buffer
= {
360 .brw
= (BRW_NEW_CLIP
|
362 BRW_NEW_URB_FENCE
| /* Implicit - hardware requires this, not used above */
363 BRW_NEW_PSP
| /* Implicit - hardware requires this, not used above */
364 BRW_NEW_CURBE_OFFSETS
),
365 .cache
= (CACHE_NEW_WM_PROG
)
367 .update
= upload_constant_buffer