• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pipe/p_defines.h"
27 #include "util/u_bitmask.h"
28 #include "util/u_format.h"
29 #include "util/u_helpers.h"
30 #include "util/u_inlines.h"
31 #include "util/u_math.h"
32 #include "util/u_memory.h"
33 #include "util/u_transfer.h"
34 #include "tgsi/tgsi_parse.h"
35 
36 #include "svga_context.h"
37 #include "svga_cmd.h"
38 #include "svga_format.h"
39 #include "svga_resource_buffer.h"
40 #include "svga_screen.h"
41 
42 
43 static void
svga_set_vertex_buffers(struct pipe_context * pipe,unsigned start_slot,unsigned count,const struct pipe_vertex_buffer * buffers)44 svga_set_vertex_buffers(struct pipe_context *pipe,
45                         unsigned start_slot, unsigned count,
46                         const struct pipe_vertex_buffer *buffers)
47 {
48    struct svga_context *svga = svga_context(pipe);
49 
50    util_set_vertex_buffers_count(svga->curr.vb,
51                                  &svga->curr.num_vertex_buffers,
52                                  buffers, start_slot, count);
53 
54    svga->dirty |= SVGA_NEW_VBUFFER;
55 }
56 
57 
58 /**
59  * Does the given vertex attrib format need range adjustment in the VS?
60  * Range adjustment scales and biases values from [0,1] to [-1,1].
61  * This lets us avoid the swtnl path.
62  */
63 static boolean
attrib_needs_range_adjustment(enum pipe_format format)64 attrib_needs_range_adjustment(enum pipe_format format)
65 {
66    switch (format) {
67    case PIPE_FORMAT_R8G8B8_SNORM:
68       return TRUE;
69    default:
70       return FALSE;
71    }
72 }
73 
74 
75 /**
76  * Given a gallium vertex element format, return the corresponding
77  * SVGA3dDeclType.
78  */
79 static SVGA3dDeclType
translate_vertex_format_to_decltype(enum pipe_format format)80 translate_vertex_format_to_decltype(enum pipe_format format)
81 {
82    switch (format) {
83    case PIPE_FORMAT_R32_FLOAT:            return SVGA3D_DECLTYPE_FLOAT1;
84    case PIPE_FORMAT_R32G32_FLOAT:         return SVGA3D_DECLTYPE_FLOAT2;
85    case PIPE_FORMAT_R32G32B32_FLOAT:      return SVGA3D_DECLTYPE_FLOAT3;
86    case PIPE_FORMAT_R32G32B32A32_FLOAT:   return SVGA3D_DECLTYPE_FLOAT4;
87    case PIPE_FORMAT_B8G8R8A8_UNORM:       return SVGA3D_DECLTYPE_D3DCOLOR;
88    case PIPE_FORMAT_R8G8B8A8_USCALED:     return SVGA3D_DECLTYPE_UBYTE4;
89    case PIPE_FORMAT_R16G16_SSCALED:       return SVGA3D_DECLTYPE_SHORT2;
90    case PIPE_FORMAT_R16G16B16A16_SSCALED: return SVGA3D_DECLTYPE_SHORT4;
91    case PIPE_FORMAT_R8G8B8A8_UNORM:       return SVGA3D_DECLTYPE_UBYTE4N;
92    case PIPE_FORMAT_R16G16_SNORM:         return SVGA3D_DECLTYPE_SHORT2N;
93    case PIPE_FORMAT_R16G16B16A16_SNORM:   return SVGA3D_DECLTYPE_SHORT4N;
94    case PIPE_FORMAT_R16G16_UNORM:         return SVGA3D_DECLTYPE_USHORT2N;
95    case PIPE_FORMAT_R16G16B16A16_UNORM:   return SVGA3D_DECLTYPE_USHORT4N;
96    case PIPE_FORMAT_R10G10B10X2_USCALED:  return SVGA3D_DECLTYPE_UDEC3;
97    case PIPE_FORMAT_R10G10B10X2_SNORM:    return SVGA3D_DECLTYPE_DEC3N;
98    case PIPE_FORMAT_R16G16_FLOAT:         return SVGA3D_DECLTYPE_FLOAT16_2;
99    case PIPE_FORMAT_R16G16B16A16_FLOAT:   return SVGA3D_DECLTYPE_FLOAT16_4;
100 
101    /* See attrib_needs_adjustment() and attrib_needs_w_to_1() above */
102    case PIPE_FORMAT_R8G8B8_SNORM:         return SVGA3D_DECLTYPE_UBYTE4N;
103 
104    /* See attrib_needs_w_to_1() above */
105    case PIPE_FORMAT_R16G16B16_SNORM:      return SVGA3D_DECLTYPE_SHORT4N;
106    case PIPE_FORMAT_R16G16B16_UNORM:      return SVGA3D_DECLTYPE_USHORT4N;
107    case PIPE_FORMAT_R8G8B8_UNORM:         return SVGA3D_DECLTYPE_UBYTE4N;
108 
109    default:
110       /* There are many formats without hardware support.  This case
111        * will be hit regularly, meaning we'll need swvfetch.
112        */
113       return SVGA3D_DECLTYPE_MAX;
114    }
115 }
116 
117 
118 static void
define_input_element_object(struct svga_context * svga,struct svga_velems_state * velems)119 define_input_element_object(struct svga_context *svga,
120                             struct svga_velems_state *velems)
121 {
122    SVGA3dInputElementDesc elements[PIPE_MAX_ATTRIBS];
123    enum pipe_error ret;
124    unsigned i;
125 
126    assert(velems->count <= PIPE_MAX_ATTRIBS);
127    assert(svga_have_vgpu10(svga));
128 
129    for (i = 0; i < velems->count; i++) {
130       const struct pipe_vertex_element *elem = velems->velem + i;
131       SVGA3dSurfaceFormat svga_format;
132       unsigned vf_flags;
133 
134       svga_translate_vertex_format_vgpu10(elem->src_format,
135                                           &svga_format, &vf_flags);
136 
137       velems->decl_type[i] =
138          translate_vertex_format_to_decltype(elem->src_format);
139       elements[i].inputSlot = elem->vertex_buffer_index;
140       elements[i].alignedByteOffset = elem->src_offset;
141       elements[i].format = svga_format;
142 
143       if (elem->instance_divisor) {
144          elements[i].inputSlotClass = SVGA3D_INPUT_PER_INSTANCE_DATA;
145          elements[i].instanceDataStepRate = elem->instance_divisor;
146       }
147       else {
148          elements[i].inputSlotClass = SVGA3D_INPUT_PER_VERTEX_DATA;
149          elements[i].instanceDataStepRate = 0;
150       }
151       elements[i].inputRegister = i;
152 
153       if (elements[i].format == SVGA3D_FORMAT_INVALID) {
154          velems->need_swvfetch = TRUE;
155       }
156 
157       if (util_format_is_pure_integer(elem->src_format)) {
158          velems->attrib_is_pure_int |= (1 << i);
159       }
160 
161       if (vf_flags & VF_W_TO_1) {
162          velems->adjust_attrib_w_1 |= (1 << i);
163       }
164 
165       if (vf_flags & VF_U_TO_F_CAST) {
166          velems->adjust_attrib_utof |= (1 << i);
167       }
168       else if (vf_flags & VF_I_TO_F_CAST) {
169          velems->adjust_attrib_itof |= (1 << i);
170       }
171 
172       if (vf_flags & VF_BGRA) {
173          velems->attrib_is_bgra |= (1 << i);
174       }
175 
176       if (vf_flags & VF_PUINT_TO_SNORM) {
177          velems->attrib_puint_to_snorm |= (1 << i);
178       }
179       else if (vf_flags & VF_PUINT_TO_USCALED) {
180          velems->attrib_puint_to_uscaled |= (1 << i);
181       }
182       else if (vf_flags & VF_PUINT_TO_SSCALED) {
183          velems->attrib_puint_to_sscaled |= (1 << i);
184       }
185    }
186 
187    velems->id = util_bitmask_add(svga->input_element_object_id_bm);
188 
189    ret = SVGA3D_vgpu10_DefineElementLayout(svga->swc, velems->count,
190                                            velems->id, elements);
191    if (ret != PIPE_OK) {
192       svga_context_flush(svga, NULL);
193       ret = SVGA3D_vgpu10_DefineElementLayout(svga->swc, velems->count,
194                                               velems->id, elements);
195       assert(ret == PIPE_OK);
196    }
197 }
198 
199 
200 /**
201  * Translate the vertex element types to SVGA3dDeclType and check
202  * for VS-based vertex attribute adjustments.
203  */
204 static void
translate_vertex_decls(struct svga_context * svga,struct svga_velems_state * velems)205 translate_vertex_decls(struct svga_context *svga,
206                        struct svga_velems_state *velems)
207 {
208    unsigned i;
209 
210    assert(!svga_have_vgpu10(svga));
211 
212    for (i = 0; i < velems->count; i++) {
213       const enum pipe_format f = velems->velem[i].src_format;
214       SVGA3dSurfaceFormat svga_format;
215       unsigned vf_flags;
216 
217       svga_translate_vertex_format_vgpu10(f, &svga_format, &vf_flags);
218 
219       velems->decl_type[i] = translate_vertex_format_to_decltype(f);
220       if (velems->decl_type[i] == SVGA3D_DECLTYPE_MAX) {
221          /* Unsupported format - use software fetch */
222          velems->need_swvfetch = TRUE;
223       }
224 
225       /* Check for VS-based adjustments */
226       if (attrib_needs_range_adjustment(f)) {
227          velems->adjust_attrib_range |= (1 << i);
228       }
229 
230       if (vf_flags & VF_W_TO_1) {
231          velems->adjust_attrib_w_1 |= (1 << i);
232       }
233    }
234 }
235 
236 
237 static void *
svga_create_vertex_elements_state(struct pipe_context * pipe,unsigned count,const struct pipe_vertex_element * attribs)238 svga_create_vertex_elements_state(struct pipe_context *pipe,
239                                   unsigned count,
240                                   const struct pipe_vertex_element *attribs)
241 {
242    struct svga_context *svga = svga_context(pipe);
243    struct svga_velems_state *velems;
244 
245    assert(count <= PIPE_MAX_ATTRIBS);
246    velems = (struct svga_velems_state *) MALLOC(sizeof(struct svga_velems_state));
247    if (velems) {
248       velems->count = count;
249       memcpy(velems->velem, attribs, sizeof(*attribs) * count);
250 
251       velems->need_swvfetch = FALSE;
252       velems->adjust_attrib_range = 0x0;
253       velems->attrib_is_pure_int = 0x0;
254       velems->adjust_attrib_w_1 = 0x0;
255       velems->adjust_attrib_itof = 0x0;
256       velems->adjust_attrib_utof = 0x0;
257       velems->attrib_is_bgra = 0x0;
258       velems->attrib_puint_to_snorm = 0x0;
259       velems->attrib_puint_to_uscaled = 0x0;
260       velems->attrib_puint_to_sscaled = 0x0;
261 
262       if (svga_have_vgpu10(svga)) {
263          define_input_element_object(svga, velems);
264       }
265       else {
266          translate_vertex_decls(svga, velems);
267       }
268    }
269 
270    svga->hud.num_vertexelement_objects++;
271    SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,
272                         SVGA_STATS_COUNT_VERTEXELEMENT);
273 
274    return velems;
275 }
276 
277 
278 static void
svga_bind_vertex_elements_state(struct pipe_context * pipe,void * state)279 svga_bind_vertex_elements_state(struct pipe_context *pipe, void *state)
280 {
281    struct svga_context *svga = svga_context(pipe);
282    struct svga_velems_state *velems = (struct svga_velems_state *) state;
283 
284    svga->curr.velems = velems;
285    svga->dirty |= SVGA_NEW_VELEMENT;
286 }
287 
288 
289 static void
svga_delete_vertex_elements_state(struct pipe_context * pipe,void * state)290 svga_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
291 {
292    struct svga_context *svga = svga_context(pipe);
293    struct svga_velems_state *velems = (struct svga_velems_state *) state;
294 
295    if (svga_have_vgpu10(svga)) {
296       enum pipe_error ret;
297 
298       svga_hwtnl_flush_retry(svga);
299 
300       ret = SVGA3D_vgpu10_DestroyElementLayout(svga->swc, velems->id);
301       if (ret != PIPE_OK) {
302          svga_context_flush(svga, NULL);
303          ret = SVGA3D_vgpu10_DestroyElementLayout(svga->swc, velems->id);
304          assert(ret == PIPE_OK);
305       }
306 
307       if (velems->id == svga->state.hw_draw.layout_id)
308          svga->state.hw_draw.layout_id = SVGA3D_INVALID_ID;
309 
310       util_bitmask_clear(svga->input_element_object_id_bm, velems->id);
311       velems->id = SVGA3D_INVALID_ID;
312    }
313 
314    FREE(velems);
315    svga->hud.num_vertexelement_objects--;
316 }
317 
318 
319 void
svga_cleanup_vertex_state(struct svga_context * svga)320 svga_cleanup_vertex_state(struct svga_context *svga)
321 {
322    unsigned i;
323 
324    for (i = 0 ; i < svga->curr.num_vertex_buffers; i++)
325       pipe_vertex_buffer_unreference(&svga->curr.vb[i]);
326 
327    pipe_resource_reference(&svga->state.hw_draw.ib, NULL);
328 
329    for (i = 0; i < svga->state.hw_draw.num_vbuffers; i++)
330       pipe_resource_reference(&svga->state.hw_draw.vbuffers[i], NULL);
331 }
332 
333 
334 void
svga_init_vertex_functions(struct svga_context * svga)335 svga_init_vertex_functions(struct svga_context *svga)
336 {
337    svga->pipe.set_vertex_buffers = svga_set_vertex_buffers;
338    svga->pipe.create_vertex_elements_state = svga_create_vertex_elements_state;
339    svga->pipe.bind_vertex_elements_state = svga_bind_vertex_elements_state;
340    svga->pipe.delete_vertex_elements_state = svga_delete_vertex_elements_state;
341 }
342