Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / 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 #include "util/u_memory.h"
33 #include "util/u_math.h"
34
35 #include "brw_batchbuffer.h"
36 #include "brw_context.h"
37 #include "brw_defines.h"
38 #include "brw_state.h"
39 #include "brw_debug.h"
40
41
42 /**
43  * Partition the CURBE between the various users of constant values:
44  * Note that vertex and fragment shaders can now fetch constants out
45  * of constant buffers.  We no longer allocatea block of the GRF for
46  * constants.  That greatly reduces the demand for space in the CURBE.
47  * Some of the comments within are dated...
48  */
49 static int calculate_curbe_offsets( struct brw_context *brw )
50 {
51    /* CACHE_NEW_WM_PROG */
52    const GLuint nr_fp_regs = brw->wm.prog_data->curb_read_length;
53    
54    /* BRW_NEW_VERTEX_PROGRAM */
55    const GLuint nr_vp_regs = brw->vs.prog_data->curb_read_length;
56    GLuint nr_clip_regs = 0;
57    GLuint total_regs;
58
59    /* PIPE_NEW_CLIP */
60    if (brw->curr.ucp.nr) {
61       GLuint nr_planes = 6 + brw->curr.ucp.nr;
62       nr_clip_regs = (nr_planes * 4 + 15) / 16;
63    }
64
65
66    total_regs = nr_fp_regs + nr_vp_regs + nr_clip_regs;
67
68    /* When this is > 32, want to use a true constant buffer to hold
69     * the extra constants.
70     */
71    assert(total_regs <= 32);
72
73    /* Lazy resize:
74     */
75    if (nr_fp_regs > brw->curbe.wm_size ||
76        nr_vp_regs > brw->curbe.vs_size ||
77        nr_clip_regs != brw->curbe.clip_size ||
78        (total_regs < brw->curbe.total_size / 4 &&
79         brw->curbe.total_size > 16)) {
80
81       GLuint reg = 0;
82
83       /* Calculate a new layout: 
84        */
85       reg = 0;
86       brw->curbe.wm_start = reg;
87       brw->curbe.wm_size = nr_fp_regs; reg += nr_fp_regs;
88       brw->curbe.clip_start = reg;
89       brw->curbe.clip_size = nr_clip_regs; reg += nr_clip_regs;
90       brw->curbe.vs_start = reg;
91       brw->curbe.vs_size = nr_vp_regs; reg += nr_vp_regs;
92       brw->curbe.total_size = reg;
93
94       if (BRW_DEBUG & DEBUG_CURBE)
95          debug_printf("curbe wm %d+%d clip %d+%d vs %d+%d\n",
96                       brw->curbe.wm_start,
97                       brw->curbe.wm_size,
98                       brw->curbe.clip_start,
99                       brw->curbe.clip_size,
100                       brw->curbe.vs_start,
101                       brw->curbe.vs_size );
102
103       brw->state.dirty.brw |= BRW_NEW_CURBE_OFFSETS;
104    }
105
106    return 0;
107 }
108
109
110 const struct brw_tracked_state brw_curbe_offsets = {
111    .dirty = {
112       .mesa = PIPE_NEW_CLIP,
113       .brw  = BRW_NEW_VERTEX_PROGRAM,
114       .cache = CACHE_NEW_WM_PROG
115    },
116    .prepare = calculate_curbe_offsets
117 };
118
119
120
121
122 /* Define the number of curbes within CS's urb allocation.  Multiple
123  * urb entries -> multiple curbes.  These will be used by
124  * fixed-function hardware in a double-buffering scheme to avoid a
125  * pipeline stall each time the contents of the curbe is changed.
126  */
127 int brw_upload_cs_urb_state(struct brw_context *brw)
128 {
129    struct brw_cs_urb_state cs_urb;
130    memset(&cs_urb, 0, sizeof(cs_urb));
131
132    /* It appears that this is the state packet for the CS unit, ie. the
133     * urb entries detailed here are housed in the CS range from the
134     * URB_FENCE command.
135     */
136    cs_urb.header.opcode = CMD_CS_URB_STATE;
137    cs_urb.header.length = sizeof(cs_urb)/4 - 2;
138
139    /* BRW_NEW_URB_FENCE */
140    cs_urb.bits0.nr_urb_entries = brw->urb.nr_cs_entries;
141    cs_urb.bits0.urb_entry_size = brw->urb.csize - 1;
142
143    assert(brw->urb.nr_cs_entries);
144    BRW_CACHED_BATCH_STRUCT(brw, &cs_urb);
145    return 0;
146 }
147
148 static GLfloat fixed_plane[6][4] = {
149    { 0,    0,   -1, 1 },
150    { 0,    0,    1, 1 },
151    { 0,   -1,    0, 1 },
152    { 0,    1,    0, 1 },
153    {-1,    0,    0, 1 },
154    { 1,    0,    0, 1 }
155 };
156
157 /* Upload a new set of constants.  Too much variability to go into the
158  * cache mechanism, but maybe would benefit from a comparison against
159  * the current uploaded set of constants.
160  */
161 static enum pipe_error prepare_curbe_buffer(struct brw_context *brw)
162 {
163    const GLuint sz = brw->curbe.total_size;
164    const GLuint bufsz = sz * 16 * sizeof(GLfloat);
165    enum pipe_error ret;
166    GLfloat *buf;
167    GLuint i;
168
169    if (sz == 0) {
170       if (brw->curbe.last_buf) {
171          FREE(brw->curbe.last_buf);
172          brw->curbe.last_buf = NULL;
173          brw->curbe.last_bufsz  = 0;
174       }
175       return 0;
176    }
177
178    buf = (GLfloat *) CALLOC(bufsz, 1);
179
180    /* fragment shader constants */
181    if (brw->curbe.wm_size) {
182       const struct brw_fragment_shader *fs = brw->curr.fragment_shader;
183       GLuint offset = brw->curbe.wm_start * 16;
184       GLuint nr_immediate, nr_const;
185
186       nr_immediate = fs->immediates.nr;
187       if (nr_immediate) {
188          memcpy(&buf[offset], 
189                 fs->immediates.data,
190                 nr_immediate * 4 * sizeof(float));
191
192          offset += nr_immediate * 4;
193       }
194
195       nr_const = fs->info.file_max[TGSI_FILE_CONSTANT] + 1;
196 /*      nr_const = brw->wm.prog_data->nr_params; */
197       if (nr_const) {
198          pipe_buffer_read( &brw->base,
199                            brw->curr.fragment_constants,
200                            0,
201                            nr_const * 4 * sizeof(float),
202                            &buf[offset]);
203       }
204    }
205
206
207    /* The clipplanes are actually delivered to both CLIP and VS units.
208     * VS uses them to calculate the outcode bitmasks.
209     */
210    if (brw->curbe.clip_size) {
211       GLuint offset = brw->curbe.clip_start * 16;
212       GLuint j;
213
214       /* If any planes are going this way, send them all this way:
215        */
216       for (i = 0; i < 6; i++) {
217          buf[offset + i * 4 + 0] = fixed_plane[i][0];
218          buf[offset + i * 4 + 1] = fixed_plane[i][1];
219          buf[offset + i * 4 + 2] = fixed_plane[i][2];
220          buf[offset + i * 4 + 3] = fixed_plane[i][3];
221       }
222
223       /* Clip planes:
224        */
225       assert(brw->curr.ucp.nr <= 6);
226       for (j = 0; j < brw->curr.ucp.nr; j++) {
227          buf[offset + i * 4 + 0] = brw->curr.ucp.ucp[j][0];
228          buf[offset + i * 4 + 1] = brw->curr.ucp.ucp[j][1];
229          buf[offset + i * 4 + 2] = brw->curr.ucp.ucp[j][2];
230          buf[offset + i * 4 + 3] = brw->curr.ucp.ucp[j][3];
231          i++;
232       }
233    }
234
235    /* vertex shader constants */
236    if (brw->curbe.vs_size) {
237       GLuint offset = brw->curbe.vs_start * 16;
238       const struct brw_vertex_shader *vs = brw->curr.vertex_shader;
239       GLuint nr_immediate, nr_const;
240
241       nr_immediate = vs->immediates.nr;
242       if (nr_immediate) {
243          memcpy(&buf[offset], 
244                 vs->immediates.data,
245                 nr_immediate * 4 * sizeof(float));
246
247          offset += nr_immediate * 4;
248       }
249
250       nr_const = vs->info.file_max[TGSI_FILE_CONSTANT] + 1;
251       if (nr_const) {
252          /* XXX: note that constant buffers are currently *already* in
253           * buffer objects.  If we want to keep on putting them into the
254           * curbe, makes sense to treat constbuf's specially with malloc.
255           */
256          
257          /* XXX: what if user's constant buffer is too small?
258           */
259          pipe_buffer_read(&brw->base,
260                           brw->curr.vertex_constants,
261                           0,
262                           nr_const * 4 * sizeof(float),
263                           &buf[offset]);
264       }
265    }
266
267    if (BRW_DEBUG & DEBUG_CURBE) {
268       for (i = 0; i < sz*16; i+=4) 
269          debug_printf("curbe %d.%d: %f %f %f %f\n", i/8, i&4,
270                       buf[i+0], buf[i+1], buf[i+2], buf[i+3]);
271
272       debug_printf("last_buf %p buf %p sz %d/%d cmp %d\n",
273                    (void *)brw->curbe.last_buf, (void *)buf,
274                    bufsz, brw->curbe.last_bufsz,
275                    brw->curbe.last_buf ? memcmp(buf, brw->curbe.last_buf, bufsz) : -1);
276    }
277
278    if (brw->curbe.curbe_bo != NULL &&
279        brw->curbe.last_buf &&
280        bufsz == brw->curbe.last_bufsz &&
281        memcmp(buf, brw->curbe.last_buf, bufsz) == 0) {
282       /* constants have not changed */
283       FREE(buf);
284    } 
285    else {
286       /* constants have changed */
287       FREE(brw->curbe.last_buf);
288
289       brw->curbe.last_buf = buf;
290       brw->curbe.last_bufsz = bufsz;
291
292       if (brw->curbe.curbe_bo != NULL &&
293           (brw->curbe.need_new_bo ||
294            brw->curbe.curbe_next_offset + bufsz > brw->curbe.curbe_bo->size))
295       {
296          bo_reference(&brw->curbe.curbe_bo, NULL);
297       }
298
299       if (brw->curbe.curbe_bo == NULL) {
300          /* Allocate a single page for CURBE entries for this
301           * batchbuffer.  They're generally around 64b.  We will
302           * discard the curbe buffer after the batch is flushed to
303           * avoid synchronous updates.
304           */
305          ret = brw->sws->bo_alloc(brw->sws, 
306                                   BRW_BUFFER_TYPE_CURBE,
307                                   4096, 1 << 6,
308                                   &brw->curbe.curbe_bo);
309          if (ret)
310             return ret;
311
312          brw->curbe.curbe_next_offset = 0;
313       }
314
315       brw->curbe.curbe_offset = brw->curbe.curbe_next_offset;
316       brw->curbe.curbe_next_offset += bufsz;
317       brw->curbe.curbe_next_offset = align(brw->curbe.curbe_next_offset, 64);
318
319       /* Copy data to the buffer:
320        */
321       brw->sws->bo_subdata(brw->curbe.curbe_bo,
322                            BRW_DATA_CONSTANT_BUFFER,
323                            brw->curbe.curbe_offset,
324                            bufsz,
325                            buf,
326                            NULL, 0);
327    }
328
329    brw_add_validated_bo(brw, brw->curbe.curbe_bo);
330
331    /* Because this provokes an action (ie copy the constants into the
332     * URB), it shouldn't be shortcircuited if identical to the
333     * previous time - because eg. the urb destination may have
334     * changed, or the urb contents different to last time.
335     *
336     * Note that the data referred to is actually copied internally,
337     * not just used in place according to passed pointer.
338     *
339     * It appears that the CS unit takes care of using each available
340     * URB entry (Const URB Entry == CURBE) in turn, and issuing
341     * flushes as necessary when doublebuffering of CURBEs isn't
342     * possible.
343     */
344
345    return 0;
346 }
347
348 static enum pipe_error emit_curbe_buffer(struct brw_context *brw)
349 {
350    GLuint sz = brw->curbe.total_size;
351
352    BEGIN_BATCH(2, IGNORE_CLIPRECTS);
353    if (sz == 0) {
354       OUT_BATCH((CMD_CONST_BUFFER << 16) | (2 - 2));
355       OUT_BATCH(0);
356    } else {
357       OUT_BATCH((CMD_CONST_BUFFER << 16) | (1 << 8) | (2 - 2));
358       OUT_RELOC(brw->curbe.curbe_bo,
359                 BRW_USAGE_STATE,
360                 (sz - 1) + brw->curbe.curbe_offset);
361    }
362    ADVANCE_BATCH();
363    return 0;
364 }
365
366 const struct brw_tracked_state brw_curbe_buffer = {
367    .dirty = {
368       .mesa = (PIPE_NEW_FRAGMENT_CONSTANTS |
369                PIPE_NEW_VERTEX_CONSTANTS |
370                PIPE_NEW_CLIP),
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                BRW_NEW_BATCH),
377       .cache = (CACHE_NEW_WM_PROG) 
378    },
379    .prepare = prepare_curbe_buffer,
380    .emit = emit_curbe_buffer,
381 };
382