[g3dvl] give mc and ycbcr stage its own vertex element state
[mesa.git] / src / gallium / auxiliary / vl / vl_vertex_buffers.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Christian König
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <assert.h>
29 #include <util/u_format.h>
30 #include "vl_vertex_buffers.h"
31 #include "vl_types.h"
32
33 struct vl_vertex_stream
34 {
35 struct vertex2s pos;
36 uint8_t mb_type_intra;
37 uint8_t dct_type_field;
38 uint8_t dummy[2];
39 uint8_t eb[3][2][2];
40 struct vertex4s mv[4];
41 };
42
43 /* vertices for a quad covering a block */
44 static const struct vertex2f block_quad[4] = {
45 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
46 };
47
48 struct pipe_vertex_buffer
49 vl_vb_upload_quads(struct pipe_context *pipe, unsigned blocks_x, unsigned blocks_y)
50 {
51 struct pipe_vertex_buffer quad;
52 struct pipe_transfer *buf_transfer;
53 struct vertex4f *v;
54
55 unsigned x, y, i;
56
57 assert(pipe);
58
59 /* create buffer */
60 quad.stride = sizeof(struct vertex4f);
61 quad.buffer_offset = 0;
62 quad.buffer = pipe_buffer_create
63 (
64 pipe->screen,
65 PIPE_BIND_VERTEX_BUFFER,
66 PIPE_USAGE_STATIC,
67 sizeof(struct vertex4f) * 4 * blocks_x * blocks_y
68 );
69
70 if(!quad.buffer)
71 return quad;
72
73 /* and fill it */
74 v = pipe_buffer_map
75 (
76 pipe,
77 quad.buffer,
78 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
79 &buf_transfer
80 );
81
82 for ( y = 0; y < blocks_y; ++y) {
83 for ( x = 0; x < blocks_x; ++x) {
84 for (i = 0; i < 4; ++i, ++v) {
85 v->x = block_quad[i].x;
86 v->y = block_quad[i].y;
87
88 v->z = x;
89 v->w = y;
90 }
91 }
92 }
93
94 pipe_buffer_unmap(pipe, buf_transfer);
95
96 return quad;
97 }
98
99 static struct pipe_vertex_element
100 vl_vb_get_quad_vertex_element(void)
101 {
102 struct pipe_vertex_element element;
103
104 /* setup rectangle element */
105 element.src_offset = 0;
106 element.instance_divisor = 0;
107 element.vertex_buffer_index = 0;
108 element.src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
109
110 return element;
111 }
112
113 static void
114 vl_vb_element_helper(struct pipe_vertex_element* elements, unsigned num_elements,
115 unsigned vertex_buffer_index)
116 {
117 unsigned i, offset = 0;
118
119 assert(elements && num_elements);
120
121 for ( i = 0; i < num_elements; ++i ) {
122 if (elements[i].src_offset)
123 offset = elements[i].src_offset;
124 else
125 elements[i].src_offset = offset;
126 elements[i].instance_divisor = 1;
127 elements[i].vertex_buffer_index = vertex_buffer_index;
128 offset += util_format_get_blocksize(elements[i].src_format);
129 }
130 }
131
132 void *
133 vl_vb_get_ves_eb(struct pipe_context *pipe, int component)
134 {
135 struct pipe_vertex_element vertex_elems[NUM_VS_INPUTS];
136
137 assert(pipe);
138
139 memset(&vertex_elems, 0, sizeof(vertex_elems));
140 vertex_elems[VS_I_RECT] = vl_vb_get_quad_vertex_element();
141
142 /* Position element */
143 vertex_elems[VS_I_VPOS].src_format = PIPE_FORMAT_R16G16_SSCALED;
144
145 /* flags */
146 vertex_elems[VS_I_FLAGS].src_format = PIPE_FORMAT_R8G8B8A8_USCALED;
147
148 /* empty block element of selected component */
149 vertex_elems[VS_I_EB].src_offset = offsetof(struct vl_vertex_stream, eb[component]);
150 vertex_elems[VS_I_EB].src_format = PIPE_FORMAT_R8G8B8A8_USCALED;
151
152 vl_vb_element_helper(&vertex_elems[VS_I_VPOS], NUM_VS_INPUTS - 1, 1);
153
154 return pipe->create_vertex_elements_state(pipe, NUM_VS_INPUTS, vertex_elems);
155 }
156
157 void *
158 vl_vb_get_ves_mv(struct pipe_context *pipe, int motionvector)
159 {
160 struct pipe_vertex_element vertex_elems[NUM_VS_INPUTS];
161
162 memset(&vertex_elems, 0, sizeof(vertex_elems));
163 vertex_elems[VS_I_RECT] = vl_vb_get_quad_vertex_element();
164
165 assert(pipe);
166
167 /* Position element */
168 vertex_elems[VS_I_VPOS].src_format = PIPE_FORMAT_R16G16_SSCALED;
169
170 /* motion vector TOP element */
171 vertex_elems[VS_I_MV_TOP].src_offset = offsetof(struct vl_vertex_stream, mv[motionvector * 2]);
172 vertex_elems[VS_I_MV_TOP].src_format = PIPE_FORMAT_R16G16B16A16_SSCALED;
173
174 /* motion vector BOTTOM element */
175 vertex_elems[VS_I_MV_BOTTOM].src_format = PIPE_FORMAT_R16G16B16A16_SSCALED;
176
177 vl_vb_element_helper(&vertex_elems[VS_I_VPOS], NUM_VS_INPUTS - 1, 1);
178
179 return pipe->create_vertex_elements_state(pipe, NUM_VS_INPUTS, vertex_elems);
180 }
181
182 struct pipe_vertex_buffer
183 vl_vb_init(struct vl_vertex_buffer *buffer, struct pipe_context *pipe, unsigned size)
184 {
185 struct pipe_vertex_buffer buf;
186
187 assert(buffer);
188
189 buffer->size = size;
190 buffer->num_not_empty = 0;
191 buffer->num_empty = 0;
192
193 buf.stride = sizeof(struct vl_vertex_stream);
194 buf.buffer_offset = 0;
195 buf.buffer = pipe_buffer_create
196 (
197 pipe->screen,
198 PIPE_BIND_VERTEX_BUFFER,
199 PIPE_USAGE_STREAM,
200 sizeof(struct vl_vertex_stream) * size
201 );
202
203 pipe_resource_reference(&buffer->resource, buf.buffer);
204
205 vl_vb_map(buffer, pipe);
206
207 return buf;
208 }
209
210 void
211 vl_vb_map(struct vl_vertex_buffer *buffer, struct pipe_context *pipe)
212 {
213 assert(buffer && pipe);
214
215 buffer->start = pipe_buffer_map
216 (
217 pipe,
218 buffer->resource,
219 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
220 &buffer->transfer
221 );
222 buffer->end = buffer->start + buffer->resource->width0 / sizeof(struct vl_vertex_stream);
223 }
224
225 static void
226 get_motion_vectors(struct pipe_mpeg12_macroblock *mb, struct vertex4s mv[4])
227 {
228 if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME) {
229 mv[0].x = mv[1].x = mb->mv[0].top.x;
230 mv[0].y = mv[1].y = mb->mv[0].top.y;
231 mv[0].z = 0; mv[1].z = 1;
232
233 mv[2].x = mv[3].x = mb->mv[1].top.x;
234 mv[2].y = mv[3].y = mb->mv[1].top.y;
235 mv[2].z = 0; mv[3].z = 1;
236
237 } else {
238 mv[0].x = mb->mv[0].top.x;
239 mv[0].y = mb->mv[0].top.y - (mb->mv[0].top.y % 4);
240 mv[0].z = mb->mv[0].top.field_select;
241
242 mv[1].x = mb->mv[0].bottom.x;
243 mv[1].y = mb->mv[0].bottom.y - (mb->mv[0].bottom.y % 4);
244 mv[1].z = mb->mv[0].bottom.field_select;
245
246 if (mb->mv[0].top.field_select) mv[0].y += 2;
247 if (!mb->mv[0].bottom.field_select) mv[1].y -= 2;
248
249 mv[2].x = mb->mv[1].top.x;
250 mv[2].y = mb->mv[1].top.y - (mb->mv[1].top.y % 4);
251 mv[2].z = mb->mv[1].top.field_select;
252
253 mv[3].x = mb->mv[1].bottom.x;
254 mv[3].y = mb->mv[1].bottom.y - (mb->mv[1].bottom.y % 4);
255 mv[3].z = mb->mv[1].bottom.field_select;
256
257 if (mb->mv[1].top.field_select) mv[2].y += 2;
258 if (!mb->mv[1].bottom.field_select) mv[3].y -= 2;
259 }
260
261 mv[0].w = mv[1].w = mb->mv[0].wheight;
262 mv[2].w = mv[3].w = mb->mv[1].wheight;
263 }
264
265 void
266 vl_vb_add_block(struct vl_vertex_buffer *buffer, struct pipe_mpeg12_macroblock *mb,
267 const unsigned (*empty_block_mask)[3][2][2])
268 {
269 struct vl_vertex_stream *stream;
270 unsigned i, j, k;
271
272 assert(buffer);
273 assert(mb);
274 assert(buffer->num_not_empty + buffer->num_empty < buffer->size);
275
276 if(mb->cbp)
277 stream = buffer->start + buffer->num_not_empty++;
278 else
279 stream = buffer->end - ++buffer->num_empty;
280
281 stream->pos.x = mb->mbx;
282 stream->pos.y = mb->mby;
283
284 for ( i = 0; i < 3; ++i)
285 for ( j = 0; j < 2; ++j)
286 for ( k = 0; k < 2; ++k)
287 stream->eb[i][j][k] = !(mb->cbp & (*empty_block_mask)[i][j][k]);
288
289 stream->dct_type_field = mb->dct_type == PIPE_MPEG12_DCT_TYPE_FIELD;
290 stream->mb_type_intra = mb->dct_intra;
291
292 get_motion_vectors(mb, stream->mv);
293 }
294
295 void
296 vl_vb_unmap(struct vl_vertex_buffer *buffer, struct pipe_context *pipe)
297 {
298 assert(buffer && pipe);
299
300 pipe_buffer_unmap(pipe, buffer->transfer);
301 }
302
303 void
304 vl_vb_restart(struct vl_vertex_buffer *buffer,
305 unsigned *not_empty_start_instance, unsigned *not_empty_num_instances,
306 unsigned *empty_start_instance, unsigned *empty_num_instances)
307 {
308 assert(buffer);
309
310 *not_empty_start_instance = 0;
311 *not_empty_num_instances = buffer->num_not_empty;
312 *empty_start_instance = buffer->size - buffer->num_empty;
313 *empty_num_instances = buffer->num_empty;
314
315 buffer->num_not_empty = 0;
316 buffer->num_empty = 0;
317 }
318
319 void
320 vl_vb_cleanup(struct vl_vertex_buffer *buffer)
321 {
322 assert(buffer);
323
324 pipe_resource_reference(&buffer->resource, NULL);
325 }