Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / drivers / svga / svga_state_need_swtnl.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 "util/u_inlines.h"
27 #include "pipe/p_state.h"
28
29
30 #include "svga_context.h"
31 #include "svga_state.h"
32 #include "svga_debug.h"
33 #include "svga_hw_reg.h"
34
35 /***********************************************************************
36 */
37
38
39 /**
40 * Given a gallium vertex element format, return the corresponding SVGA3D
41 * format. Return SVGA3D_DECLTYPE_MAX for unsupported gallium formats.
42 */
43 static INLINE SVGA3dDeclType
44 svga_translate_vertex_format(enum pipe_format format)
45 {
46 switch (format) {
47 case PIPE_FORMAT_R32_FLOAT: return SVGA3D_DECLTYPE_FLOAT1;
48 case PIPE_FORMAT_R32G32_FLOAT: return SVGA3D_DECLTYPE_FLOAT2;
49 case PIPE_FORMAT_R32G32B32_FLOAT: return SVGA3D_DECLTYPE_FLOAT3;
50 case PIPE_FORMAT_R32G32B32A32_FLOAT: return SVGA3D_DECLTYPE_FLOAT4;
51 case PIPE_FORMAT_B8G8R8A8_UNORM: return SVGA3D_DECLTYPE_D3DCOLOR;
52 case PIPE_FORMAT_R8G8B8A8_USCALED: return SVGA3D_DECLTYPE_UBYTE4;
53 case PIPE_FORMAT_R16G16_SSCALED: return SVGA3D_DECLTYPE_SHORT2;
54 case PIPE_FORMAT_R16G16B16A16_SSCALED: return SVGA3D_DECLTYPE_SHORT4;
55 case PIPE_FORMAT_R8G8B8A8_UNORM: return SVGA3D_DECLTYPE_UBYTE4N;
56 case PIPE_FORMAT_R16G16_SNORM: return SVGA3D_DECLTYPE_SHORT2N;
57 case PIPE_FORMAT_R16G16B16A16_SNORM: return SVGA3D_DECLTYPE_SHORT4N;
58 case PIPE_FORMAT_R16G16_UNORM: return SVGA3D_DECLTYPE_USHORT2N;
59 case PIPE_FORMAT_R16G16B16A16_UNORM: return SVGA3D_DECLTYPE_USHORT4N;
60 case PIPE_FORMAT_R10G10B10X2_USCALED: return SVGA3D_DECLTYPE_UDEC3;
61 case PIPE_FORMAT_R10G10B10X2_SNORM: return SVGA3D_DECLTYPE_DEC3N;
62 case PIPE_FORMAT_R16G16_FLOAT: return SVGA3D_DECLTYPE_FLOAT16_2;
63 case PIPE_FORMAT_R16G16B16A16_FLOAT: return SVGA3D_DECLTYPE_FLOAT16_4;
64
65 default:
66 /* There are many formats without hardware support. This case
67 * will be hit regularly, meaning we'll need swvfetch.
68 */
69 return SVGA3D_DECLTYPE_MAX;
70 }
71 }
72
73
74 static int update_need_swvfetch( struct svga_context *svga,
75 unsigned dirty )
76 {
77 unsigned i;
78 boolean need_swvfetch = FALSE;
79
80 if (!svga->curr.velems) {
81 /* No vertex elements bound. */
82 return 0;
83 }
84
85 for (i = 0; i < svga->curr.velems->count; i++) {
86 svga->state.sw.ve_format[i] = svga_translate_vertex_format(svga->curr.velems->velem[i].src_format);
87 if (svga->state.sw.ve_format[i] == SVGA3D_DECLTYPE_MAX) {
88 /* Unsupported format - use software fetch */
89 need_swvfetch = TRUE;
90 break;
91 }
92 }
93
94 if (need_swvfetch != svga->state.sw.need_swvfetch) {
95 svga->state.sw.need_swvfetch = need_swvfetch;
96 svga->dirty |= SVGA_NEW_NEED_SWVFETCH;
97 }
98
99 return 0;
100 }
101
102 struct svga_tracked_state svga_update_need_swvfetch =
103 {
104 "update need_swvfetch",
105 ( SVGA_NEW_VELEMENT ),
106 update_need_swvfetch
107 };
108
109
110 /***********************************************************************
111 */
112
113 static int update_need_pipeline( struct svga_context *svga,
114 unsigned dirty )
115 {
116
117 boolean need_pipeline = FALSE;
118 struct svga_vertex_shader *vs = svga->curr.vs;
119
120 /* SVGA_NEW_RAST, SVGA_NEW_REDUCED_PRIMITIVE
121 */
122 if (svga->curr.rast->need_pipeline & (1 << svga->curr.reduced_prim)) {
123 SVGA_DBG(DEBUG_SWTNL, "%s: rast need_pipeline (0x%x) & prim (0x%x)\n",
124 __FUNCTION__,
125 svga->curr.rast->need_pipeline,
126 (1 << svga->curr.reduced_prim) );
127 SVGA_DBG(DEBUG_SWTNL, "%s: rast need_pipeline tris (%s), lines (%s), points (%s)\n",
128 __FUNCTION__,
129 svga->curr.rast->need_pipeline_tris_str,
130 svga->curr.rast->need_pipeline_lines_str,
131 svga->curr.rast->need_pipeline_points_str);
132 need_pipeline = TRUE;
133 }
134
135 /* EDGEFLAGS
136 */
137 if (vs->base.info.writes_edgeflag) {
138 SVGA_DBG(DEBUG_SWTNL, "%s: edgeflags\n", __FUNCTION__);
139 need_pipeline = TRUE;
140 }
141
142 /* SVGA_NEW_CLIP
143 */
144 if (svga->curr.clip.nr) {
145 SVGA_DBG(DEBUG_SWTNL, "%s: userclip\n", __FUNCTION__);
146 need_pipeline = TRUE;
147 }
148
149 if (need_pipeline != svga->state.sw.need_pipeline) {
150 svga->state.sw.need_pipeline = need_pipeline;
151 svga->dirty |= SVGA_NEW_NEED_PIPELINE;
152 }
153
154 /* DEBUG */
155 if (0 && svga->state.sw.need_pipeline)
156 debug_printf("sw.need_pipeline = %d\n", svga->state.sw.need_pipeline);
157
158 return 0;
159 }
160
161
162 struct svga_tracked_state svga_update_need_pipeline =
163 {
164 "need pipeline",
165 (SVGA_NEW_RAST |
166 SVGA_NEW_CLIP |
167 SVGA_NEW_VS |
168 SVGA_NEW_REDUCED_PRIMITIVE),
169 update_need_pipeline
170 };
171
172
173 /***********************************************************************
174 */
175
176 static int update_need_swtnl( struct svga_context *svga,
177 unsigned dirty )
178 {
179 boolean need_swtnl;
180
181 if (svga->debug.no_swtnl) {
182 svga->state.sw.need_swvfetch = FALSE;
183 svga->state.sw.need_pipeline = FALSE;
184 }
185
186 need_swtnl = (svga->state.sw.need_swvfetch ||
187 svga->state.sw.need_pipeline);
188
189 if (svga->debug.force_swtnl) {
190 need_swtnl = TRUE;
191 }
192
193 /*
194 * Some state changes the draw module does makes us belive we
195 * we don't need swtnl. This causes the vdecl code to pickup
196 * the wrong buffers and vertex formats. Try trivial/line-wide.
197 */
198 if (svga->state.sw.in_swtnl_draw)
199 need_swtnl = TRUE;
200
201 if (need_swtnl != svga->state.sw.need_swtnl) {
202 SVGA_DBG(DEBUG_SWTNL|DEBUG_PERF,
203 "%s: need_swvfetch %s, need_pipeline %s\n",
204 __FUNCTION__,
205 svga->state.sw.need_swvfetch ? "true" : "false",
206 svga->state.sw.need_pipeline ? "true" : "false");
207
208 svga->state.sw.need_swtnl = need_swtnl;
209 svga->dirty |= SVGA_NEW_NEED_SWTNL;
210 svga->swtnl.new_vdecl = TRUE;
211 }
212
213 return 0;
214 }
215
216
217 struct svga_tracked_state svga_update_need_swtnl =
218 {
219 "need swtnl",
220 (SVGA_NEW_NEED_PIPELINE |
221 SVGA_NEW_NEED_SWVFETCH),
222 update_need_swtnl
223 };