Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / i965 / brw_sf.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 "pipe/p_state.h"
33
34 #include "brw_batchbuffer.h"
35 #include "brw_defines.h"
36 #include "brw_context.h"
37 #include "brw_pipe_rast.h"
38 #include "brw_eu.h"
39 #include "brw_sf.h"
40 #include "brw_state.h"
41
42 static enum pipe_error compile_sf_prog( struct brw_context *brw,
43                                         struct brw_sf_prog_key *key,
44                                         struct brw_winsys_buffer **bo_out )
45 {
46    enum pipe_error ret;
47    struct brw_sf_compile c;
48    const GLuint *program;
49    GLuint program_size;
50
51    memset(&c, 0, sizeof(c));
52
53    /* Begin the compilation:
54     */
55    brw_init_compile(brw, &c.func);
56
57    c.key = *key;
58    c.nr_attrs = c.key.nr_attrs;
59    c.nr_attr_regs = (c.nr_attrs+1)/2;
60    c.nr_setup_attrs = c.key.nr_attrs;
61    c.nr_setup_regs = (c.nr_setup_attrs+1)/2;
62
63    c.prog_data.urb_read_length = c.nr_attr_regs;
64    c.prog_data.urb_entry_size = c.nr_setup_regs * 2;
65
66    /* Special case when there are no attributes to setup.
67     *
68     * XXX: should be able to set nr_setup_attrs to nr_attrs-1 -- but
69     * breaks vp-tris.c
70     */
71    if (c.nr_attrs - 1 == 0) {
72       c.nr_verts = 0;
73       brw_emit_null_setup( &c );
74    }
75    else {
76       /* Which primitive?  Or all three? 
77        */
78       switch (key->primitive) {
79       case SF_TRIANGLES:
80          c.nr_verts = 3;
81          brw_emit_tri_setup( &c, GL_TRUE );
82          break;
83       case SF_LINES:
84          c.nr_verts = 2;
85          brw_emit_line_setup( &c, GL_TRUE );
86          break;
87       case SF_POINTS:
88          c.nr_verts = 1;
89          if (key->do_point_sprite)
90             brw_emit_point_sprite_setup( &c, GL_TRUE );
91          else
92             brw_emit_point_setup( &c, GL_TRUE );
93          break;
94       case SF_UNFILLED_TRIS:
95          c.nr_verts = 3;
96          brw_emit_anyprim_setup( &c );
97          break;
98       default:
99          assert(0);
100          return PIPE_ERROR_BAD_INPUT;
101       }
102    }
103
104    /* get the program
105     */
106    ret = brw_get_program(&c.func, &program, &program_size);
107    if (ret)
108       return ret;
109
110    /* Upload
111     */
112    ret = brw_upload_cache( &brw->cache, BRW_SF_PROG,
113                            &c.key, sizeof(c.key),
114                            NULL, 0,
115                            program, program_size,
116                            &c.prog_data,
117                            &brw->sf.prog_data,
118                            bo_out);
119    if (ret)
120       return ret;
121
122    return PIPE_OK;
123 }
124
125 /* Calculate interpolants for triangle and line rasterization.
126  */
127 static enum pipe_error upload_sf_prog(struct brw_context *brw)
128 {
129    const struct brw_fs_signature *sig = &brw->curr.fragment_shader->signature;
130    const struct pipe_rasterizer_state *rast = &brw->curr.rast->templ;
131    struct brw_sf_prog_key key;
132    enum pipe_error ret;
133    unsigned i;
134
135    memset(&key, 0, sizeof(key));
136
137    /* Populate the key, noting state dependencies:
138     */
139
140    /* XXX: Add one to account for the position input.
141     */
142    /* PIPE_NEW_FRAGMENT_SIGNATURE */
143    key.nr_attrs = sig->nr_inputs + 1;
144
145
146    /* XXX: why is position required to be linear?  why do we care
147     * about it at all?
148     */
149    key.linear_attrs = 1;        /* position -- but why? */
150
151    for (i = 0; i < sig->nr_inputs; i++) {
152       switch (sig->input[i].interp) {
153       case TGSI_INTERPOLATE_CONSTANT:
154          break;
155       case TGSI_INTERPOLATE_LINEAR:
156          key.linear_attrs |= 1 << (i+1);
157          break;
158       case TGSI_INTERPOLATE_PERSPECTIVE:
159          key.persp_attrs |= 1 << (i+1);
160          break;
161       }
162    }
163
164    /* BRW_NEW_REDUCED_PRIMITIVE */
165    switch (brw->reduced_primitive) {
166    case PIPE_PRIM_TRIANGLES: 
167       /* PIPE_NEW_RAST
168        */
169       if (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
170           rast->fill_back != PIPE_POLYGON_MODE_FILL)
171          key.primitive = SF_UNFILLED_TRIS;
172       else
173          key.primitive = SF_TRIANGLES;
174       break;
175    case PIPE_PRIM_LINES: 
176       key.primitive = SF_LINES; 
177       break;
178    case PIPE_PRIM_POINTS: 
179       key.primitive = SF_POINTS; 
180       break;
181    }
182
183    key.do_point_sprite = rast->sprite_coord_enable ? 1 : 0;
184    key.sprite_origin_lower_left = (rast->sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT);
185    key.point_coord_replace_attrs = rast->sprite_coord_enable;
186    key.do_flat_shading = rast->flatshade;
187    key.do_twoside_color = rast->light_twoside;
188
189    if (key.do_twoside_color) {
190       key.frontface_ccw = rast->front_ccw;
191    }
192
193    if (brw_search_cache(&brw->cache, BRW_SF_PROG,
194                         &key, sizeof(key),
195                         NULL, 0,
196                         &brw->sf.prog_data,
197                         &brw->sf.prog_bo))
198       return PIPE_OK;
199
200    ret = compile_sf_prog( brw, &key, &brw->sf.prog_bo );
201    if (ret)
202       return ret;
203
204    return PIPE_OK;
205 }
206
207
208 const struct brw_tracked_state brw_sf_prog = {
209    .dirty = {
210       .mesa  = (PIPE_NEW_RAST | PIPE_NEW_FRAGMENT_SIGNATURE),
211       .brw   = (BRW_NEW_REDUCED_PRIMITIVE),
212       .cache = 0
213    },
214    .prepare = upload_sf_prog
215 };
216