Update DRI drivers for new glsl compiler.
[mesa.git] / src / mesa / drivers / dri / i965 / 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 "glheader.h"
35 #include "context.h"
36 #include "macros.h"
37 #include "enums.h"
38 #include "shader/prog_parameter.h"
39 #include "shader/prog_statevars.h"
40 #include "intel_batchbuffer.h"
41 #include "brw_context.h"
42 #include "brw_defines.h"
43 #include "brw_state.h"
44 #include "brw_util.h"
45 #include "brw_aub.h"
46
47
48 /* Partition the CURBE between the various users of constant values:
49 */
50 static void calculate_curbe_offsets( struct brw_context *brw )
51 {
52 /* CACHE_NEW_WM_PROG */
53 GLuint nr_fp_regs = (brw->wm.prog_data->nr_params + 15) / 16;
54
55 /* BRW_NEW_VERTEX_PROGRAM */
56 struct brw_vertex_program *vp = (struct brw_vertex_program *)brw->vertex_program;
57 GLuint nr_vp_regs = (vp->program.Base.Parameters->NumParameters * 4 + 15) / 16;
58 GLuint nr_clip_regs = 0;
59 GLuint total_regs;
60
61 /* _NEW_TRANSFORM */
62 if (brw->attribs.Transform->ClipPlanesEnabled) {
63 GLuint nr_planes = 6 + brw_count_bits(brw->attribs.Transform->ClipPlanesEnabled);
64 nr_clip_regs = (nr_planes * 4 + 15) / 16;
65 }
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 GLuint 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 _mesa_printf("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
119 brw->state.dirty.brw |= BRW_NEW_CURBE_OFFSETS;
120 }
121 }
122
123
124 const struct brw_tracked_state brw_curbe_offsets = {
125 .dirty = {
126 .mesa = _NEW_TRANSFORM,
127 .brw = BRW_NEW_VERTEX_PROGRAM,
128 .cache = CACHE_NEW_WM_PROG
129 },
130 .update = calculate_curbe_offsets
131 };
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 #if 0
162 const struct brw_tracked_state brw_constant_buffer_state = {
163 .dirty = {
164 .mesa = 0,
165 .brw = BRW_NEW_URB_FENCE,
166 .cache = 0
167 },
168 .update = brw_upload_constant_buffer_state
169 };
170 #endif
171
172
173 static GLfloat fixed_plane[6][4] = {
174 { 0, 0, -1, 1 },
175 { 0, 0, 1, 1 },
176 { 0, -1, 0, 1 },
177 { 0, 1, 0, 1 },
178 {-1, 0, 0, 1 },
179 { 1, 0, 0, 1 }
180 };
181
182 /* Upload a new set of constants. Too much variability to go into the
183 * cache mechanism, but maybe would benefit from a comparison against
184 * the current uploaded set of constants.
185 */
186 static void upload_constant_buffer(struct brw_context *brw)
187 {
188 GLcontext *ctx = &brw->intel.ctx;
189 struct brw_vertex_program *vp = (struct brw_vertex_program *)brw->vertex_program;
190 struct brw_fragment_program *fp = (struct brw_fragment_program *)brw->fragment_program;
191 struct brw_mem_pool *pool = &brw->pool[BRW_GS_POOL];
192 GLuint sz = brw->curbe.total_size;
193 GLuint bufsz = sz * 16 * sizeof(GLfloat);
194 GLfloat *buf;
195 GLuint i;
196
197 /* Update our own dependency flags. This works because this
198 * function will also be called whenever fp or vp changes.
199 */
200 brw->curbe.tracked_state.dirty.mesa = (_NEW_TRANSFORM|_NEW_PROJECTION);
201 brw->curbe.tracked_state.dirty.mesa |= vp->param_state;
202 brw->curbe.tracked_state.dirty.mesa |= fp->param_state;
203
204 if (sz == 0) {
205 struct brw_constant_buffer cb;
206 cb.header.opcode = CMD_CONST_BUFFER;
207 cb.header.length = sizeof(cb)/4 - 2;
208 cb.header.valid = 0;
209 cb.bits0.buffer_length = 0;
210 cb.bits0.buffer_address = 0;
211 BRW_BATCH_STRUCT(brw, &cb);
212
213 if (brw->curbe.last_buf) {
214 free(brw->curbe.last_buf);
215 brw->curbe.last_buf = NULL;
216 brw->curbe.last_bufsz = 0;
217 }
218
219 return;
220 }
221
222 buf = (GLfloat *)malloc(bufsz);
223
224 memset(buf, 0, bufsz);
225
226 if (brw->curbe.wm_size) {
227 GLuint offset = brw->curbe.wm_start * 16;
228
229 _mesa_load_state_parameters(ctx, fp->program.Base.Parameters);
230
231 for (i = 0; i < brw->wm.prog_data->nr_params; i++)
232 buf[offset + i] = brw->wm.prog_data->param[i][0];
233 }
234
235
236 /* The clipplanes are actually delivered to both CLIP and VS units.
237 * VS uses them to calculate the outcode bitmasks.
238 */
239 if (brw->curbe.clip_size) {
240 GLuint offset = brw->curbe.clip_start * 16;
241 GLuint j;
242
243 /* If any planes are going this way, send them all this way:
244 */
245 for (i = 0; i < 6; i++) {
246 buf[offset + i * 4 + 0] = fixed_plane[i][0];
247 buf[offset + i * 4 + 1] = fixed_plane[i][1];
248 buf[offset + i * 4 + 2] = fixed_plane[i][2];
249 buf[offset + i * 4 + 3] = fixed_plane[i][3];
250 }
251
252 /* Clip planes: _NEW_TRANSFORM plus _NEW_PROJECTION to get to
253 * clip-space:
254 */
255 assert(MAX_CLIP_PLANES == 6);
256 for (j = 0; j < MAX_CLIP_PLANES; j++) {
257 if (brw->attribs.Transform->ClipPlanesEnabled & (1<<j)) {
258 buf[offset + i * 4 + 0] = brw->attribs.Transform->_ClipUserPlane[j][0];
259 buf[offset + i * 4 + 1] = brw->attribs.Transform->_ClipUserPlane[j][1];
260 buf[offset + i * 4 + 2] = brw->attribs.Transform->_ClipUserPlane[j][2];
261 buf[offset + i * 4 + 3] = brw->attribs.Transform->_ClipUserPlane[j][3];
262 i++;
263 }
264 }
265 }
266
267
268 if (brw->curbe.vs_size) {
269 GLuint offset = brw->curbe.vs_start * 16;
270 GLuint nr = vp->program.Base.Parameters->NumParameters;
271
272 _mesa_load_state_parameters(ctx, vp->program.Base.Parameters);
273
274 for (i = 0; i < nr; i++) {
275 buf[offset + i * 4 + 0] = vp->program.Base.Parameters->ParameterValues[i][0];
276 buf[offset + i * 4 + 1] = vp->program.Base.Parameters->ParameterValues[i][1];
277 buf[offset + i * 4 + 2] = vp->program.Base.Parameters->ParameterValues[i][2];
278 buf[offset + i * 4 + 3] = vp->program.Base.Parameters->ParameterValues[i][3];
279 }
280 }
281
282 if (0) {
283 for (i = 0; i < sz*16; i+=4)
284 _mesa_printf("curbe %d.%d: %f %f %f %f\n", i/8, i&4,
285 buf[i+0], buf[i+1], buf[i+2], buf[i+3]);
286
287 _mesa_printf("last_buf %p buf %p sz %d/%d cmp %d\n",
288 brw->curbe.last_buf, buf,
289 bufsz, brw->curbe.last_bufsz,
290 brw->curbe.last_buf ? memcmp(buf, brw->curbe.last_buf, bufsz) : -1);
291 }
292
293 if (brw->curbe.last_buf &&
294 bufsz == brw->curbe.last_bufsz &&
295 memcmp(buf, brw->curbe.last_buf, bufsz) == 0) {
296 free(buf);
297 /* return; */
298 }
299 else {
300 if (brw->curbe.last_buf)
301 free(brw->curbe.last_buf);
302 brw->curbe.last_buf = buf;
303 brw->curbe.last_bufsz = bufsz;
304
305
306 if (!brw_pool_alloc(pool,
307 bufsz,
308 6,
309 &brw->curbe.gs_offset)) {
310 _mesa_printf("out of GS memory for curbe\n");
311 assert(0);
312 return;
313 }
314
315
316 /* Copy data to the buffer:
317 */
318 bmBufferSubDataAUB(&brw->intel,
319 pool->buffer,
320 brw->curbe.gs_offset,
321 bufsz,
322 buf,
323 DW_CONSTANT_BUFFER,
324 0);
325 }
326
327 /* TODO: only emit the constant_buffer packet when necessary, ie:
328 - contents have changed
329 - offset has changed
330 - hw requirements due to other packets emitted.
331 */
332 {
333 struct brw_constant_buffer cb;
334
335 memset(&cb, 0, sizeof(cb));
336
337 cb.header.opcode = CMD_CONST_BUFFER;
338 cb.header.length = sizeof(cb)/4 - 2;
339 cb.header.valid = 1;
340 cb.bits0.buffer_length = sz - 1;
341 cb.bits0.buffer_address = brw->curbe.gs_offset >> 6;
342
343 /* Because this provokes an action (ie copy the constants into the
344 * URB), it shouldn't be shortcircuited if identical to the
345 * previous time - because eg. the urb destination may have
346 * changed, or the urb contents different to last time.
347 *
348 * Note that the data referred to is actually copied internally,
349 * not just used in place according to passed pointer.
350 *
351 * It appears that the CS unit takes care of using each available
352 * URB entry (Const URB Entry == CURBE) in turn, and issuing
353 * flushes as necessary when doublebuffering of CURBEs isn't
354 * possible.
355 */
356 /* intel_batchbuffer_align(brw->intel.batch, 64, sizeof(cb)); */
357 BRW_BATCH_STRUCT(brw, &cb);
358 /* intel_batchbuffer_align(brw->intel.batch, 64, 0); */
359 }
360 }
361
362 /* This tracked state is unique in that the state it monitors varies
363 * dynamically depending on the parameters tracked by the fragment and
364 * vertex programs. This is the template used as a starting point,
365 * each context will maintain a copy of this internally and update as
366 * required.
367 */
368 const struct brw_tracked_state brw_constant_buffer = {
369 .dirty = {
370 .mesa = (_NEW_TRANSFORM|_NEW_PROJECTION), /* plus fp and vp flags */
371 .brw = (BRW_NEW_FRAGMENT_PROGRAM |
372 BRW_NEW_VERTEX_PROGRAM |
373 BRW_NEW_URB_FENCE | /* Implicit - hardware requires this, not used above */
374 BRW_NEW_PSP | /* Implicit - hardware requires this, not used above */
375 BRW_NEW_CURBE_OFFSETS),
376 .cache = (CACHE_NEW_WM_PROG)
377 },
378 .update = upload_constant_buffer
379 };
380