i965: fix fetching constants from constant buffer in glsl path
[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 /**
49 * Partition the CURBE between the various users of constant values:
50 * Note that vertex and fragment shaders can now fetch constants out
51 * of constant buffers. We no longer allocatea block of the GRF for
52 * constants. That greatly reduces the demand for space in the CURBE.
53 * Some of the comments within are dated...
54 */
55 static void calculate_curbe_offsets( struct brw_context *brw )
56 {
57 GLcontext *ctx = &brw->intel.ctx;
58 /* CACHE_NEW_WM_PROG */
59 const GLuint nr_fp_regs = (brw->wm.prog_data->nr_params + 15) / 16;
60
61 /* BRW_NEW_VERTEX_PROGRAM */
62 const GLuint nr_vp_regs = (brw->vs.prog_data->nr_params + 15) / 16;
63 GLuint nr_clip_regs = 0;
64 GLuint total_regs;
65
66 /* _NEW_TRANSFORM */
67 if (ctx->Transform.ClipPlanesEnabled) {
68 GLuint nr_planes = 6 + brw_count_bits(ctx->Transform.ClipPlanesEnabled);
69 nr_clip_regs = (nr_planes * 4 + 15) / 16;
70 }
71
72
73 total_regs = nr_fp_regs + nr_vp_regs + nr_clip_regs;
74
75 /* This can happen - what to do? Probably rather than falling
76 * back, the best thing to do is emit programs which code the
77 * constants as immediate values. Could do this either as a static
78 * cap on WM and VS, or adaptively.
79 *
80 * Unfortunately, this is currently dependent on the results of the
81 * program generation process (in the case of wm), so this would
82 * introduce the need to re-generate programs in the event of a
83 * curbe allocation failure.
84 */
85 /* Max size is 32 - just large enough to
86 * hold the 128 parameters allowed by
87 * the fragment and vertex program
88 * api's. It's not clear what happens
89 * when both VP and FP want to use 128
90 * parameters, though.
91 */
92 assert(total_regs <= 32);
93
94 /* Lazy resize:
95 */
96 if (nr_fp_regs > brw->curbe.wm_size ||
97 nr_vp_regs > brw->curbe.vs_size ||
98 nr_clip_regs != brw->curbe.clip_size ||
99 (total_regs < brw->curbe.total_size / 4 &&
100 brw->curbe.total_size > 16)) {
101
102 GLuint reg = 0;
103
104 /* Calculate a new layout:
105 */
106 reg = 0;
107 brw->curbe.wm_start = reg;
108 brw->curbe.wm_size = nr_fp_regs; reg += nr_fp_regs;
109 brw->curbe.clip_start = reg;
110 brw->curbe.clip_size = nr_clip_regs; reg += nr_clip_regs;
111 brw->curbe.vs_start = reg;
112 brw->curbe.vs_size = nr_vp_regs; reg += nr_vp_regs;
113 brw->curbe.total_size = reg;
114
115 if (0)
116 _mesa_printf("curbe wm %d+%d clip %d+%d vs %d+%d\n",
117 brw->curbe.wm_start,
118 brw->curbe.wm_size,
119 brw->curbe.clip_start,
120 brw->curbe.clip_size,
121 brw->curbe.vs_start,
122 brw->curbe.vs_size );
123
124 brw->state.dirty.brw |= BRW_NEW_CURBE_OFFSETS;
125 }
126 }
127
128
129 const struct brw_tracked_state brw_curbe_offsets = {
130 .dirty = {
131 .mesa = _NEW_TRANSFORM,
132 .brw = BRW_NEW_VERTEX_PROGRAM,
133 .cache = CACHE_NEW_WM_PROG
134 },
135 .prepare = calculate_curbe_offsets
136 };
137
138
139
140
141 /* Define the number of curbes within CS's urb allocation. Multiple
142 * urb entries -> multiple curbes. These will be used by
143 * fixed-function hardware in a double-buffering scheme to avoid a
144 * pipeline stall each time the contents of the curbe is changed.
145 */
146 void brw_upload_cs_urb_state(struct brw_context *brw)
147 {
148 struct brw_cs_urb_state cs_urb;
149 memset(&cs_urb, 0, sizeof(cs_urb));
150
151 /* It appears that this is the state packet for the CS unit, ie. the
152 * urb entries detailed here are housed in the CS range from the
153 * URB_FENCE command.
154 */
155 cs_urb.header.opcode = CMD_CS_URB_STATE;
156 cs_urb.header.length = sizeof(cs_urb)/4 - 2;
157
158 /* BRW_NEW_URB_FENCE */
159 cs_urb.bits0.nr_urb_entries = brw->urb.nr_cs_entries;
160 cs_urb.bits0.urb_entry_size = brw->urb.csize - 1;
161
162 assert(brw->urb.nr_cs_entries);
163 BRW_CACHED_BATCH_STRUCT(brw, &cs_urb);
164 }
165
166 static GLfloat fixed_plane[6][4] = {
167 { 0, 0, -1, 1 },
168 { 0, 0, 1, 1 },
169 { 0, -1, 0, 1 },
170 { 0, 1, 0, 1 },
171 {-1, 0, 0, 1 },
172 { 1, 0, 0, 1 }
173 };
174
175 /* Upload a new set of constants. Too much variability to go into the
176 * cache mechanism, but maybe would benefit from a comparison against
177 * the current uploaded set of constants.
178 */
179 static void prepare_constant_buffer(struct brw_context *brw)
180 {
181 GLcontext *ctx = &brw->intel.ctx;
182 const struct brw_vertex_program *vp =
183 brw_vertex_program_const(brw->vertex_program);
184 const struct brw_fragment_program *fp =
185 brw_fragment_program_const(brw->fragment_program);
186 const GLuint sz = brw->curbe.total_size;
187 const GLuint bufsz = sz * 16 * sizeof(GLfloat);
188 GLfloat *buf;
189 GLuint i;
190
191 /* Update our own dependency flags. This works because this
192 * function will also be called whenever fp or vp changes.
193 */
194 brw->curbe.tracked_state.dirty.mesa = (_NEW_TRANSFORM|_NEW_PROJECTION);
195 brw->curbe.tracked_state.dirty.mesa |= vp->program.Base.Parameters->StateFlags;
196 brw->curbe.tracked_state.dirty.mesa |= fp->program.Base.Parameters->StateFlags;
197
198 if (sz == 0) {
199 if (brw->curbe.last_buf) {
200 free(brw->curbe.last_buf);
201 brw->curbe.last_buf = NULL;
202 brw->curbe.last_bufsz = 0;
203 }
204 return;
205 }
206
207 buf = (GLfloat *) _mesa_calloc(bufsz);
208
209 /* fragment shader constants */
210 if (brw->curbe.wm_size) {
211 GLuint offset = brw->curbe.wm_start * 16;
212
213 _mesa_load_state_parameters(ctx, fp->program.Base.Parameters);
214
215 /* copy float constants */
216 for (i = 0; i < brw->wm.prog_data->nr_params; i++)
217 buf[offset + i] = *brw->wm.prog_data->param[i];
218 }
219
220
221 /* The clipplanes are actually delivered to both CLIP and VS units.
222 * VS uses them to calculate the outcode bitmasks.
223 */
224 if (brw->curbe.clip_size) {
225 GLuint offset = brw->curbe.clip_start * 16;
226 GLuint j;
227
228 /* If any planes are going this way, send them all this way:
229 */
230 for (i = 0; i < 6; i++) {
231 buf[offset + i * 4 + 0] = fixed_plane[i][0];
232 buf[offset + i * 4 + 1] = fixed_plane[i][1];
233 buf[offset + i * 4 + 2] = fixed_plane[i][2];
234 buf[offset + i * 4 + 3] = fixed_plane[i][3];
235 }
236
237 /* Clip planes: _NEW_TRANSFORM plus _NEW_PROJECTION to get to
238 * clip-space:
239 */
240 assert(MAX_CLIP_PLANES == 6);
241 for (j = 0; j < MAX_CLIP_PLANES; j++) {
242 if (ctx->Transform.ClipPlanesEnabled & (1<<j)) {
243 buf[offset + i * 4 + 0] = ctx->Transform._ClipUserPlane[j][0];
244 buf[offset + i * 4 + 1] = ctx->Transform._ClipUserPlane[j][1];
245 buf[offset + i * 4 + 2] = ctx->Transform._ClipUserPlane[j][2];
246 buf[offset + i * 4 + 3] = ctx->Transform._ClipUserPlane[j][3];
247 i++;
248 }
249 }
250 }
251
252 /* vertex shader constants */
253 if (brw->curbe.vs_size) {
254 GLuint offset = brw->curbe.vs_start * 16;
255 GLuint nr = brw->vs.prog_data->nr_params / 4;
256
257 _mesa_load_state_parameters(ctx, vp->program.Base.Parameters);
258
259 /* XXX just use a memcpy here */
260 for (i = 0; i < nr; i++) {
261 const GLfloat *value = vp->program.Base.Parameters->ParameterValues[i];
262 buf[offset + i * 4 + 0] = value[0];
263 buf[offset + i * 4 + 1] = value[1];
264 buf[offset + i * 4 + 2] = value[2];
265 buf[offset + i * 4 + 3] = value[3];
266 }
267 }
268
269 if (0) {
270 for (i = 0; i < sz*16; i+=4)
271 _mesa_printf("curbe %d.%d: %f %f %f %f\n", i/8, i&4,
272 buf[i+0], buf[i+1], buf[i+2], buf[i+3]);
273
274 _mesa_printf("last_buf %p buf %p sz %d/%d cmp %d\n",
275 brw->curbe.last_buf, buf,
276 bufsz, brw->curbe.last_bufsz,
277 brw->curbe.last_buf ? memcmp(buf, brw->curbe.last_buf, bufsz) : -1);
278 }
279
280 if (brw->curbe.curbe_bo != NULL &&
281 brw->curbe.last_buf &&
282 bufsz == brw->curbe.last_bufsz &&
283 memcmp(buf, brw->curbe.last_buf, bufsz) == 0) {
284 /* constants have not changed */
285 _mesa_free(buf);
286 }
287 else {
288 /* constants have changed */
289 if (brw->curbe.last_buf)
290 _mesa_free(brw->curbe.last_buf);
291
292 brw->curbe.last_buf = buf;
293 brw->curbe.last_bufsz = bufsz;
294
295 if (brw->curbe.curbe_bo != NULL &&
296 (brw->curbe.need_new_bo ||
297 brw->curbe.curbe_next_offset + bufsz > brw->curbe.curbe_bo->size))
298 {
299 dri_bo_unreference(brw->curbe.curbe_bo);
300 brw->curbe.curbe_bo = NULL;
301 }
302
303 if (brw->curbe.curbe_bo == NULL) {
304 /* Allocate a single page for CURBE entries for this batchbuffer.
305 * They're generally around 64b.
306 */
307 brw->curbe.curbe_bo = dri_bo_alloc(brw->intel.bufmgr, "CURBE",
308 4096, 1 << 6);
309 brw->curbe.curbe_next_offset = 0;
310 }
311
312 brw->curbe.curbe_offset = brw->curbe.curbe_next_offset;
313 brw->curbe.curbe_next_offset += bufsz;
314 brw->curbe.curbe_next_offset = ALIGN(brw->curbe.curbe_next_offset, 64);
315
316 /* Copy data to the buffer:
317 */
318 dri_bo_subdata(brw->curbe.curbe_bo, brw->curbe.curbe_offset, bufsz, buf);
319 }
320
321 brw_add_validated_bo(brw, brw->curbe.curbe_bo);
322
323 /* Because this provokes an action (ie copy the constants into the
324 * URB), it shouldn't be shortcircuited if identical to the
325 * previous time - because eg. the urb destination may have
326 * changed, or the urb contents different to last time.
327 *
328 * Note that the data referred to is actually copied internally,
329 * not just used in place according to passed pointer.
330 *
331 * It appears that the CS unit takes care of using each available
332 * URB entry (Const URB Entry == CURBE) in turn, and issuing
333 * flushes as necessary when doublebuffering of CURBEs isn't
334 * possible.
335 */
336 }
337
338
339 /**
340 * Copy Mesa program parameters into given constant buffer.
341 */
342 static void
343 update_constant_buffer(struct brw_context *brw,
344 const struct gl_program_parameter_list *params,
345 dri_bo *const_buffer)
346 {
347 struct intel_context *intel = &brw->intel;
348 const int size = params->NumParameters * 4 * sizeof(GLfloat);
349
350 /* copy Mesa program constants into the buffer */
351 if (const_buffer && size > 0) {
352
353 assert(const_buffer);
354 assert(const_buffer->size >= size);
355
356 if (intel->intelScreen->kernel_exec_fencing) {
357 drm_intel_gem_bo_map_gtt(const_buffer);
358 memcpy(const_buffer->virtual, params->ParameterValues, size);
359 drm_intel_gem_bo_unmap_gtt(const_buffer);
360 }
361 else {
362 dri_bo_subdata(const_buffer, 0, size, params->ParameterValues);
363 }
364
365 if (0) {
366 int i;
367 for (i = 0; i < params->NumParameters; i++) {
368 float *p = params->ParameterValues[i];
369 printf("%d: %f %f %f %f\n", i, p[0], p[1], p[2], p[3]);
370 }
371 }
372 }
373 }
374
375
376 /** Copy current vertex program's parameters into the constant buffer */
377 static void
378 update_vertex_constant_buffer(struct brw_context *brw)
379 {
380 struct brw_vertex_program *vp =
381 (struct brw_vertex_program *) brw->vertex_program;
382 if (0) {
383 printf("update VS constants in buffer %p\n", vp->const_buffer);
384 printf("program %u\n", vp->program.Base.Id);
385 }
386 if (vp->use_const_buffer)
387 update_constant_buffer(brw, vp->program.Base.Parameters, vp->const_buffer);
388 }
389
390
391 /** Copy current fragment program's parameters into the constant buffer */
392 static void
393 update_fragment_constant_buffer(struct brw_context *brw)
394 {
395 struct brw_fragment_program *fp =
396 (struct brw_fragment_program *) brw->fragment_program;
397 if (fp->use_const_buffer)
398 update_constant_buffer(brw, fp->program.Base.Parameters, fp->const_buffer);
399 }
400
401
402 static void emit_constant_buffer(struct brw_context *brw)
403 {
404 struct intel_context *intel = &brw->intel;
405 GLuint sz = brw->curbe.total_size;
406
407 update_vertex_constant_buffer(brw);
408 update_fragment_constant_buffer(brw);
409
410 BEGIN_BATCH(2, IGNORE_CLIPRECTS);
411 if (sz == 0) {
412 OUT_BATCH((CMD_CONST_BUFFER << 16) | (2 - 2));
413 OUT_BATCH(0);
414 } else {
415 OUT_BATCH((CMD_CONST_BUFFER << 16) | (1 << 8) | (2 - 2));
416 OUT_RELOC(brw->curbe.curbe_bo,
417 I915_GEM_DOMAIN_INSTRUCTION, 0,
418 (sz - 1) + brw->curbe.curbe_offset);
419 }
420 ADVANCE_BATCH();
421 }
422
423 /* This tracked state is unique in that the state it monitors varies
424 * dynamically depending on the parameters tracked by the fragment and
425 * vertex programs. This is the template used as a starting point,
426 * each context will maintain a copy of this internally and update as
427 * required.
428 */
429 const struct brw_tracked_state brw_constant_buffer = {
430 .dirty = {
431 .mesa = (_NEW_TRANSFORM|_NEW_PROJECTION), /* plus fp and vp flags */
432 .brw = (BRW_NEW_FRAGMENT_PROGRAM |
433 BRW_NEW_VERTEX_PROGRAM |
434 BRW_NEW_URB_FENCE | /* Implicit - hardware requires this, not used above */
435 BRW_NEW_PSP | /* Implicit - hardware requires this, not used above */
436 BRW_NEW_CURBE_OFFSETS |
437 BRW_NEW_BATCH),
438 .cache = (CACHE_NEW_WM_PROG)
439 },
440 .prepare = prepare_constant_buffer,
441 .emit = emit_constant_buffer,
442 };
443