Merge commit 'origin/master' into gallium-0.2
[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 "util/u_math.h"
43 #include "util/u_memory.h"
44
45 #define FILE_DEBUG_FLAG DEBUG_FALLBACKS
46
47 /* Partition the CURBE between the various users of constant values:
48 */
49 static void calculate_curbe_offsets( struct brw_context *brw )
50 {
51 /* CACHE_NEW_WM_PROG */
52 unsigned nr_fp_regs = align(brw->wm.prog_data->max_const, 16);
53
54 /* BRW_NEW_VERTEX_PROGRAM */
55 unsigned nr_vp_regs = align(brw->vs.prog_data->max_const, 16);
56 unsigned nr_clip_regs = 0;
57 unsigned total_regs;
58
59 #if 0
60 /* BRW_NEW_CLIP ? */
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);
64 }
65 #endif
66
67
68 total_regs = nr_fp_regs + nr_vp_regs + nr_clip_regs;
69
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.
74 *
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.
79 */
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
85 * parameters, though.
86 */
87 assert(total_regs <= 32);
88
89 /* Lazy resize:
90 */
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)) {
96
97 unsigned reg = 0;
98
99 /* Calculate a new layout:
100 */
101 reg = 0;
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;
109
110 #if 0
111 if (0)
112 DBG("curbe wm %d+%d clip %d+%d vs %d+%d\n",
113 brw->curbe.wm_start,
114 brw->curbe.wm_size,
115 brw->curbe.clip_start,
116 brw->curbe.clip_size,
117 brw->curbe.vs_start,
118 brw->curbe.vs_size );
119 #endif
120
121 brw->state.dirty.brw |= BRW_NEW_CURBE_OFFSETS;
122 }
123 }
124
125
126 const struct brw_tracked_state brw_curbe_offsets = {
127 .dirty = {
128 .brw = (BRW_NEW_CLIP |
129 BRW_NEW_VS),
130 .cache = CACHE_NEW_WM_PROG
131 },
132 .update = calculate_curbe_offsets
133 };
134
135
136
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.
141 */
142 void brw_upload_constant_buffer_state(struct brw_context *brw)
143 {
144 struct brw_constant_buffer_state cbs;
145 memset(&cbs, 0, sizeof(cbs));
146
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
149 * URB_FENCE command.
150 */
151 cbs.header.opcode = CMD_CONST_BUFFER_STATE;
152 cbs.header.length = sizeof(cbs)/4 - 2;
153
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;
157
158 assert(brw->urb.nr_cs_entries);
159 BRW_CACHED_BATCH_STRUCT(brw, &cbs);
160 }
161
162
163 static float fixed_plane[6][4] = {
164 { 0, 0, -1, 1 },
165 { 0, 0, 1, 1 },
166 { 0, -1, 0, 1 },
167 { 0, 1, 0, 1 },
168 {-1, 0, 0, 1 },
169 { 1, 0, 0, 1 }
170 };
171
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.
175 */
176 static void upload_constant_buffer(struct brw_context *brw)
177 {
178 struct brw_mem_pool *pool = &brw->pool[BRW_GS_POOL];
179 unsigned sz = brw->curbe.total_size;
180 unsigned bufsz = sz * sizeof(float);
181 float *buf;
182 unsigned i;
183
184
185 if (sz == 0) {
186 struct brw_constant_buffer cb;
187 cb.header.opcode = CMD_CONST_BUFFER;
188 cb.header.length = sizeof(cb)/4 - 2;
189 cb.header.valid = 0;
190 cb.bits0.buffer_length = 0;
191 cb.bits0.buffer_address = 0;
192 BRW_BATCH_STRUCT(brw, &cb);
193
194 if (brw->curbe.last_buf) {
195 free(brw->curbe.last_buf);
196 brw->curbe.last_buf = NULL;
197 brw->curbe.last_bufsz = 0;
198 }
199
200 return;
201 }
202
203 buf = (float *)malloc(bufsz);
204
205 memset(buf, 0, bufsz);
206
207 if (brw->curbe.wm_size) {
208 unsigned offset = brw->curbe.wm_start * 16;
209
210 /* First the constant buffer constants:
211 */
212
213 /* Then any internally generated constants:
214 */
215 for (i = 0; i < brw->wm.prog_data->nr_internal_consts; i++)
216 buf[offset + i] = brw->wm.prog_data->internal_const[i];
217
218 assert(brw->wm.prog_data->max_const ==
219 brw->wm.prog_data->nr_internal_consts);
220 }
221
222
223 /* The clipplanes are actually delivered to both CLIP and VS units.
224 * VS uses them to calculate the outcode bitmasks.
225 */
226 if (brw->curbe.clip_size) {
227 unsigned offset = brw->curbe.clip_start * 16;
228 unsigned j;
229
230 /* If any planes are going this way, send them all this way:
231 */
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];
237 }
238
239 /* Clip planes: BRW_NEW_CLIP:
240 */
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];
246 i++;
247 }
248 }
249
250
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;
267 }
268 /*immediates*/
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));
272 }
273 }
274
275 if (1) {
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]);
279
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);
284 }
285
286 if (brw->curbe.last_buf &&
287 bufsz == brw->curbe.last_bufsz &&
288 memcmp(buf, brw->curbe.last_buf, bufsz) == 0) {
289 free(buf);
290 /* return; */
291 }
292 else {
293 if (brw->curbe.last_buf)
294 free(brw->curbe.last_buf);
295 brw->curbe.last_buf = buf;
296 brw->curbe.last_bufsz = bufsz;
297
298
299 if (!brw_pool_alloc(pool,
300 bufsz,
301 1 << 6,
302 &brw->curbe.gs_offset)) {
303 debug_printf("out of GS memory for curbe\n");
304 assert(0);
305 return;
306 }
307
308
309 /* Copy data to the buffer:
310 */
311 brw->winsys->buffer_subdata_typed(brw->winsys,
312 pool->buffer,
313 brw->curbe.gs_offset,
314 bufsz,
315 buf,
316 BRW_CONSTANT_BUFFER );
317 }
318
319 /* TODO: only emit the constant_buffer packet when necessary, ie:
320 - contents have changed
321 - offset has changed
322 - hw requirements due to other packets emitted.
323 */
324 {
325 struct brw_constant_buffer cb;
326
327 memset(&cb, 0, sizeof(cb));
328
329 cb.header.opcode = CMD_CONST_BUFFER;
330 cb.header.length = sizeof(cb)/4 - 2;
331 cb.header.valid = 1;
332 cb.bits0.buffer_length = sz - 1;
333 cb.bits0.buffer_address = brw->curbe.gs_offset >> 6;
334
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.
339 *
340 * Note that the data referred to is actually copied internally,
341 * not just used in place according to passed pointer.
342 *
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
346 * possible.
347 */
348 BRW_BATCH_STRUCT(brw, &cb);
349 }
350 }
351
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
356 * required.
357 */
358 const struct brw_tracked_state brw_constant_buffer = {
359 .dirty = {
360 .brw = (BRW_NEW_CLIP |
361 BRW_NEW_CONSTANTS |
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)
366 },
367 .update = upload_constant_buffer
368 };
369