svga: update driver for version 10 GPU interface
[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_bitmask.h"
29 #include "util/u_inlines.h"
30 #include "pipe/p_state.h"
31
32 #include "svga_cmd.h"
33 #include "svga_context.h"
34 #include "svga_shader.h"
35 #include "svga_swtnl.h"
36 #include "svga_state.h"
37 #include "svga_tgsi.h"
38 #include "svga_swtnl_private.h"
39
40
41 #define SVGA_POINT_ADJ_X -0.375f
42 #define SVGA_POINT_ADJ_Y -0.5f
43
44 #define SVGA_LINE_ADJ_X -0.5f
45 #define SVGA_LINE_ADJ_Y -0.5f
46
47 #define SVGA_TRIANGLE_ADJ_X -0.375f
48 #define SVGA_TRIANGLE_ADJ_Y -0.5f
49
50
51 static void set_draw_viewport( struct svga_context *svga )
52 {
53 struct pipe_viewport_state vp = svga->curr.viewport;
54 float adjx = 0.0f;
55 float adjy = 0.0f;
56
57 if (svga_have_vgpu10(svga)) {
58 if (svga->curr.reduced_prim == PIPE_PRIM_TRIANGLES) {
59 adjy = 0.25;
60 }
61 }
62 else {
63 switch (svga->curr.reduced_prim) {
64 case PIPE_PRIM_POINTS:
65 adjx = SVGA_POINT_ADJ_X;
66 adjy = SVGA_POINT_ADJ_Y;
67 break;
68 case PIPE_PRIM_LINES:
69 /* XXX: This is to compensate for the fact that wide lines are
70 * going to be drawn with triangles, but we're not catching all
71 * cases where that will happen.
72 */
73 if (svga->curr.rast->need_pipeline & SVGA_PIPELINE_FLAG_LINES)
74 {
75 adjx = SVGA_LINE_ADJ_X + 0.175f;
76 adjy = SVGA_LINE_ADJ_Y - 0.175f;
77 }
78 else {
79 adjx = SVGA_LINE_ADJ_X;
80 adjy = SVGA_LINE_ADJ_Y;
81 }
82 break;
83 case PIPE_PRIM_TRIANGLES:
84 adjx += SVGA_TRIANGLE_ADJ_X;
85 adjy += SVGA_TRIANGLE_ADJ_Y;
86 break;
87 }
88 }
89
90 vp.translate[0] += adjx;
91 vp.translate[1] += adjy;
92
93 draw_set_viewport_states(svga->swtnl.draw, 0, 1, &vp);
94 }
95
96 static enum pipe_error
97 update_swtnl_draw( struct svga_context *svga,
98 unsigned dirty )
99 {
100 draw_flush( svga->swtnl.draw );
101
102 if (dirty & SVGA_NEW_VS)
103 draw_bind_vertex_shader(svga->swtnl.draw,
104 svga->curr.vs->draw_shader);
105
106 if (dirty & SVGA_NEW_FS)
107 draw_bind_fragment_shader(svga->swtnl.draw,
108 svga->curr.fs->draw_shader);
109
110 if (dirty & SVGA_NEW_VBUFFER)
111 draw_set_vertex_buffers(svga->swtnl.draw, 0,
112 svga->curr.num_vertex_buffers,
113 svga->curr.vb);
114
115 if (dirty & SVGA_NEW_VELEMENT)
116 draw_set_vertex_elements(svga->swtnl.draw,
117 svga->curr.velems->count,
118 svga->curr.velems->velem );
119
120 if (dirty & SVGA_NEW_CLIP)
121 draw_set_clip_state(svga->swtnl.draw,
122 &svga->curr.clip);
123
124 if (dirty & (SVGA_NEW_VIEWPORT |
125 SVGA_NEW_REDUCED_PRIMITIVE |
126 SVGA_NEW_RAST))
127 set_draw_viewport( svga );
128
129 if (dirty & SVGA_NEW_RAST)
130 draw_set_rasterizer_state(svga->swtnl.draw,
131 &svga->curr.rast->templ,
132 (void *) svga->curr.rast);
133
134 /* Tell the draw module how deep the Z/depth buffer is.
135 *
136 * If no depth buffer is bound, send the utility function the
137 * format for no bound depth (PIPE_FORMAT_NONE).
138 */
139 if (dirty & SVGA_NEW_FRAME_BUFFER)
140 draw_set_zs_format(svga->swtnl.draw,
141 (svga->curr.framebuffer.zsbuf) ?
142 svga->curr.framebuffer.zsbuf->format : PIPE_FORMAT_NONE);
143
144 return PIPE_OK;
145 }
146
147
148 struct svga_tracked_state svga_update_swtnl_draw =
149 {
150 "update draw module state",
151 (SVGA_NEW_VS |
152 SVGA_NEW_VBUFFER |
153 SVGA_NEW_VELEMENT |
154 SVGA_NEW_CLIP |
155 SVGA_NEW_VIEWPORT |
156 SVGA_NEW_RAST |
157 SVGA_NEW_FRAME_BUFFER |
158 SVGA_NEW_REDUCED_PRIMITIVE),
159 update_swtnl_draw
160 };
161
162
163 static SVGA3dSurfaceFormat
164 translate_vertex_format(SVGA3dDeclType format)
165 {
166 switch (format) {
167 case SVGA3D_DECLTYPE_FLOAT1:
168 return SVGA3D_R32_FLOAT;
169 case SVGA3D_DECLTYPE_FLOAT2:
170 return SVGA3D_R32G32_FLOAT;
171 case SVGA3D_DECLTYPE_FLOAT3:
172 return SVGA3D_R32G32B32_FLOAT;
173 case SVGA3D_DECLTYPE_FLOAT4:
174 return SVGA3D_R32G32B32A32_FLOAT;
175 default:
176 assert(!"Unexpected format in translate_vertex_format()");
177 return SVGA3D_R32G32B32A32_FLOAT;
178 }
179 }
180
181
182 static SVGA3dElementLayoutId
183 svga_vdecl_to_input_element(struct svga_context *svga,
184 const SVGA3dVertexDecl *vdecl, unsigned num_decls)
185 {
186 SVGA3dElementLayoutId id;
187 SVGA3dInputElementDesc elements[PIPE_MAX_ATTRIBS];
188 enum pipe_error ret;
189 unsigned i;
190
191 assert(num_decls <= PIPE_MAX_ATTRIBS);
192 assert(svga_have_vgpu10(svga));
193
194 for (i = 0; i < num_decls; i++) {
195 elements[i].inputSlot = 0; /* vertex buffer index */
196 elements[i].alignedByteOffset = vdecl[i].array.offset;
197 elements[i].format = translate_vertex_format(vdecl[i].identity.type);
198 elements[i].inputSlotClass = SVGA3D_INPUT_PER_VERTEX_DATA;
199 elements[i].instanceDataStepRate = 0;
200 elements[i].inputRegister = i;
201 }
202
203 id = util_bitmask_add(svga->input_element_object_id_bm);
204
205 ret = SVGA3D_vgpu10_DefineElementLayout(svga->swc, num_decls, id, elements);
206 if (ret != PIPE_OK) {
207 svga_context_flush(svga, NULL);
208 ret = SVGA3D_vgpu10_DefineElementLayout(svga->swc, num_decls, id, elements);
209 assert(ret == PIPE_OK);
210 }
211
212 return id;
213 }
214
215
216 enum pipe_error
217 svga_swtnl_update_vdecl( struct svga_context *svga )
218 {
219 struct svga_vbuf_render *svga_render = svga_vbuf_render(svga->swtnl.backend);
220 struct draw_context *draw = svga->swtnl.draw;
221 struct vertex_info *vinfo = &svga_render->vertex_info;
222 SVGA3dVertexDecl vdecl[PIPE_MAX_ATTRIBS];
223 const enum interp_mode colorInterp =
224 svga->curr.rast->templ.flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
225 struct svga_fragment_shader *fs = svga->curr.fs;
226 int offset = 0;
227 int nr_decls = 0;
228 int src;
229 unsigned i;
230 int any_change;
231
232 memset(vinfo, 0, sizeof(*vinfo));
233 memset(vdecl, 0, sizeof(vdecl));
234
235 draw_prepare_shader_outputs(draw);
236
237 /* always add position */
238 src = draw_find_shader_output(draw, TGSI_SEMANTIC_POSITION, 0);
239 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_LINEAR, src);
240 vinfo->attrib[0].emit = EMIT_4F;
241 vdecl[0].array.offset = offset;
242 vdecl[0].identity.method = SVGA3D_DECLMETHOD_DEFAULT;
243 vdecl[0].identity.type = SVGA3D_DECLTYPE_FLOAT4;
244 vdecl[0].identity.usage = SVGA3D_DECLUSAGE_POSITIONT;
245 vdecl[0].identity.usageIndex = 0;
246 offset += 16;
247 nr_decls++;
248
249 for (i = 0; i < fs->base.info.num_inputs; i++) {
250 const unsigned sem_name = fs->base.info.input_semantic_name[i];
251 const unsigned sem_index = fs->base.info.input_semantic_index[i];
252
253 src = draw_find_shader_output(draw, sem_name, sem_index);
254
255 vdecl[nr_decls].array.offset = offset;
256 vdecl[nr_decls].identity.usageIndex = sem_index;
257
258 switch (sem_name) {
259 case TGSI_SEMANTIC_COLOR:
260 draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
261 vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_COLOR;
262 vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT4;
263 offset += 16;
264 nr_decls++;
265 break;
266 case TGSI_SEMANTIC_GENERIC:
267 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
268 vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_TEXCOORD;
269 vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT4;
270 vdecl[nr_decls].identity.usageIndex =
271 svga_remap_generic_index(fs->generic_remap_table, sem_index);
272 offset += 16;
273 nr_decls++;
274 break;
275 case TGSI_SEMANTIC_FOG:
276 draw_emit_vertex_attr(vinfo, EMIT_1F, INTERP_PERSPECTIVE, src);
277 vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_TEXCOORD;
278 vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT1;
279 assert(vdecl[nr_decls].identity.usageIndex == 0);
280 offset += 4;
281 nr_decls++;
282 break;
283 case TGSI_SEMANTIC_POSITION:
284 /* generated internally, not a vertex shader output */
285 break;
286 default:
287 assert(0);
288 }
289 }
290
291 draw_compute_vertex_size(vinfo);
292
293 svga_render->vdecl_count = nr_decls;
294 for (i = 0; i < svga_render->vdecl_count; i++) {
295 vdecl[i].array.stride = offset;
296 }
297
298 any_change = memcmp(svga_render->vdecl, vdecl, sizeof(vdecl));
299
300 if (svga_have_vgpu10(svga)) {
301 enum pipe_error ret;
302
303 if (!any_change && svga_render->layout_id != SVGA3D_INVALID_ID) {
304 return PIPE_OK;
305 }
306
307 if (svga_render->layout_id != SVGA3D_INVALID_ID) {
308 /* destroy old */
309 ret = SVGA3D_vgpu10_DestroyElementLayout(svga->swc,
310 svga_render->layout_id);
311 if (ret != PIPE_OK) {
312 svga_context_flush(svga, NULL);
313 ret = SVGA3D_vgpu10_DestroyElementLayout(svga->swc,
314 svga_render->layout_id);
315 assert(ret == PIPE_OK);
316 }
317
318 /**
319 * reset current layout id state after the element layout is
320 * destroyed, so that if a new layout has the same layout id, we
321 * will know to re-issue the SetInputLayout command.
322 */
323 if (svga->state.hw_draw.layout_id == svga_render->layout_id)
324 svga->state.hw_draw.layout_id = SVGA3D_INVALID_ID;
325
326 util_bitmask_clear(svga->input_element_object_id_bm,
327 svga_render->layout_id);
328 }
329
330 svga_render->layout_id =
331 svga_vdecl_to_input_element(svga, vdecl, nr_decls);
332
333 /* bind new */
334 if (svga->state.hw_draw.layout_id != svga_render->layout_id) {
335 ret = SVGA3D_vgpu10_SetInputLayout(svga->swc, svga_render->layout_id);
336 if (ret != PIPE_OK) {
337 svga_context_flush(svga, NULL);
338 ret = SVGA3D_vgpu10_SetInputLayout(svga->swc,
339 svga_render->layout_id);
340 assert(ret == PIPE_OK);
341 }
342
343 svga->state.hw_draw.layout_id = svga_render->layout_id;
344 }
345 }
346 else {
347 if (!any_change)
348 return PIPE_OK;
349 }
350
351 memcpy(svga_render->vdecl, vdecl, sizeof(vdecl));
352 svga->swtnl.new_vdecl = TRUE;
353
354 return 0;
355 }
356
357
358 static enum pipe_error
359 update_swtnl_vdecl( struct svga_context *svga,
360 unsigned dirty )
361 {
362 return svga_swtnl_update_vdecl( svga );
363 }
364
365
366 struct svga_tracked_state svga_update_swtnl_vdecl =
367 {
368 "update draw module vdecl",
369 (SVGA_NEW_VS |
370 SVGA_NEW_FS),
371 update_swtnl_vdecl
372 };