Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[profile/ivi/mesa.git] / src / gallium / drivers / svga / svga_swtnl_state.c
1 /**********************************************************
2  * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  **********************************************************/
25
26 #include "draw/draw_context.h"
27 #include "draw/draw_vbuf.h"
28 #include "pipe/p_inlines.h"
29 #include "pipe/p_state.h"
30 #include "util/u_memory.h"
31
32 #include "svga_context.h"
33 #include "svga_swtnl.h"
34 #include "svga_state.h"
35
36 #include "svga_swtnl_private.h"
37
38
39 #define SVGA_POINT_ADJ_X -0.375
40 #define SVGA_POINT_ADJ_Y -0.5
41
42 #define SVGA_LINE_ADJ_X -0.5
43 #define SVGA_LINE_ADJ_Y -0.5
44
45 #define SVGA_TRIANGLE_ADJ_X -0.375
46 #define SVGA_TRIANGLE_ADJ_Y -0.5
47
48
49 static void set_draw_viewport( struct svga_context *svga )
50 {
51    struct pipe_viewport_state vp = svga->curr.viewport;
52    float adjx = 0;
53    float adjy = 0;
54
55    switch (svga->curr.reduced_prim) {
56    case PIPE_PRIM_POINTS:
57       adjx = SVGA_POINT_ADJ_X;
58       adjy = SVGA_POINT_ADJ_Y;
59       break;
60    case PIPE_PRIM_LINES:
61       /* XXX: This is to compensate for the fact that wide lines are
62        * going to be drawn with triangles, but we're not catching all
63        * cases where that will happen.
64        */
65       if (svga->curr.rast->templ.line_width > 1.0) 
66       {
67          adjx = SVGA_LINE_ADJ_X + 0.175;
68          adjy = SVGA_LINE_ADJ_Y - 0.175;
69       }
70       else {
71          adjx = SVGA_LINE_ADJ_X;
72          adjy = SVGA_LINE_ADJ_Y;
73       }
74       break;
75    case PIPE_PRIM_TRIANGLES:
76       adjx += SVGA_TRIANGLE_ADJ_X;
77       adjy += SVGA_TRIANGLE_ADJ_Y;
78       break;
79    }
80
81    vp.translate[0] += adjx;
82    vp.translate[1] += adjy;
83
84    draw_set_viewport_state(svga->swtnl.draw, &vp);
85 }
86
87 static int update_swtnl_draw( struct svga_context *svga,
88                               unsigned dirty )
89 {
90    draw_flush( svga->swtnl.draw );
91
92    if (dirty & SVGA_NEW_VS) 
93       draw_bind_vertex_shader(svga->swtnl.draw,
94                               svga->curr.vs->draw_shader);
95
96    if (dirty & SVGA_NEW_VBUFFER)
97       draw_set_vertex_buffers(svga->swtnl.draw, 
98                               svga->curr.num_vertex_buffers, 
99                               svga->curr.vb);
100
101    if (dirty & SVGA_NEW_VELEMENT)
102       draw_set_vertex_elements(svga->swtnl.draw, 
103                                svga->curr.num_vertex_elements, 
104                                svga->curr.ve );
105
106    if (dirty & SVGA_NEW_CLIP)
107       draw_set_clip_state(svga->swtnl.draw, 
108                           &svga->curr.clip);
109
110    if (dirty & (SVGA_NEW_VIEWPORT |
111                 SVGA_NEW_REDUCED_PRIMITIVE | 
112                 SVGA_NEW_RAST))
113       set_draw_viewport( svga );
114
115    if (dirty & SVGA_NEW_RAST)
116       draw_set_rasterizer_state(svga->swtnl.draw,
117                                 &svga->curr.rast->templ);
118
119    if (dirty & SVGA_NEW_FRAME_BUFFER)
120       draw_set_mrd(svga->swtnl.draw, 
121                    svga->curr.depthscale);
122
123    if (dirty & SVGA_NEW_EDGEFLAGS)
124       draw_set_edgeflags( svga->swtnl.draw, 
125                           svga->curr.edgeflags );
126
127    return 0;
128 }
129
130
131 struct svga_tracked_state svga_update_swtnl_draw =
132 {
133    "update draw module state",
134    (SVGA_NEW_VS |
135     SVGA_NEW_VBUFFER |
136     SVGA_NEW_VELEMENT |
137     SVGA_NEW_CLIP |
138     SVGA_NEW_VIEWPORT |
139     SVGA_NEW_RAST |
140     SVGA_NEW_FRAME_BUFFER |
141     SVGA_NEW_REDUCED_PRIMITIVE |
142     SVGA_NEW_EDGEFLAGS),
143    update_swtnl_draw
144 };
145
146
147 int svga_swtnl_update_vdecl( struct svga_context *svga )
148 {
149    struct svga_vbuf_render *svga_render = svga_vbuf_render(svga->swtnl.backend);
150    struct draw_context *draw = svga->swtnl.draw;
151    struct vertex_info *vinfo = &svga_render->vertex_info;
152    SVGA3dVertexDecl vdecl[PIPE_MAX_ATTRIBS];
153    const enum interp_mode colorInterp =
154       svga->curr.rast->templ.flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
155    const struct svga_fragment_shader *fs = svga->curr.fs;
156    int offset = 0;
157    int nr_decls = 0;
158    int src, i;
159
160    memset(vinfo, 0, sizeof(*vinfo));
161    memset(vdecl, 0, sizeof(vdecl));
162
163    /* always add position */
164    src = draw_find_vs_output(draw, TGSI_SEMANTIC_POSITION, 0);
165    draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_LINEAR, src);
166    vinfo->attrib[0].emit = EMIT_4F;
167    vdecl[0].array.offset = offset;
168    vdecl[0].identity.type = SVGA3D_DECLTYPE_FLOAT4;
169    vdecl[0].identity.usage = SVGA3D_DECLUSAGE_POSITIONT;
170    vdecl[0].identity.usageIndex = 0;
171    offset += 16;
172    nr_decls++;
173
174    for (i = 0; i < fs->base.info.num_inputs; i++) {
175       unsigned name = fs->base.info.input_semantic_name[i];
176       unsigned index = fs->base.info.input_semantic_index[i];
177       src = draw_find_vs_output(draw, name, index);
178       vdecl[nr_decls].array.offset = offset;
179       vdecl[nr_decls].identity.usageIndex = fs->base.info.input_semantic_index[i];
180
181       switch (name) {
182       case TGSI_SEMANTIC_COLOR:
183          draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
184          vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_COLOR;
185          vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT4;
186          offset += 16;
187          nr_decls++;
188          break;
189       case TGSI_SEMANTIC_GENERIC:
190          draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
191          vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_TEXCOORD;
192          vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT4;
193          vdecl[nr_decls].identity.usageIndex += 1;
194          offset += 16;
195          nr_decls++;
196          break;
197       case TGSI_SEMANTIC_FOG:
198          draw_emit_vertex_attr(vinfo, EMIT_1F, INTERP_PERSPECTIVE, src);
199          vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_TEXCOORD;
200          vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT1;
201          assert(vdecl[nr_decls].identity.usageIndex == 0);
202          offset += 4;
203          nr_decls++;
204          break;
205       case TGSI_SEMANTIC_POSITION:
206          /* generated internally, not a vertex shader output */
207          break;
208       default:
209          assert(0);
210       }
211    }
212
213    draw_compute_vertex_size(vinfo);
214
215    svga_render->vdecl_count = nr_decls;
216    for (i = 0; i < svga_render->vdecl_count; i++)
217       vdecl[i].array.stride = offset;
218
219    if (memcmp(svga_render->vdecl, vdecl, sizeof(vdecl)) == 0)
220       return 0;
221
222    memcpy(svga_render->vdecl, vdecl, sizeof(vdecl));
223    svga->swtnl.new_vdecl = TRUE;
224
225    return 0;
226 }
227
228
229 static int update_swtnl_vdecl( struct svga_context *svga,
230                                unsigned dirty )
231 {
232    return svga_swtnl_update_vdecl( svga );
233 }
234
235
236 struct svga_tracked_state svga_update_swtnl_vdecl =
237 {
238    "update draw module vdecl",
239    (SVGA_NEW_VS |
240     SVGA_NEW_FS),
241    update_swtnl_vdecl
242 };