svga: compute need_swvfetch in svga_create_vertex_elements_state()
[mesa.git] / src / gallium / drivers / svga / svga_pipe_vertex.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_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"
33
34 #include "svga_screen.h"
35 #include "svga_resource_buffer.h"
36 #include "svga_context.h"
37
38
39 static void svga_set_vertex_buffers(struct pipe_context *pipe,
40 unsigned start_slot, unsigned count,
41 const struct pipe_vertex_buffer *buffers)
42 {
43 struct svga_context *svga = svga_context(pipe);
44
45 util_set_vertex_buffers_count(svga->curr.vb,
46 &svga->curr.num_vertex_buffers,
47 buffers, start_slot, count);
48
49 svga->dirty |= SVGA_NEW_VBUFFER;
50 }
51
52
53 static void svga_set_index_buffer(struct pipe_context *pipe,
54 const struct pipe_index_buffer *ib)
55 {
56 struct svga_context *svga = svga_context(pipe);
57
58 if (ib) {
59 pipe_resource_reference(&svga->curr.ib.buffer, ib->buffer);
60 memcpy(&svga->curr.ib, ib, sizeof(svga->curr.ib));
61 }
62 else {
63 pipe_resource_reference(&svga->curr.ib.buffer, NULL);
64 memset(&svga->curr.ib, 0, sizeof(svga->curr.ib));
65 }
66
67 /* TODO make this more like a state */
68 }
69
70
71 /**
72 * Given a gallium vertex element format, return the corresponding SVGA3D
73 * format. Return SVGA3D_DECLTYPE_MAX for unsupported gallium formats.
74 */
75 static SVGA3dDeclType
76 translate_vertex_format(enum pipe_format format)
77 {
78 switch (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;
96
97 /* See attrib_needs_adjustment() and attrib_needs_w_to_1() below */
98 case PIPE_FORMAT_R8G8B8_SNORM: return SVGA3D_DECLTYPE_UBYTE4N;
99
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;
104
105 default:
106 /* There are many formats without hardware support. This case
107 * will be hit regularly, meaning we'll need swvfetch.
108 */
109 return SVGA3D_DECLTYPE_MAX;
110 }
111 }
112
113
114 /**
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.
118 */
119 static boolean
120 attrib_needs_range_adjustment(enum pipe_format format)
121 {
122 switch (format) {
123 case PIPE_FORMAT_R8G8B8_SNORM:
124 return TRUE;
125 default:
126 return FALSE;
127 }
128 }
129
130
131 /**
132 * Does the given vertex attrib format need to have the W component set
133 * to one in the VS?
134 */
135 static boolean
136 attrib_needs_w_to_1(enum pipe_format format)
137 {
138 switch (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:
143 return TRUE;
144 default:
145 return FALSE;
146 }
147 }
148
149
150 static void *
151 svga_create_vertex_elements_state(struct pipe_context *pipe,
152 unsigned count,
153 const struct pipe_vertex_element *attribs)
154 {
155 struct svga_velems_state *velems;
156 assert(count <= PIPE_MAX_ATTRIBS);
157 velems = (struct svga_velems_state *) MALLOC(sizeof(struct svga_velems_state));
158 if (velems) {
159 unsigned i;
160
161 velems->count = count;
162 memcpy(velems->velem, attribs, sizeof(*attribs) * count);
163
164 velems->need_swvfetch = FALSE;
165 velems->adjust_attrib_range = 0x0;
166 velems->adjust_attrib_w_1 = 0x0;
167
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;
175 break;
176 }
177
178 if (attrib_needs_range_adjustment(f)) {
179 velems->adjust_attrib_range |= (1 << i);
180 }
181 if (attrib_needs_w_to_1(f)) {
182 velems->adjust_attrib_w_1 |= (1 << i);
183 }
184 }
185 }
186 return velems;
187 }
188
189 static void svga_bind_vertex_elements_state(struct pipe_context *pipe,
190 void *velems)
191 {
192 struct svga_context *svga = svga_context(pipe);
193 struct svga_velems_state *svga_velems = (struct svga_velems_state *) velems;
194
195 svga->curr.velems = svga_velems;
196 svga->dirty |= SVGA_NEW_VELEMENT;
197 }
198
199 static void svga_delete_vertex_elements_state(struct pipe_context *pipe,
200 void *velems)
201 {
202 FREE(velems);
203 }
204
205 void svga_cleanup_vertex_state( struct svga_context *svga )
206 {
207 unsigned i;
208
209 for (i = 0 ; i < svga->curr.num_vertex_buffers; i++)
210 pipe_resource_reference(&svga->curr.vb[i].buffer, NULL);
211 }
212
213
214 void svga_init_vertex_functions( struct svga_context *svga )
215 {
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;
221 }
222
223