Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / i965 / brw_gs.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 "brw_batchbuffer.h"
33
34 #include "brw_defines.h"
35 #include "brw_context.h"
36 #include "brw_eu.h"
37 #include "brw_state.h"
38 #include "brw_gs.h"
39
40
41
42 static enum pipe_error compile_gs_prog( struct brw_context *brw,
43                                         struct brw_gs_prog_key *key,
44                                         struct brw_winsys_buffer **bo_out )
45 {
46    struct brw_gs_compile c;
47    enum pipe_error ret;
48    const GLuint *program;
49    GLuint program_size;
50
51    memset(&c, 0, sizeof(c));
52    
53    c.key = *key;
54    c.need_ff_sync = brw->gen == 5;
55    /* Need to locate the two positions present in vertex + header.
56     * These are currently hardcoded:
57     */
58    c.nr_attrs = c.key.nr_attrs;
59
60    if (brw->gen == 5)
61       c.nr_regs = (c.nr_attrs + 1) / 2 + 3;  /* are vertices packed, or reg-aligned? */
62    else
63       c.nr_regs = (c.nr_attrs + 1) / 2 + 1;  /* are vertices packed, or reg-aligned? */
64
65    c.nr_bytes = c.nr_regs * REG_SIZE;
66
67    
68    /* Begin the compilation:
69     */
70    brw_init_compile(brw, &c.func);
71
72    c.func.single_program_flow = 1;
73
74    /* For some reason the thread is spawned with only 4 channels
75     * unmasked.  
76     */
77    brw_set_mask_control(&c.func, BRW_MASK_DISABLE);
78
79
80    /* Note that primitives which don't require a GS program have
81     * already been weeded out by this stage:
82     */
83    switch (key->primitive) {
84    case PIPE_PRIM_QUADS:
85       brw_gs_quads( &c ); 
86       break;
87    case PIPE_PRIM_QUAD_STRIP:
88       brw_gs_quad_strip( &c );
89       break;
90    case PIPE_PRIM_LINE_LOOP:
91       brw_gs_lines( &c );
92       break;
93    case PIPE_PRIM_LINES:
94       if (key->hint_gs_always)
95          brw_gs_lines( &c );
96       else {
97          return PIPE_OK;
98       }
99       break;
100    case PIPE_PRIM_TRIANGLES:
101       if (key->hint_gs_always)
102          brw_gs_tris( &c );
103       else {
104          return PIPE_OK;
105       }
106       break;
107    case PIPE_PRIM_POINTS:
108       if (key->hint_gs_always)
109          brw_gs_points( &c );
110       else {
111          return PIPE_OK;
112       }
113       break;
114    default:
115       assert(0);
116       return PIPE_ERROR_BAD_INPUT;
117    }
118
119    /* get the program
120     */
121    ret = brw_get_program(&c.func, &program, &program_size);
122    if (ret)
123       return ret;
124
125    /* Upload
126     */
127    ret = brw_upload_cache( &brw->cache, BRW_GS_PROG,
128                            &c.key, sizeof(c.key),
129                            NULL, 0,
130                            program, program_size,
131                            &c.prog_data,
132                            &brw->gs.prog_data,
133                            bo_out );
134    if (ret)
135       return ret;
136
137    return PIPE_OK;
138 }
139
140 static const unsigned gs_prim[PIPE_PRIM_MAX] = {  
141    PIPE_PRIM_POINTS,
142    PIPE_PRIM_LINES,
143    PIPE_PRIM_LINE_LOOP,
144    PIPE_PRIM_LINES,
145    PIPE_PRIM_TRIANGLES,
146    PIPE_PRIM_TRIANGLES,
147    PIPE_PRIM_TRIANGLES,
148    PIPE_PRIM_QUADS,
149    PIPE_PRIM_QUAD_STRIP,
150    PIPE_PRIM_TRIANGLES
151 };
152
153 static void populate_key( struct brw_context *brw,
154                           struct brw_gs_prog_key *key )
155 {
156    const struct brw_fs_signature *sig = &brw->curr.fragment_shader->signature;
157
158    memset(key, 0, sizeof(*key));
159
160    /* PIPE_NEW_FRAGMENT_SIGNATURE */
161    key->nr_attrs = sig->nr_inputs + 1;
162
163    /* BRW_NEW_PRIMITIVE */
164    key->primitive = gs_prim[brw->primitive];
165
166    key->hint_gs_always = 0;     /* debug code? */
167
168    key->need_gs_prog = (key->hint_gs_always ||
169                         brw->primitive == PIPE_PRIM_QUADS ||
170                         brw->primitive == PIPE_PRIM_QUAD_STRIP ||
171                         brw->primitive == PIPE_PRIM_LINE_LOOP);
172 }
173
174 /* Calculate interpolants for triangle and line rasterization.
175  */
176 static int prepare_gs_prog(struct brw_context *brw)
177 {
178    struct brw_gs_prog_key key;
179    enum pipe_error ret;
180
181    /* Populate the key:
182     */
183    populate_key(brw, &key);
184
185    if (brw->gs.prog_active != key.need_gs_prog) {
186       brw->state.dirty.cache |= CACHE_NEW_GS_PROG;
187       brw->gs.prog_active = key.need_gs_prog;
188    }
189
190    if (!brw->gs.prog_active)
191       return PIPE_OK;
192
193    if (brw_search_cache(&brw->cache, BRW_GS_PROG,
194                         &key, sizeof(key),
195                         NULL, 0,
196                         &brw->gs.prog_data,
197                         &brw->gs.prog_bo))
198       return PIPE_OK;
199
200    ret = compile_gs_prog( brw, &key, &brw->gs.prog_bo );
201    if (ret)
202       return ret;
203
204    return PIPE_OK;
205 }
206
207
208 const struct brw_tracked_state brw_gs_prog = {
209    .dirty = {
210       .mesa  = PIPE_NEW_FRAGMENT_SIGNATURE,
211       .brw   = BRW_NEW_PRIMITIVE,
212       .cache = 0,
213    },
214    .prepare = prepare_gs_prog
215 };