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 "pipe/p_util.h"
44 #define FILE_DEBUG_FLAG DEBUG_FALLBACKS
46 /* Partition the CURBE between the various users of constant values:
48 static void calculate_curbe_offsets( struct brw_context
*brw
)
50 /* CACHE_NEW_WM_PROG */
51 unsigned nr_fp_regs
= align(brw
->wm
.prog_data
->max_const
, 16);
53 /* BRW_NEW_VERTEX_PROGRAM */
54 unsigned nr_vp_regs
= align(brw
->vs
.prog_data
->max_const
, 16);
55 unsigned nr_clip_regs
= 0;
60 if (brw
->attribs
.Transform
->ClipPlanesEnabled
) {
61 unsigned nr_planes
= 6 + brw_count_bits(brw
->attribs
.Transform
->ClipPlanesEnabled
);
62 nr_clip_regs
= align(nr_planes
* 4, 16);
67 total_regs
= nr_fp_regs
+ nr_vp_regs
+ nr_clip_regs
;
69 /* This can happen - what to do? Probably rather than falling
70 * back, the best thing to do is emit programs which code the
71 * constants as immediate values. Could do this either as a static
72 * cap on WM and VS, or adaptively.
74 * Unfortunately, this is currently dependent on the results of the
75 * program generation process (in the case of wm), so this would
76 * introduce the need to re-generate programs in the event of a
77 * curbe allocation failure.
79 /* Max size is 32 - just large enough to
80 * hold the 128 parameters allowed by
81 * the fragment and vertex program
82 * api's. It's not clear what happens
83 * when both VP and FP want to use 128
86 assert(total_regs
<= 32);
90 if (nr_fp_regs
> brw
->curbe
.wm_size
||
91 nr_vp_regs
> brw
->curbe
.vs_size
||
92 nr_clip_regs
!= brw
->curbe
.clip_size
||
93 (total_regs
< brw
->curbe
.total_size
/ 4 &&
94 brw
->curbe
.total_size
> 16)) {
98 /* Calculate a new layout:
101 brw
->curbe
.wm_start
= reg
;
102 brw
->curbe
.wm_size
= nr_fp_regs
; reg
+= nr_fp_regs
;
103 brw
->curbe
.clip_start
= reg
;
104 brw
->curbe
.clip_size
= nr_clip_regs
; reg
+= nr_clip_regs
;
105 brw
->curbe
.vs_start
= reg
;
106 brw
->curbe
.vs_size
= nr_vp_regs
; reg
+= nr_vp_regs
;
107 brw
->curbe
.total_size
= reg
;
111 DBG("curbe wm %d+%d clip %d+%d vs %d+%d\n",
114 brw
->curbe
.clip_start
,
115 brw
->curbe
.clip_size
,
117 brw
->curbe
.vs_size
);
120 brw
->state
.dirty
.brw
|= BRW_NEW_CURBE_OFFSETS
;
125 const struct brw_tracked_state brw_curbe_offsets
= {
127 .brw
= (BRW_NEW_CLIP
|
129 .cache
= CACHE_NEW_WM_PROG
131 .update
= calculate_curbe_offsets
136 /* Define the number of curbes within CS's urb allocation. Multiple
137 * urb entries -> multiple curbes. These will be used by
138 * fixed-function hardware in a double-buffering scheme to avoid a
139 * pipeline stall each time the contents of the curbe is changed.
141 void brw_upload_constant_buffer_state(struct brw_context
*brw
)
143 struct brw_constant_buffer_state cbs
;
144 memset(&cbs
, 0, sizeof(cbs
));
146 /* It appears that this is the state packet for the CS unit, ie. the
147 * urb entries detailed here are housed in the CS range from the
150 cbs
.header
.opcode
= CMD_CONST_BUFFER_STATE
;
151 cbs
.header
.length
= sizeof(cbs
)/4 - 2;
153 /* BRW_NEW_URB_FENCE */
154 cbs
.bits0
.nr_urb_entries
= brw
->urb
.nr_cs_entries
;
155 cbs
.bits0
.urb_entry_size
= brw
->urb
.csize
- 1;
157 assert(brw
->urb
.nr_cs_entries
);
158 BRW_CACHED_BATCH_STRUCT(brw
, &cbs
);
162 static float fixed_plane
[6][4] = {
171 /* Upload a new set of constants. Too much variability to go into the
172 * cache mechanism, but maybe would benefit from a comparison against
173 * the current uploaded set of constants.
175 static void upload_constant_buffer(struct brw_context
*brw
)
177 struct brw_mem_pool
*pool
= &brw
->pool
[BRW_GS_POOL
];
178 unsigned sz
= brw
->curbe
.total_size
;
179 unsigned bufsz
= sz
* sizeof(float);
185 struct brw_constant_buffer cb
;
186 cb
.header
.opcode
= CMD_CONST_BUFFER
;
187 cb
.header
.length
= sizeof(cb
)/4 - 2;
189 cb
.bits0
.buffer_length
= 0;
190 cb
.bits0
.buffer_address
= 0;
191 BRW_BATCH_STRUCT(brw
, &cb
);
193 if (brw
->curbe
.last_buf
) {
194 free(brw
->curbe
.last_buf
);
195 brw
->curbe
.last_buf
= NULL
;
196 brw
->curbe
.last_bufsz
= 0;
202 buf
= (float *)malloc(bufsz
);
204 memset(buf
, 0, bufsz
);
206 if (brw
->curbe
.wm_size
) {
207 unsigned offset
= brw
->curbe
.wm_start
* 16;
209 /* First the constant buffer constants:
212 /* Then any internally generated constants:
214 for (i
= 0; i
< brw
->wm
.prog_data
->nr_internal_consts
; i
++)
215 buf
[offset
+ i
] = brw
->wm
.prog_data
->internal_const
[i
];
217 assert(brw
->wm
.prog_data
->max_const
==
218 brw
->wm
.prog_data
->nr_internal_consts
);
222 /* The clipplanes are actually delivered to both CLIP and VS units.
223 * VS uses them to calculate the outcode bitmasks.
225 if (brw
->curbe
.clip_size
) {
226 unsigned offset
= brw
->curbe
.clip_start
* 16;
229 /* If any planes are going this way, send them all this way:
231 for (i
= 0; i
< 6; i
++) {
232 buf
[offset
+ i
* 4 + 0] = fixed_plane
[i
][0];
233 buf
[offset
+ i
* 4 + 1] = fixed_plane
[i
][1];
234 buf
[offset
+ i
* 4 + 2] = fixed_plane
[i
][2];
235 buf
[offset
+ i
* 4 + 3] = fixed_plane
[i
][3];
238 /* Clip planes: BRW_NEW_CLIP:
240 for (j
= 0; j
< brw
->attribs
.Clip
.nr
; j
++) {
241 buf
[offset
+ i
* 4 + 0] = brw
->attribs
.Clip
.ucp
[j
][0];
242 buf
[offset
+ i
* 4 + 1] = brw
->attribs
.Clip
.ucp
[j
][1];
243 buf
[offset
+ i
* 4 + 2] = brw
->attribs
.Clip
.ucp
[j
][2];
244 buf
[offset
+ i
* 4 + 3] = brw
->attribs
.Clip
.ucp
[j
][3];
250 if (brw
->curbe
.vs_size
) {
251 unsigned offset
= brw
->curbe
.vs_start
* 16;
252 /*unsigned nr = vp->max_const;*/
253 const struct pipe_constant_buffer
*cbuffer
= brw
->attribs
.Constants
[0];
254 struct pipe_winsys
*ws
= brw
->pipe
.winsys
;
255 /* FIXME: buffer size is num_consts + num_immediates */
256 if (brw
->vs
.prog_data
->num_consts
) {
257 /* map the vertex constant buffer and copy to curbe: */
258 void *data
= ws
->buffer_map(ws
, cbuffer
->buffer
, 0);
259 /* FIXME: this is wrong. the cbuffer->size currently
260 * represents size of consts + immediates. so if we'll
261 * have both we'll copy over the end of the buffer
262 * with the subsequent memcpy */
263 memcpy(&buf
[offset
], data
, cbuffer
->size
);
264 ws
->buffer_unmap(ws
, cbuffer
->buffer
);
265 offset
+= cbuffer
->size
;
268 if (brw
->vs
.prog_data
->num_imm
) {
269 memcpy(&buf
[offset
], brw
->vs
.prog_data
->imm_buf
,
270 brw
->vs
.prog_data
->num_imm
* 4 * sizeof(float));
275 for (i
= 0; i
< sz
; i
+=4)
276 debug_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 debug_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
.last_buf
&&
286 bufsz
== brw
->curbe
.last_bufsz
&&
287 memcmp(buf
, brw
->curbe
.last_buf
, bufsz
) == 0) {
292 if (brw
->curbe
.last_buf
)
293 free(brw
->curbe
.last_buf
);
294 brw
->curbe
.last_buf
= buf
;
295 brw
->curbe
.last_bufsz
= bufsz
;
298 if (!brw_pool_alloc(pool
,
301 &brw
->curbe
.gs_offset
)) {
302 debug_printf("out of GS memory for curbe\n");
308 /* Copy data to the buffer:
310 brw
->winsys
->buffer_subdata_typed(brw
->winsys
,
312 brw
->curbe
.gs_offset
,
315 BRW_CONSTANT_BUFFER
);
318 /* TODO: only emit the constant_buffer packet when necessary, ie:
319 - contents have changed
321 - hw requirements due to other packets emitted.
324 struct brw_constant_buffer cb
;
326 memset(&cb
, 0, sizeof(cb
));
328 cb
.header
.opcode
= CMD_CONST_BUFFER
;
329 cb
.header
.length
= sizeof(cb
)/4 - 2;
331 cb
.bits0
.buffer_length
= sz
- 1;
332 cb
.bits0
.buffer_address
= brw
->curbe
.gs_offset
>> 6;
334 /* Because this provokes an action (ie copy the constants into the
335 * URB), it shouldn't be shortcircuited if identical to the
336 * previous time - because eg. the urb destination may have
337 * changed, or the urb contents different to last time.
339 * Note that the data referred to is actually copied internally,
340 * not just used in place according to passed pointer.
342 * It appears that the CS unit takes care of using each available
343 * URB entry (Const URB Entry == CURBE) in turn, and issuing
344 * flushes as necessary when doublebuffering of CURBEs isn't
347 BRW_BATCH_STRUCT(brw
, &cb
);
351 /* This tracked state is unique in that the state it monitors varies
352 * dynamically depending on the parameters tracked by the fragment and
353 * vertex programs. This is the template used as a starting point,
354 * each context will maintain a copy of this internally and update as
357 const struct brw_tracked_state brw_constant_buffer
= {
359 .brw
= (BRW_NEW_CLIP
|
361 BRW_NEW_URB_FENCE
| /* Implicit - hardware requires this, not used above */
362 BRW_NEW_PSP
| /* Implicit - hardware requires this, not used above */
363 BRW_NEW_CURBE_OFFSETS
),
364 .cache
= (CACHE_NEW_WM_PROG
)
366 .update
= upload_constant_buffer