1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
26 #include "util/u_helpers.h"
27 #include "util/u_inlines.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_transfer.h"
32 #include "tgsi/tgsi_parse.h"
34 #include "svga_screen.h"
35 #include "svga_resource_buffer.h"
36 #include "svga_context.h"
39 static void svga_set_vertex_buffers(struct pipe_context
*pipe
,
40 unsigned start_slot
, unsigned count
,
41 const struct pipe_vertex_buffer
*buffers
)
43 struct svga_context
*svga
= svga_context(pipe
);
45 util_set_vertex_buffers_count(svga
->curr
.vb
,
46 &svga
->curr
.num_vertex_buffers
,
47 buffers
, start_slot
, count
);
49 svga
->dirty
|= SVGA_NEW_VBUFFER
;
53 static void svga_set_index_buffer(struct pipe_context
*pipe
,
54 const struct pipe_index_buffer
*ib
)
56 struct svga_context
*svga
= svga_context(pipe
);
59 pipe_resource_reference(&svga
->curr
.ib
.buffer
, ib
->buffer
);
60 memcpy(&svga
->curr
.ib
, ib
, sizeof(svga
->curr
.ib
));
63 pipe_resource_reference(&svga
->curr
.ib
.buffer
, NULL
);
64 memset(&svga
->curr
.ib
, 0, sizeof(svga
->curr
.ib
));
67 /* TODO make this more like a state */
72 * Given a gallium vertex element format, return the corresponding SVGA3D
73 * format. Return SVGA3D_DECLTYPE_MAX for unsupported gallium formats.
76 translate_vertex_format(enum pipe_format format
)
79 case PIPE_FORMAT_R32_FLOAT
: return SVGA3D_DECLTYPE_FLOAT1
;
80 case PIPE_FORMAT_R32G32_FLOAT
: return SVGA3D_DECLTYPE_FLOAT2
;
81 case PIPE_FORMAT_R32G32B32_FLOAT
: return SVGA3D_DECLTYPE_FLOAT3
;
82 case PIPE_FORMAT_R32G32B32A32_FLOAT
: return SVGA3D_DECLTYPE_FLOAT4
;
83 case PIPE_FORMAT_B8G8R8A8_UNORM
: return SVGA3D_DECLTYPE_D3DCOLOR
;
84 case PIPE_FORMAT_R8G8B8A8_USCALED
: return SVGA3D_DECLTYPE_UBYTE4
;
85 case PIPE_FORMAT_R16G16_SSCALED
: return SVGA3D_DECLTYPE_SHORT2
;
86 case PIPE_FORMAT_R16G16B16A16_SSCALED
: return SVGA3D_DECLTYPE_SHORT4
;
87 case PIPE_FORMAT_R8G8B8A8_UNORM
: return SVGA3D_DECLTYPE_UBYTE4N
;
88 case PIPE_FORMAT_R16G16_SNORM
: return SVGA3D_DECLTYPE_SHORT2N
;
89 case PIPE_FORMAT_R16G16B16A16_SNORM
: return SVGA3D_DECLTYPE_SHORT4N
;
90 case PIPE_FORMAT_R16G16_UNORM
: return SVGA3D_DECLTYPE_USHORT2N
;
91 case PIPE_FORMAT_R16G16B16A16_UNORM
: return SVGA3D_DECLTYPE_USHORT4N
;
92 case PIPE_FORMAT_R10G10B10X2_USCALED
: return SVGA3D_DECLTYPE_UDEC3
;
93 case PIPE_FORMAT_R10G10B10X2_SNORM
: return SVGA3D_DECLTYPE_DEC3N
;
94 case PIPE_FORMAT_R16G16_FLOAT
: return SVGA3D_DECLTYPE_FLOAT16_2
;
95 case PIPE_FORMAT_R16G16B16A16_FLOAT
: return SVGA3D_DECLTYPE_FLOAT16_4
;
97 /* See attrib_needs_adjustment() and attrib_needs_w_to_1() below */
98 case PIPE_FORMAT_R8G8B8_SNORM
: return SVGA3D_DECLTYPE_UBYTE4N
;
100 /* See attrib_needs_w_to_1() below */
101 case PIPE_FORMAT_R16G16B16_SNORM
: return SVGA3D_DECLTYPE_SHORT4N
;
102 case PIPE_FORMAT_R16G16B16_UNORM
: return SVGA3D_DECLTYPE_USHORT4N
;
103 case PIPE_FORMAT_R8G8B8_UNORM
: return SVGA3D_DECLTYPE_UBYTE4N
;
106 /* There are many formats without hardware support. This case
107 * will be hit regularly, meaning we'll need swvfetch.
109 return SVGA3D_DECLTYPE_MAX
;
115 * Does the given vertex attrib format need range adjustment in the VS?
116 * Range adjustment scales and biases values from [0,1] to [-1,1].
117 * This lets us avoid the swtnl path.
120 attrib_needs_range_adjustment(enum pipe_format format
)
123 case PIPE_FORMAT_R8G8B8_SNORM
:
132 * Does the given vertex attrib format need to have the W component set
136 attrib_needs_w_to_1(enum pipe_format format
)
139 case PIPE_FORMAT_R8G8B8_SNORM
:
140 case PIPE_FORMAT_R8G8B8_UNORM
:
141 case PIPE_FORMAT_R16G16B16_SNORM
:
142 case PIPE_FORMAT_R16G16B16_UNORM
:
151 svga_create_vertex_elements_state(struct pipe_context
*pipe
,
153 const struct pipe_vertex_element
*attribs
)
155 struct svga_velems_state
*velems
;
156 assert(count
<= PIPE_MAX_ATTRIBS
);
157 velems
= (struct svga_velems_state
*) MALLOC(sizeof(struct svga_velems_state
));
161 velems
->count
= count
;
162 memcpy(velems
->velem
, attribs
, sizeof(*attribs
) * count
);
164 velems
->need_swvfetch
= FALSE
;
165 velems
->adjust_attrib_range
= 0x0;
166 velems
->adjust_attrib_w_1
= 0x0;
168 /* Translate Gallium vertex format to SVGA3dDeclType */
169 for (i
= 0; i
< count
; i
++) {
170 enum pipe_format f
= attribs
[i
].src_format
;
171 velems
->decl_type
[i
] = translate_vertex_format(f
);
172 if (velems
->decl_type
[i
] == SVGA3D_DECLTYPE_MAX
) {
173 /* Unsupported format - use software fetch */
174 velems
->need_swvfetch
= TRUE
;
178 if (attrib_needs_range_adjustment(f
)) {
179 velems
->adjust_attrib_range
|= (1 << i
);
181 if (attrib_needs_w_to_1(f
)) {
182 velems
->adjust_attrib_w_1
|= (1 << i
);
189 static void svga_bind_vertex_elements_state(struct pipe_context
*pipe
,
192 struct svga_context
*svga
= svga_context(pipe
);
193 struct svga_velems_state
*svga_velems
= (struct svga_velems_state
*) velems
;
195 svga
->curr
.velems
= svga_velems
;
196 svga
->dirty
|= SVGA_NEW_VELEMENT
;
199 static void svga_delete_vertex_elements_state(struct pipe_context
*pipe
,
205 void svga_cleanup_vertex_state( struct svga_context
*svga
)
209 for (i
= 0 ; i
< svga
->curr
.num_vertex_buffers
; i
++)
210 pipe_resource_reference(&svga
->curr
.vb
[i
].buffer
, NULL
);
214 void svga_init_vertex_functions( struct svga_context
*svga
)
216 svga
->pipe
.set_vertex_buffers
= svga_set_vertex_buffers
;
217 svga
->pipe
.set_index_buffer
= svga_set_index_buffer
;
218 svga
->pipe
.create_vertex_elements_state
= svga_create_vertex_elements_state
;
219 svga
->pipe
.bind_vertex_elements_state
= svga_bind_vertex_elements_state
;
220 svga
->pipe
.delete_vertex_elements_state
= svga_delete_vertex_elements_state
;