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