• 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/format/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    unsigned i;
124 
125    assert(velems->count <= PIPE_MAX_ATTRIBS);
126    assert(svga_have_vgpu10(svga));
127 
128    for (i = 0; i < velems->count; i++) {
129       const struct pipe_vertex_element *elem = velems->velem + i;
130       SVGA3dSurfaceFormat svga_format;
131       unsigned vf_flags;
132 
133       svga_translate_vertex_format_vgpu10(elem->src_format,
134                                           &svga_format, &vf_flags);
135 
136       velems->decl_type[i] =
137          translate_vertex_format_to_decltype(elem->src_format);
138       elements[i].inputSlot = elem->vertex_buffer_index;
139       elements[i].alignedByteOffset = elem->src_offset;
140       elements[i].format = svga_format;
141 
142       if (elem->instance_divisor) {
143          elements[i].inputSlotClass = SVGA3D_INPUT_PER_INSTANCE_DATA;
144          elements[i].instanceDataStepRate = elem->instance_divisor;
145       }
146       else {
147          elements[i].inputSlotClass = SVGA3D_INPUT_PER_VERTEX_DATA;
148          elements[i].instanceDataStepRate = 0;
149       }
150       elements[i].inputRegister = i;
151 
152       if (elements[i].format == SVGA3D_FORMAT_INVALID) {
153          velems->need_swvfetch = TRUE;
154       }
155 
156       if (util_format_is_pure_integer(elem->src_format)) {
157          velems->attrib_is_pure_int |= (1 << i);
158       }
159 
160       if (vf_flags & VF_W_TO_1) {
161          velems->adjust_attrib_w_1 |= (1 << i);
162       }
163 
164       if (vf_flags & VF_U_TO_F_CAST) {
165          velems->adjust_attrib_utof |= (1 << i);
166       }
167       else if (vf_flags & VF_I_TO_F_CAST) {
168          velems->adjust_attrib_itof |= (1 << i);
169       }
170 
171       if (vf_flags & VF_BGRA) {
172          velems->attrib_is_bgra |= (1 << i);
173       }
174 
175       if (vf_flags & VF_PUINT_TO_SNORM) {
176          velems->attrib_puint_to_snorm |= (1 << i);
177       }
178       else if (vf_flags & VF_PUINT_TO_USCALED) {
179          velems->attrib_puint_to_uscaled |= (1 << i);
180       }
181       else if (vf_flags & VF_PUINT_TO_SSCALED) {
182          velems->attrib_puint_to_sscaled |= (1 << i);
183       }
184    }
185 
186    velems->id = util_bitmask_add(svga->input_element_object_id_bm);
187 
188    SVGA_RETRY(svga, SVGA3D_vgpu10_DefineElementLayout(svga->swc, velems->count,
189                                                       velems->id, elements));
190 }
191 
192 
193 /**
194  * Translate the vertex element types to SVGA3dDeclType and check
195  * for VS-based vertex attribute adjustments.
196  */
197 static void
translate_vertex_decls(struct svga_context * svga,struct svga_velems_state * velems)198 translate_vertex_decls(struct svga_context *svga,
199                        struct svga_velems_state *velems)
200 {
201    unsigned i;
202 
203    assert(!svga_have_vgpu10(svga));
204 
205    for (i = 0; i < velems->count; i++) {
206       const enum pipe_format f = velems->velem[i].src_format;
207       SVGA3dSurfaceFormat svga_format;
208       unsigned vf_flags;
209 
210       svga_translate_vertex_format_vgpu10(f, &svga_format, &vf_flags);
211 
212       velems->decl_type[i] = translate_vertex_format_to_decltype(f);
213       if (velems->decl_type[i] == SVGA3D_DECLTYPE_MAX) {
214          /* Unsupported format - use software fetch */
215          velems->need_swvfetch = TRUE;
216       }
217 
218       /* Check for VS-based adjustments */
219       if (attrib_needs_range_adjustment(f)) {
220          velems->adjust_attrib_range |= (1 << i);
221       }
222 
223       if (vf_flags & VF_W_TO_1) {
224          velems->adjust_attrib_w_1 |= (1 << i);
225       }
226    }
227 }
228 
229 
230 static void *
svga_create_vertex_elements_state(struct pipe_context * pipe,unsigned count,const struct pipe_vertex_element * attribs)231 svga_create_vertex_elements_state(struct pipe_context *pipe,
232                                   unsigned count,
233                                   const struct pipe_vertex_element *attribs)
234 {
235    struct svga_context *svga = svga_context(pipe);
236    struct svga_velems_state *velems;
237 
238    assert(count <= PIPE_MAX_ATTRIBS);
239    velems = (struct svga_velems_state *) MALLOC(sizeof(struct svga_velems_state));
240    if (velems) {
241       velems->count = count;
242       memcpy(velems->velem, attribs, sizeof(*attribs) * count);
243 
244       velems->need_swvfetch = FALSE;
245       velems->adjust_attrib_range = 0x0;
246       velems->attrib_is_pure_int = 0x0;
247       velems->adjust_attrib_w_1 = 0x0;
248       velems->adjust_attrib_itof = 0x0;
249       velems->adjust_attrib_utof = 0x0;
250       velems->attrib_is_bgra = 0x0;
251       velems->attrib_puint_to_snorm = 0x0;
252       velems->attrib_puint_to_uscaled = 0x0;
253       velems->attrib_puint_to_sscaled = 0x0;
254 
255       if (svga_have_vgpu10(svga)) {
256          define_input_element_object(svga, velems);
257       }
258       else {
259          translate_vertex_decls(svga, velems);
260       }
261    }
262 
263    svga->hud.num_vertexelement_objects++;
264    SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,
265                         SVGA_STATS_COUNT_VERTEXELEMENT);
266 
267    return velems;
268 }
269 
270 
271 static void
svga_bind_vertex_elements_state(struct pipe_context * pipe,void * state)272 svga_bind_vertex_elements_state(struct pipe_context *pipe, void *state)
273 {
274    struct svga_context *svga = svga_context(pipe);
275    struct svga_velems_state *velems = (struct svga_velems_state *) state;
276 
277    svga->curr.velems = velems;
278    svga->dirty |= SVGA_NEW_VELEMENT;
279 }
280 
281 
282 static void
svga_delete_vertex_elements_state(struct pipe_context * pipe,void * state)283 svga_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
284 {
285    struct svga_context *svga = svga_context(pipe);
286    struct svga_velems_state *velems = (struct svga_velems_state *) state;
287 
288    if (svga_have_vgpu10(svga)) {
289       svga_hwtnl_flush_retry(svga);
290 
291       SVGA_RETRY(svga, SVGA3D_vgpu10_DestroyElementLayout(svga->swc,
292                                                           velems->id));
293 
294       if (velems->id == svga->state.hw_draw.layout_id)
295          svga->state.hw_draw.layout_id = SVGA3D_INVALID_ID;
296 
297       util_bitmask_clear(svga->input_element_object_id_bm, velems->id);
298       velems->id = SVGA3D_INVALID_ID;
299    }
300 
301    FREE(velems);
302    svga->hud.num_vertexelement_objects--;
303 }
304 
305 
306 void
svga_cleanup_vertex_state(struct svga_context * svga)307 svga_cleanup_vertex_state(struct svga_context *svga)
308 {
309    unsigned i;
310 
311    for (i = 0 ; i < svga->curr.num_vertex_buffers; i++)
312       pipe_vertex_buffer_unreference(&svga->curr.vb[i]);
313 
314    pipe_resource_reference(&svga->state.hw_draw.ib, NULL);
315 
316    for (i = 0; i < svga->state.hw_draw.num_vbuffers; i++)
317       pipe_resource_reference(&svga->state.hw_draw.vbuffers[i], NULL);
318 }
319 
320 
321 void
svga_init_vertex_functions(struct svga_context * svga)322 svga_init_vertex_functions(struct svga_context *svga)
323 {
324    svga->pipe.set_vertex_buffers = svga_set_vertex_buffers;
325    svga->pipe.create_vertex_elements_state = svga_create_vertex_elements_state;
326    svga->pipe.bind_vertex_elements_state = svga_bind_vertex_elements_state;
327    svga->pipe.delete_vertex_elements_state = svga_delete_vertex_elements_state;
328 }
329