Merge remote branch 'origin/master' into radeon-rewrite
[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 "main/glheader.h"
35 #include "main/context.h"
36 #include "main/macros.h"
37 #include "main/enums.h"
38 #include "shader/prog_parameter.h"
39 #include "shader/prog_statevars.h"
40 #include "intel_batchbuffer.h"
41 #include "intel_regions.h"
42 #include "brw_context.h"
43 #include "brw_defines.h"
44 #include "brw_state.h"
45 #include "brw_util.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 GLcontext *ctx = &brw->intel.ctx;
53 /* CACHE_NEW_WM_PROG */
54 GLuint nr_fp_regs = (brw->wm.prog_data->nr_params + 15) / 16;
55
56 /* BRW_NEW_VERTEX_PROGRAM */
57 const struct brw_vertex_program *vp = brw_vertex_program_const(brw->vertex_program);
58 GLuint nr_vp_regs = (vp->program.Base.Parameters->NumParameters * 4 + 15) / 16;
59 GLuint nr_clip_regs = 0;
60 GLuint total_regs;
61
62 /* _NEW_TRANSFORM */
63 if (ctx->Transform.ClipPlanesEnabled) {
64 GLuint nr_planes = 6 + brw_count_bits(ctx->Transform.ClipPlanesEnabled);
65 nr_clip_regs = (nr_planes * 4 + 15) / 16;
66 }
67
68
69 total_regs = nr_fp_regs + nr_vp_regs + nr_clip_regs;
70
71 /* This can happen - what to do? Probably rather than falling
72 * back, the best thing to do is emit programs which code the
73 * constants as immediate values. Could do this either as a static
74 * cap on WM and VS, or adaptively.
75 *
76 * Unfortunately, this is currently dependent on the results of the
77 * program generation process (in the case of wm), so this would
78 * introduce the need to re-generate programs in the event of a
79 * curbe allocation failure.
80 */
81 /* Max size is 32 - just large enough to
82 * hold the 128 parameters allowed by
83 * the fragment and vertex program
84 * api's. It's not clear what happens
85 * when both VP and FP want to use 128
86 * parameters, though.
87 */
88 assert(total_regs <= 32);
89
90 /* Lazy resize:
91 */
92 if (nr_fp_regs > brw->curbe.wm_size ||
93 nr_vp_regs > brw->curbe.vs_size ||
94 nr_clip_regs != brw->curbe.clip_size ||
95 (total_regs < brw->curbe.total_size / 4 &&
96 brw->curbe.total_size > 16)) {
97
98 GLuint reg = 0;
99
100 /* Calculate a new layout:
101 */
102 reg = 0;
103 brw->curbe.wm_start = reg;
104 brw->curbe.wm_size = nr_fp_regs; reg += nr_fp_regs;
105 brw->curbe.clip_start = reg;
106 brw->curbe.clip_size = nr_clip_regs; reg += nr_clip_regs;
107 brw->curbe.vs_start = reg;
108 brw->curbe.vs_size = nr_vp_regs; reg += nr_vp_regs;
109 brw->curbe.total_size = reg;
110
111 if (0)
112 _mesa_printf("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
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 .mesa = _NEW_TRANSFORM,
128 .brw = BRW_NEW_VERTEX_PROGRAM,
129 .cache = CACHE_NEW_WM_PROG
130 },
131 .prepare = calculate_curbe_offsets
132 };
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_cs_urb_state(struct brw_context *brw)
143 {
144 struct brw_cs_urb_state cs_urb;
145 memset(&cs_urb, 0, sizeof(cs_urb));
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 cs_urb.header.opcode = CMD_CS_URB_STATE;
152 cs_urb.header.length = sizeof(cs_urb)/4 - 2;
153
154 /* BRW_NEW_URB_FENCE */
155 cs_urb.bits0.nr_urb_entries = brw->urb.nr_cs_entries;
156 cs_urb.bits0.urb_entry_size = brw->urb.csize - 1;
157
158 assert(brw->urb.nr_cs_entries);
159 BRW_CACHED_BATCH_STRUCT(brw, &cs_urb);
160 }
161
162 static GLfloat 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 prepare_constant_buffer(struct brw_context *brw)
176 {
177 GLcontext *ctx = &brw->intel.ctx;
178 const struct brw_vertex_program *vp =
179 brw_vertex_program_const(brw->vertex_program);
180 const struct brw_fragment_program *fp =
181 brw_fragment_program_const(brw->fragment_program);
182 const GLuint sz = brw->curbe.total_size;
183 const GLuint bufsz = sz * 16 * sizeof(GLfloat);
184 GLfloat *buf;
185 GLuint i;
186
187 /* Update our own dependency flags. This works because this
188 * function will also be called whenever fp or vp changes.
189 */
190 brw->curbe.tracked_state.dirty.mesa = (_NEW_TRANSFORM|_NEW_PROJECTION);
191 brw->curbe.tracked_state.dirty.mesa |= vp->program.Base.Parameters->StateFlags;
192 brw->curbe.tracked_state.dirty.mesa |= fp->program.Base.Parameters->StateFlags;
193
194 if (sz == 0) {
195 if (brw->curbe.last_buf) {
196 free(brw->curbe.last_buf);
197 brw->curbe.last_buf = NULL;
198 brw->curbe.last_bufsz = 0;
199 }
200 return;
201 }
202
203 buf = (GLfloat *) _mesa_calloc(bufsz);
204
205 /* fragment shader constants */
206 if (brw->curbe.wm_size) {
207 GLuint offset = brw->curbe.wm_start * 16;
208
209 _mesa_load_state_parameters(ctx, fp->program.Base.Parameters);
210
211 /* copy float constants */
212 for (i = 0; i < brw->wm.prog_data->nr_params; i++)
213 buf[offset + i] = *brw->wm.prog_data->param[i];
214 }
215
216
217 /* The clipplanes are actually delivered to both CLIP and VS units.
218 * VS uses them to calculate the outcode bitmasks.
219 */
220 if (brw->curbe.clip_size) {
221 GLuint offset = brw->curbe.clip_start * 16;
222 GLuint j;
223
224 /* If any planes are going this way, send them all this way:
225 */
226 for (i = 0; i < 6; i++) {
227 buf[offset + i * 4 + 0] = fixed_plane[i][0];
228 buf[offset + i * 4 + 1] = fixed_plane[i][1];
229 buf[offset + i * 4 + 2] = fixed_plane[i][2];
230 buf[offset + i * 4 + 3] = fixed_plane[i][3];
231 }
232
233 /* Clip planes: _NEW_TRANSFORM plus _NEW_PROJECTION to get to
234 * clip-space:
235 */
236 assert(MAX_CLIP_PLANES == 6);
237 for (j = 0; j < MAX_CLIP_PLANES; j++) {
238 if (ctx->Transform.ClipPlanesEnabled & (1<<j)) {
239 buf[offset + i * 4 + 0] = ctx->Transform._ClipUserPlane[j][0];
240 buf[offset + i * 4 + 1] = ctx->Transform._ClipUserPlane[j][1];
241 buf[offset + i * 4 + 2] = ctx->Transform._ClipUserPlane[j][2];
242 buf[offset + i * 4 + 3] = ctx->Transform._ClipUserPlane[j][3];
243 i++;
244 }
245 }
246 }
247
248 /* vertex shader constants */
249 if (brw->curbe.vs_size) {
250 GLuint offset = brw->curbe.vs_start * 16;
251 GLuint nr = vp->program.Base.Parameters->NumParameters;
252
253 _mesa_load_state_parameters(ctx, vp->program.Base.Parameters);
254
255 /* XXX just use a memcpy here */
256 for (i = 0; i < nr; i++) {
257 const GLfloat *value = vp->program.Base.Parameters->ParameterValues[i];
258 buf[offset + i * 4 + 0] = value[0];
259 buf[offset + i * 4 + 1] = value[1];
260 buf[offset + i * 4 + 2] = value[2];
261 buf[offset + i * 4 + 3] = value[3];
262 }
263 }
264
265 if (0) {
266 for (i = 0; i < sz*16; i+=4)
267 _mesa_printf("curbe %d.%d: %f %f %f %f\n", i/8, i&4,
268 buf[i+0], buf[i+1], buf[i+2], buf[i+3]);
269
270 _mesa_printf("last_buf %p buf %p sz %d/%d cmp %d\n",
271 brw->curbe.last_buf, buf,
272 bufsz, brw->curbe.last_bufsz,
273 brw->curbe.last_buf ? memcmp(buf, brw->curbe.last_buf, bufsz) : -1);
274 }
275
276 if (brw->curbe.curbe_bo != NULL &&
277 brw->curbe.last_buf &&
278 bufsz == brw->curbe.last_bufsz &&
279 memcmp(buf, brw->curbe.last_buf, bufsz) == 0) {
280 /* constants have not changed */
281 _mesa_free(buf);
282 }
283 else {
284 /* constants have changed */
285 if (brw->curbe.last_buf)
286 _mesa_free(brw->curbe.last_buf);
287
288 brw->curbe.last_buf = buf;
289 brw->curbe.last_bufsz = bufsz;
290
291 if (brw->curbe.curbe_bo != NULL &&
292 (brw->curbe.need_new_bo ||
293 brw->curbe.curbe_next_offset + bufsz > brw->curbe.curbe_bo->size))
294 {
295 dri_bo_unreference(brw->curbe.curbe_bo);
296 brw->curbe.curbe_bo = NULL;
297 }
298
299 if (brw->curbe.curbe_bo == NULL) {
300 /* Allocate a single page for CURBE entries for this batchbuffer.
301 * They're generally around 64b.
302 */
303 brw->curbe.curbe_bo = dri_bo_alloc(brw->intel.bufmgr, "CURBE",
304 4096, 1 << 6);
305 brw->curbe.curbe_next_offset = 0;
306 }
307
308 brw->curbe.curbe_offset = brw->curbe.curbe_next_offset;
309 brw->curbe.curbe_next_offset += bufsz;
310 brw->curbe.curbe_next_offset = ALIGN(brw->curbe.curbe_next_offset, 64);
311
312 /* Copy data to the buffer:
313 */
314 dri_bo_subdata(brw->curbe.curbe_bo, brw->curbe.curbe_offset, bufsz, buf);
315 }
316
317 brw_add_validated_bo(brw, brw->curbe.curbe_bo);
318
319 /* Because this provokes an action (ie copy the constants into the
320 * URB), it shouldn't be shortcircuited if identical to the
321 * previous time - because eg. the urb destination may have
322 * changed, or the urb contents different to last time.
323 *
324 * Note that the data referred to is actually copied internally,
325 * not just used in place according to passed pointer.
326 *
327 * It appears that the CS unit takes care of using each available
328 * URB entry (Const URB Entry == CURBE) in turn, and issuing
329 * flushes as necessary when doublebuffering of CURBEs isn't
330 * possible.
331 */
332 }
333
334
335 /**
336 * Vertex/fragment shader constants are stored in a pseudo 1D texture.
337 * This function updates the constants in that buffer.
338 */
339 static void
340 update_texture_constant_buffer(struct brw_context *brw)
341 {
342 struct brw_fragment_program *fp =
343 (struct brw_fragment_program *) brw->fragment_program;
344 const struct gl_program_parameter_list *params = fp->program.Base.Parameters;
345 const int size = params->NumParameters * 4 * sizeof(GLfloat);
346
347 assert(fp->const_buffer);
348 assert(fp->const_buffer->size >= size);
349
350 /* copy constants into the buffer */
351 if (size > 0) {
352 GLubyte *map;
353 dri_bo_map(fp->const_buffer, GL_TRUE);
354 map = fp->const_buffer->virtual;
355 memcpy(map, params->ParameterValues, size);
356 dri_bo_unmap(fp->const_buffer);
357 }
358 }
359
360
361 static void emit_constant_buffer(struct brw_context *brw)
362 {
363 struct intel_context *intel = &brw->intel;
364 GLuint sz = brw->curbe.total_size;
365
366 update_texture_constant_buffer(brw);
367
368 BEGIN_BATCH(2, IGNORE_CLIPRECTS);
369 if (sz == 0) {
370 OUT_BATCH((CMD_CONST_BUFFER << 16) | (2 - 2));
371 OUT_BATCH(0);
372 } else {
373 OUT_BATCH((CMD_CONST_BUFFER << 16) | (1 << 8) | (2 - 2));
374 OUT_RELOC(brw->curbe.curbe_bo,
375 I915_GEM_DOMAIN_INSTRUCTION, 0,
376 (sz - 1) + brw->curbe.curbe_offset);
377 }
378 ADVANCE_BATCH();
379 }
380
381 /* This tracked state is unique in that the state it monitors varies
382 * dynamically depending on the parameters tracked by the fragment and
383 * vertex programs. This is the template used as a starting point,
384 * each context will maintain a copy of this internally and update as
385 * required.
386 */
387 const struct brw_tracked_state brw_constant_buffer = {
388 .dirty = {
389 .mesa = (_NEW_TRANSFORM|_NEW_PROJECTION), /* plus fp and vp flags */
390 .brw = (BRW_NEW_FRAGMENT_PROGRAM |
391 BRW_NEW_VERTEX_PROGRAM |
392 BRW_NEW_URB_FENCE | /* Implicit - hardware requires this, not used above */
393 BRW_NEW_PSP | /* Implicit - hardware requires this, not used above */
394 BRW_NEW_CURBE_OFFSETS |
395 BRW_NEW_BATCH),
396 .cache = (CACHE_NEW_WM_PROG)
397 },
398 .prepare = prepare_constant_buffer,
399 .emit = emit_constant_buffer,
400 };
401