d679ad3bdf3bc577c8593c3ea2577f58b527ad21
[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() below */
98 case PIPE_FORMAT_R8G8B8_SNORM: return SVGA3D_DECLTYPE_UBYTE4N;
99
100 default:
101 /* There are many formats without hardware support. This case
102 * will be hit regularly, meaning we'll need swvfetch.
103 */
104 return SVGA3D_DECLTYPE_MAX;
105 }
106 }
107
108
109 /**
110 * Does the given vertex attrib format need range adjustment in the VS?
111 * Range adjustment scales and biases values from [0,1] to [-1,1].
112 * This lets us avoid the swtnl path.
113 */
114 static boolean
115 attrib_needs_range_adjustment(enum pipe_format format)
116 {
117 switch (format) {
118 case PIPE_FORMAT_R8G8B8_SNORM:
119 return TRUE;
120 default:
121 return FALSE;
122 }
123 }
124
125
126 static void *
127 svga_create_vertex_elements_state(struct pipe_context *pipe,
128 unsigned count,
129 const struct pipe_vertex_element *attribs)
130 {
131 struct svga_velems_state *velems;
132 assert(count <= PIPE_MAX_ATTRIBS);
133 velems = (struct svga_velems_state *) MALLOC(sizeof(struct svga_velems_state));
134 if (velems) {
135 unsigned i;
136
137 velems->count = count;
138 memcpy(velems->velem, attribs, sizeof(*attribs) * count);
139
140 velems->adjust_attrib_range = 0x0;
141
142 /* Translate Gallium vertex format to SVGA3dDeclType */
143 for (i = 0; i < count; i++) {
144 enum pipe_format f = attribs[i].src_format;
145 velems->decl_type[i] = translate_vertex_format(f);
146
147 if (attrib_needs_range_adjustment(f)) {
148 velems->adjust_attrib_range |= (1 << i);
149 }
150 }
151 }
152 return velems;
153 }
154
155 static void svga_bind_vertex_elements_state(struct pipe_context *pipe,
156 void *velems)
157 {
158 struct svga_context *svga = svga_context(pipe);
159 struct svga_velems_state *svga_velems = (struct svga_velems_state *) velems;
160
161 svga->curr.velems = svga_velems;
162 svga->dirty |= SVGA_NEW_VELEMENT;
163 }
164
165 static void svga_delete_vertex_elements_state(struct pipe_context *pipe,
166 void *velems)
167 {
168 FREE(velems);
169 }
170
171 void svga_cleanup_vertex_state( struct svga_context *svga )
172 {
173 unsigned i;
174
175 for (i = 0 ; i < svga->curr.num_vertex_buffers; i++)
176 pipe_resource_reference(&svga->curr.vb[i].buffer, NULL);
177 }
178
179
180 void svga_init_vertex_functions( struct svga_context *svga )
181 {
182 svga->pipe.set_vertex_buffers = svga_set_vertex_buffers;
183 svga->pipe.set_index_buffer = svga_set_index_buffer;
184 svga->pipe.create_vertex_elements_state = svga_create_vertex_elements_state;
185 svga->pipe.bind_vertex_elements_state = svga_bind_vertex_elements_state;
186 svga->pipe.delete_vertex_elements_state = svga_delete_vertex_elements_state;
187 }
188
189