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