1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
26 #include "draw/draw_context.h"
27 #include "draw/draw_vbuf.h"
28 #include "util/u_inlines.h"
29 #include "pipe/p_state.h"
31 #include "svga_context.h"
32 #include "svga_swtnl.h"
33 #include "svga_state.h"
34 #include "svga_tgsi.h"
35 #include "svga_swtnl_private.h"
38 #define SVGA_POINT_ADJ_X -0.375
39 #define SVGA_POINT_ADJ_Y -0.5
41 #define SVGA_LINE_ADJ_X -0.5
42 #define SVGA_LINE_ADJ_Y -0.5
44 #define SVGA_TRIANGLE_ADJ_X -0.375
45 #define SVGA_TRIANGLE_ADJ_Y -0.5
48 static void set_draw_viewport( struct svga_context
*svga
)
50 struct pipe_viewport_state vp
= svga
->curr
.viewport
;
54 switch (svga
->curr
.reduced_prim
) {
55 case PIPE_PRIM_POINTS
:
56 adjx
= SVGA_POINT_ADJ_X
;
57 adjy
= SVGA_POINT_ADJ_Y
;
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.
64 if (svga
->curr
.rast
->need_pipeline
& SVGA_PIPELINE_FLAG_LINES
)
66 adjx
= SVGA_LINE_ADJ_X
+ 0.175;
67 adjy
= SVGA_LINE_ADJ_Y
- 0.175;
70 adjx
= SVGA_LINE_ADJ_X
;
71 adjy
= SVGA_LINE_ADJ_Y
;
74 case PIPE_PRIM_TRIANGLES
:
75 adjx
+= SVGA_TRIANGLE_ADJ_X
;
76 adjy
+= SVGA_TRIANGLE_ADJ_Y
;
80 vp
.translate
[0] += adjx
;
81 vp
.translate
[1] += adjy
;
83 draw_set_viewport_state(svga
->swtnl
.draw
, &vp
);
86 static enum pipe_error
87 update_swtnl_draw( struct svga_context
*svga
,
90 draw_flush( svga
->swtnl
.draw
);
92 if (dirty
& SVGA_NEW_VS
)
93 draw_bind_vertex_shader(svga
->swtnl
.draw
,
94 svga
->curr
.vs
->draw_shader
);
96 if (dirty
& SVGA_NEW_FS
)
97 draw_bind_fragment_shader(svga
->swtnl
.draw
,
98 svga
->curr
.fs
->draw_shader
);
100 if (dirty
& SVGA_NEW_VBUFFER
)
101 draw_set_vertex_buffers(svga
->swtnl
.draw
,
102 svga
->curr
.num_vertex_buffers
,
105 if (dirty
& SVGA_NEW_VELEMENT
)
106 draw_set_vertex_elements(svga
->swtnl
.draw
,
107 svga
->curr
.velems
->count
,
108 svga
->curr
.velems
->velem
);
110 if (dirty
& SVGA_NEW_CLIP
)
111 draw_set_clip_state(svga
->swtnl
.draw
,
114 if (dirty
& (SVGA_NEW_VIEWPORT
|
115 SVGA_NEW_REDUCED_PRIMITIVE
|
117 set_draw_viewport( svga
);
119 if (dirty
& SVGA_NEW_RAST
)
120 draw_set_rasterizer_state(svga
->swtnl
.draw
,
121 &svga
->curr
.rast
->templ
,
122 (void *) svga
->curr
.rast
);
124 if (dirty
& SVGA_NEW_FRAME_BUFFER
)
125 draw_set_mrd(svga
->swtnl
.draw
,
126 svga
->curr
.depthscale
);
132 struct svga_tracked_state svga_update_swtnl_draw
=
134 "update draw module state",
141 SVGA_NEW_FRAME_BUFFER
|
142 SVGA_NEW_REDUCED_PRIMITIVE
),
148 svga_swtnl_update_vdecl( struct svga_context
*svga
)
150 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(svga
->swtnl
.backend
);
151 struct draw_context
*draw
= svga
->swtnl
.draw
;
152 struct vertex_info
*vinfo
= &svga_render
->vertex_info
;
153 SVGA3dVertexDecl vdecl
[PIPE_MAX_ATTRIBS
];
154 const enum interp_mode colorInterp
=
155 svga
->curr
.rast
->templ
.flatshade
? INTERP_CONSTANT
: INTERP_LINEAR
;
156 struct svga_fragment_shader
*fs
= svga
->curr
.fs
;
161 memset(vinfo
, 0, sizeof(*vinfo
));
162 memset(vdecl
, 0, sizeof(vdecl
));
164 /* always add position */
165 src
= draw_find_shader_output(draw
, TGSI_SEMANTIC_POSITION
, 0);
166 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_LINEAR
, src
);
167 vinfo
->attrib
[0].emit
= EMIT_4F
;
168 vdecl
[0].array
.offset
= offset
;
169 vdecl
[0].identity
.type
= SVGA3D_DECLTYPE_FLOAT4
;
170 vdecl
[0].identity
.usage
= SVGA3D_DECLUSAGE_POSITIONT
;
171 vdecl
[0].identity
.usageIndex
= 0;
175 for (i
= 0; i
< fs
->base
.info
.num_inputs
; i
++) {
176 const unsigned sem_name
= fs
->base
.info
.input_semantic_name
[i
];
177 const unsigned sem_index
= fs
->base
.info
.input_semantic_index
[i
];
179 src
= draw_find_shader_output(draw
, sem_name
, sem_index
);
181 vdecl
[nr_decls
].array
.offset
= offset
;
182 vdecl
[nr_decls
].identity
.usageIndex
= sem_index
;
185 case TGSI_SEMANTIC_COLOR
:
186 draw_emit_vertex_attr(vinfo
, EMIT_4F
, colorInterp
, src
);
187 vdecl
[nr_decls
].identity
.usage
= SVGA3D_DECLUSAGE_COLOR
;
188 vdecl
[nr_decls
].identity
.type
= SVGA3D_DECLTYPE_FLOAT4
;
192 case TGSI_SEMANTIC_GENERIC
:
193 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
, src
);
194 vdecl
[nr_decls
].identity
.usage
= SVGA3D_DECLUSAGE_TEXCOORD
;
195 vdecl
[nr_decls
].identity
.type
= SVGA3D_DECLTYPE_FLOAT4
;
196 vdecl
[nr_decls
].identity
.usageIndex
=
197 svga_remap_generic_index(fs
->generic_remap_table
, sem_index
);
201 case TGSI_SEMANTIC_FOG
:
202 draw_emit_vertex_attr(vinfo
, EMIT_1F
, INTERP_PERSPECTIVE
, src
);
203 vdecl
[nr_decls
].identity
.usage
= SVGA3D_DECLUSAGE_TEXCOORD
;
204 vdecl
[nr_decls
].identity
.type
= SVGA3D_DECLTYPE_FLOAT1
;
205 assert(vdecl
[nr_decls
].identity
.usageIndex
== 0);
209 case TGSI_SEMANTIC_POSITION
:
210 /* generated internally, not a vertex shader output */
217 draw_compute_vertex_size(vinfo
);
219 svga_render
->vdecl_count
= nr_decls
;
220 for (i
= 0; i
< svga_render
->vdecl_count
; i
++)
221 vdecl
[i
].array
.stride
= offset
;
223 if (memcmp(svga_render
->vdecl
, vdecl
, sizeof(vdecl
)) == 0)
226 memcpy(svga_render
->vdecl
, vdecl
, sizeof(vdecl
));
227 svga
->swtnl
.new_vdecl
= TRUE
;
233 static enum pipe_error
234 update_swtnl_vdecl( struct svga_context
*svga
,
237 return svga_swtnl_update_vdecl( svga
);
241 struct svga_tracked_state svga_update_swtnl_vdecl
=
243 "update draw module vdecl",