OSDN Git Service

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