gallium: move pipe_copy_rect(), pipe_fill_rect() protos into new u_rect.h header
[mesa.git] / src / gallium / drivers / i965simple / brw_curbe.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
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:
13
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.
17
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.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33
34 #include "brw_context.h"
35 #include "brw_defines.h"
36 #include "brw_state.h"
37 #include "brw_batch.h"
38 #include "brw_util.h"
39 #include "brw_wm.h"
40 #include "pipe/p_state.h"
41 #include "pipe/p_winsys.h"
42 #include "pipe/p_util.h"
43
44 #define FILE_DEBUG_FLAG DEBUG_FALLBACKS
45
46 /* Partition the CURBE between the various users of constant values:
47 */
48 static void calculate_curbe_offsets( struct brw_context *brw )
49 {
50 /* CACHE_NEW_WM_PROG */
51 unsigned nr_fp_regs = align(brw->wm.prog_data->max_const, 16);
52
53 /* BRW_NEW_VERTEX_PROGRAM */
54 unsigned nr_vp_regs = align(brw->vs.prog_data->max_const, 16);
55 unsigned nr_clip_regs = 0;
56 unsigned total_regs;
57
58 #if 0
59 /* BRW_NEW_CLIP ? */
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);
63 }
64 #endif
65
66
67 total_regs = nr_fp_regs + nr_vp_regs + nr_clip_regs;
68
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.
73 *
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.
78 */
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
84 * parameters, though.
85 */
86 assert(total_regs <= 32);
87
88 /* Lazy resize:
89 */
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)) {
95
96 unsigned reg = 0;
97
98 /* Calculate a new layout:
99 */
100 reg = 0;
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;
108
109 #if 0
110 if (0)
111 DBG("curbe wm %d+%d clip %d+%d vs %d+%d\n",
112 brw->curbe.wm_start,
113 brw->curbe.wm_size,
114 brw->curbe.clip_start,
115 brw->curbe.clip_size,
116 brw->curbe.vs_start,
117 brw->curbe.vs_size );
118 #endif
119
120 brw->state.dirty.brw |= BRW_NEW_CURBE_OFFSETS;
121 }
122 }
123
124
125 const struct brw_tracked_state brw_curbe_offsets = {
126 .dirty = {
127 .brw = (BRW_NEW_CLIP |
128 BRW_NEW_VS),
129 .cache = CACHE_NEW_WM_PROG
130 },
131 .update = calculate_curbe_offsets
132 };
133
134
135
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.
140 */
141 void brw_upload_constant_buffer_state(struct brw_context *brw)
142 {
143 struct brw_constant_buffer_state cbs;
144 memset(&cbs, 0, sizeof(cbs));
145
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
148 * URB_FENCE command.
149 */
150 cbs.header.opcode = CMD_CONST_BUFFER_STATE;
151 cbs.header.length = sizeof(cbs)/4 - 2;
152
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;
156
157 assert(brw->urb.nr_cs_entries);
158 BRW_CACHED_BATCH_STRUCT(brw, &cbs);
159 }
160
161
162 static float fixed_plane[6][4] = {
163 { 0, 0, -1, 1 },
164 { 0, 0, 1, 1 },
165 { 0, -1, 0, 1 },
166 { 0, 1, 0, 1 },
167 {-1, 0, 0, 1 },
168 { 1, 0, 0, 1 }
169 };
170
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.
174 */
175 static void upload_constant_buffer(struct brw_context *brw)
176 {
177 struct brw_mem_pool *pool = &brw->pool[BRW_GS_POOL];
178 unsigned sz = brw->curbe.total_size;
179 unsigned bufsz = sz * sizeof(float);
180 float *buf;
181 unsigned i;
182
183
184 if (sz == 0) {
185 struct brw_constant_buffer cb;
186 cb.header.opcode = CMD_CONST_BUFFER;
187 cb.header.length = sizeof(cb)/4 - 2;
188 cb.header.valid = 0;
189 cb.bits0.buffer_length = 0;
190 cb.bits0.buffer_address = 0;
191 BRW_BATCH_STRUCT(brw, &cb);
192
193 if (brw->curbe.last_buf) {
194 free(brw->curbe.last_buf);
195 brw->curbe.last_buf = NULL;
196 brw->curbe.last_bufsz = 0;
197 }
198
199 return;
200 }
201
202 buf = (float *)malloc(bufsz);
203
204 memset(buf, 0, bufsz);
205
206 if (brw->curbe.wm_size) {
207 unsigned offset = brw->curbe.wm_start * 16;
208
209 /* First the constant buffer constants:
210 */
211
212 /* Then any internally generated constants:
213 */
214 for (i = 0; i < brw->wm.prog_data->nr_internal_consts; i++)
215 buf[offset + i] = brw->wm.prog_data->internal_const[i];
216
217 assert(brw->wm.prog_data->max_const ==
218 brw->wm.prog_data->nr_internal_consts);
219 }
220
221
222 /* The clipplanes are actually delivered to both CLIP and VS units.
223 * VS uses them to calculate the outcode bitmasks.
224 */
225 if (brw->curbe.clip_size) {
226 unsigned offset = brw->curbe.clip_start * 16;
227 unsigned j;
228
229 /* If any planes are going this way, send them all this way:
230 */
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];
236 }
237
238 /* Clip planes: BRW_NEW_CLIP:
239 */
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];
245 i++;
246 }
247 }
248
249
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;
266 }
267 /*immediates*/
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));
271 }
272 }
273
274 if (1) {
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]);
278
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);
283 }
284
285 if (brw->curbe.last_buf &&
286 bufsz == brw->curbe.last_bufsz &&
287 memcmp(buf, brw->curbe.last_buf, bufsz) == 0) {
288 free(buf);
289 /* return; */
290 }
291 else {
292 if (brw->curbe.last_buf)
293 free(brw->curbe.last_buf);
294 brw->curbe.last_buf = buf;
295 brw->curbe.last_bufsz = bufsz;
296
297
298 if (!brw_pool_alloc(pool,
299 bufsz,
300 1 << 6,
301 &brw->curbe.gs_offset)) {
302 debug_printf("out of GS memory for curbe\n");
303 assert(0);
304 return;
305 }
306
307
308 /* Copy data to the buffer:
309 */
310 brw->winsys->buffer_subdata_typed(brw->winsys,
311 pool->buffer,
312 brw->curbe.gs_offset,
313 bufsz,
314 buf,
315 BRW_CONSTANT_BUFFER );
316 }
317
318 /* TODO: only emit the constant_buffer packet when necessary, ie:
319 - contents have changed
320 - offset has changed
321 - hw requirements due to other packets emitted.
322 */
323 {
324 struct brw_constant_buffer cb;
325
326 memset(&cb, 0, sizeof(cb));
327
328 cb.header.opcode = CMD_CONST_BUFFER;
329 cb.header.length = sizeof(cb)/4 - 2;
330 cb.header.valid = 1;
331 cb.bits0.buffer_length = sz - 1;
332 cb.bits0.buffer_address = brw->curbe.gs_offset >> 6;
333
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.
338 *
339 * Note that the data referred to is actually copied internally,
340 * not just used in place according to passed pointer.
341 *
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
345 * possible.
346 */
347 BRW_BATCH_STRUCT(brw, &cb);
348 }
349 }
350
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
355 * required.
356 */
357 const struct brw_tracked_state brw_constant_buffer = {
358 .dirty = {
359 .brw = (BRW_NEW_CLIP |
360 BRW_NEW_CONSTANTS |
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)
365 },
366 .update = upload_constant_buffer
367 };
368