Merge branch '7.8'
[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 "util/u_inlines.h"
29 #include "pipe/p_state.h"
30
31 #include "svga_context.h"
32 #include "svga_swtnl.h"
33 #include "svga_state.h"
34
35 #include "svga_swtnl_private.h"
36
37
38 #define SVGA_POINT_ADJ_X -0.375
39 #define SVGA_POINT_ADJ_Y -0.5
40
41 #define SVGA_LINE_ADJ_X -0.5
42 #define SVGA_LINE_ADJ_Y -0.5
43
44 #define SVGA_TRIANGLE_ADJ_X -0.375
45 #define SVGA_TRIANGLE_ADJ_Y -0.5
46
47
48 static void set_draw_viewport( struct svga_context *svga )
49 {
50    struct pipe_viewport_state vp = svga->curr.viewport;
51    float adjx = 0;
52    float adjy = 0;
53
54    switch (svga->curr.reduced_prim) {
55    case PIPE_PRIM_POINTS:
56       adjx = SVGA_POINT_ADJ_X;
57       adjy = SVGA_POINT_ADJ_Y;
58       break;
59    case PIPE_PRIM_LINES:
60       /* XXX: This is to compensate for the fact that wide lines are
61        * going to be drawn with triangles, but we're not catching all
62        * cases where that will happen.
63        */
64       if (svga->curr.rast->templ.line_width > 1.0) 
65       {
66          adjx = SVGA_LINE_ADJ_X + 0.175;
67          adjy = SVGA_LINE_ADJ_Y - 0.175;
68       }
69       else {
70          adjx = SVGA_LINE_ADJ_X;
71          adjy = SVGA_LINE_ADJ_Y;
72       }
73       break;
74    case PIPE_PRIM_TRIANGLES:
75       adjx += SVGA_TRIANGLE_ADJ_X;
76       adjy += SVGA_TRIANGLE_ADJ_Y;
77       break;
78    }
79
80    vp.translate[0] += adjx;
81    vp.translate[1] += adjy;
82
83    draw_set_viewport_state(svga->swtnl.draw, &vp);
84 }
85
86 static int update_swtnl_draw( struct svga_context *svga,
87                               unsigned dirty )
88 {
89    draw_flush( svga->swtnl.draw );
90
91    if (dirty & SVGA_NEW_VS) 
92       draw_bind_vertex_shader(svga->swtnl.draw,
93                               svga->curr.vs->draw_shader);
94
95    if (dirty & SVGA_NEW_VBUFFER)
96       draw_set_vertex_buffers(svga->swtnl.draw, 
97                               svga->curr.num_vertex_buffers, 
98                               svga->curr.vb);
99
100    if (dirty & SVGA_NEW_VELEMENT)
101       draw_set_vertex_elements(svga->swtnl.draw, 
102                                svga->curr.velems->count, 
103                                svga->curr.velems->velem );
104
105    if (dirty & SVGA_NEW_CLIP)
106       draw_set_clip_state(svga->swtnl.draw, 
107                           &svga->curr.clip);
108
109    if (dirty & (SVGA_NEW_VIEWPORT |
110                 SVGA_NEW_REDUCED_PRIMITIVE | 
111                 SVGA_NEW_RAST))
112       set_draw_viewport( svga );
113
114    if (dirty & SVGA_NEW_RAST)
115       draw_set_rasterizer_state(svga->swtnl.draw,
116                                 &svga->curr.rast->templ);
117
118    if (dirty & SVGA_NEW_FRAME_BUFFER)
119       draw_set_mrd(svga->swtnl.draw, 
120                    svga->curr.depthscale);
121
122    return 0;
123 }
124
125
126 struct svga_tracked_state svga_update_swtnl_draw =
127 {
128    "update draw module state",
129    (SVGA_NEW_VS |
130     SVGA_NEW_VBUFFER |
131     SVGA_NEW_VELEMENT |
132     SVGA_NEW_CLIP |
133     SVGA_NEW_VIEWPORT |
134     SVGA_NEW_RAST |
135     SVGA_NEW_FRAME_BUFFER |
136     SVGA_NEW_REDUCED_PRIMITIVE),
137    update_swtnl_draw
138 };
139
140
141 int svga_swtnl_update_vdecl( struct svga_context *svga )
142 {
143    struct svga_vbuf_render *svga_render = svga_vbuf_render(svga->swtnl.backend);
144    struct draw_context *draw = svga->swtnl.draw;
145    struct vertex_info *vinfo = &svga_render->vertex_info;
146    SVGA3dVertexDecl vdecl[PIPE_MAX_ATTRIBS];
147    const enum interp_mode colorInterp =
148       svga->curr.rast->templ.flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
149    const struct svga_fragment_shader *fs = svga->curr.fs;
150    int offset = 0;
151    int nr_decls = 0;
152    int src, i;
153
154    memset(vinfo, 0, sizeof(*vinfo));
155    memset(vdecl, 0, sizeof(vdecl));
156
157    /* always add position */
158    src = draw_find_shader_output(draw, TGSI_SEMANTIC_POSITION, 0);
159    draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_LINEAR, src);
160    vinfo->attrib[0].emit = EMIT_4F;
161    vdecl[0].array.offset = offset;
162    vdecl[0].identity.type = SVGA3D_DECLTYPE_FLOAT4;
163    vdecl[0].identity.usage = SVGA3D_DECLUSAGE_POSITIONT;
164    vdecl[0].identity.usageIndex = 0;
165    offset += 16;
166    nr_decls++;
167
168    for (i = 0; i < fs->base.info.num_inputs; i++) {
169       unsigned name = fs->base.info.input_semantic_name[i];
170       unsigned index = fs->base.info.input_semantic_index[i];
171       src = draw_find_shader_output(draw, name, index);
172       vdecl[nr_decls].array.offset = offset;
173       vdecl[nr_decls].identity.usageIndex = fs->base.info.input_semantic_index[i];
174
175       switch (name) {
176       case TGSI_SEMANTIC_COLOR:
177          draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
178          vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_COLOR;
179          vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT4;
180          offset += 16;
181          nr_decls++;
182          break;
183       case TGSI_SEMANTIC_GENERIC:
184          draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
185          vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_TEXCOORD;
186          vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT4;
187          vdecl[nr_decls].identity.usageIndex += 1;
188          offset += 16;
189          nr_decls++;
190          break;
191       case TGSI_SEMANTIC_FOG:
192          draw_emit_vertex_attr(vinfo, EMIT_1F, INTERP_PERSPECTIVE, src);
193          vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_TEXCOORD;
194          vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT1;
195          assert(vdecl[nr_decls].identity.usageIndex == 0);
196          offset += 4;
197          nr_decls++;
198          break;
199       case TGSI_SEMANTIC_POSITION:
200          /* generated internally, not a vertex shader output */
201          break;
202       default:
203          assert(0);
204       }
205    }
206
207    draw_compute_vertex_size(vinfo);
208
209    svga_render->vdecl_count = nr_decls;
210    for (i = 0; i < svga_render->vdecl_count; i++)
211       vdecl[i].array.stride = offset;
212
213    if (memcmp(svga_render->vdecl, vdecl, sizeof(vdecl)) == 0)
214       return 0;
215
216    memcpy(svga_render->vdecl, vdecl, sizeof(vdecl));
217    svga->swtnl.new_vdecl = TRUE;
218
219    return 0;
220 }
221
222
223 static int update_swtnl_vdecl( struct svga_context *svga,
224                                unsigned dirty )
225 {
226    return svga_swtnl_update_vdecl( svga );
227 }
228
229
230 struct svga_tracked_state svga_update_swtnl_vdecl =
231 {
232    "update draw module vdecl",
233    (SVGA_NEW_VS |
234     SVGA_NEW_FS),
235    update_swtnl_vdecl
236 };