41e9809f7447745cff5c80bb28321f31ca8559d9
[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 <pipe/p_context.h>
30 #include <pipe/p_screen.h>
31 #include <util/u_memory.h>
32 #include <util/u_inlines.h>
33 #include <util/u_format.h>
34 #include "vl_vertex_buffers.h"
35 #include "vl_types.h"
36
37 struct vl_vertex_stream
38 {
39 struct vertex2s pos;
40 struct {
41 int8_t y;
42 int8_t cr;
43 int8_t cb;
44 int8_t flag;
45 } eb[2][2];
46 struct vertex2s mv[4];
47 };
48
49 /* vertices for a quad covering a block */
50 static const struct vertex2f block_quad[4] = {
51 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
52 };
53
54 struct pipe_vertex_buffer
55 vl_vb_upload_quads(struct pipe_context *pipe, unsigned blocks_x, unsigned blocks_y)
56 {
57 struct pipe_vertex_buffer quad;
58 struct pipe_transfer *buf_transfer;
59 struct vertex4f *v;
60
61 unsigned x, y, i;
62
63 assert(pipe);
64
65 /* create buffer */
66 quad.stride = sizeof(struct vertex4f);
67 quad.buffer_offset = 0;
68 quad.buffer = pipe_buffer_create
69 (
70 pipe->screen,
71 PIPE_BIND_VERTEX_BUFFER,
72 PIPE_USAGE_STATIC,
73 sizeof(struct vertex4f) * 4 * blocks_x * blocks_y
74 );
75
76 if(!quad.buffer)
77 return quad;
78
79 /* and fill it */
80 v = pipe_buffer_map
81 (
82 pipe,
83 quad.buffer,
84 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
85 &buf_transfer
86 );
87
88 for ( y = 0; y < blocks_y; ++y) {
89 for ( x = 0; x < blocks_x; ++x) {
90 for (i = 0; i < 4; ++i, ++v) {
91 v->x = block_quad[i].x;
92 v->y = block_quad[i].y;
93
94 v->z = x;
95 v->w = y;
96 }
97 }
98 }
99
100 pipe_buffer_unmap(pipe, buf_transfer);
101
102 return quad;
103 }
104
105 static struct pipe_vertex_element
106 vl_vb_get_quad_vertex_element(void)
107 {
108 struct pipe_vertex_element element;
109
110 /* setup rectangle element */
111 element.src_offset = 0;
112 element.instance_divisor = 0;
113 element.vertex_buffer_index = 0;
114 element.src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
115
116 return element;
117 }
118
119 static void
120 vl_vb_element_helper(struct pipe_vertex_element* elements, unsigned num_elements,
121 unsigned vertex_buffer_index)
122 {
123 unsigned i, offset = 0;
124
125 assert(elements && num_elements);
126
127 for ( i = 0; i < num_elements; ++i ) {
128 elements[i].src_offset = offset;
129 elements[i].instance_divisor = 1;
130 elements[i].vertex_buffer_index = vertex_buffer_index;
131 offset += util_format_get_blocksize(elements[i].src_format);
132 }
133 }
134
135 void *
136 vl_vb_get_elems_state(struct pipe_context *pipe, bool include_mvs)
137 {
138 struct pipe_vertex_element vertex_elems[NUM_VS_INPUTS];
139
140 unsigned i;
141
142 memset(&vertex_elems, 0, sizeof(vertex_elems));
143 vertex_elems[VS_I_RECT] = vl_vb_get_quad_vertex_element();
144
145 /* Position element */
146 vertex_elems[VS_I_VPOS].src_format = PIPE_FORMAT_R16G16_SSCALED;
147
148 /* y, cr, cb empty block element top left block */
149 vertex_elems[VS_I_EB_0_0].src_format = PIPE_FORMAT_R8G8B8A8_SSCALED;
150
151 /* y, cr, cb empty block element top right block */
152 vertex_elems[VS_I_EB_0_1].src_format = PIPE_FORMAT_R8G8B8A8_SSCALED;
153
154 /* y, cr, cb empty block element bottom left block */
155 vertex_elems[VS_I_EB_1_0].src_format = PIPE_FORMAT_R8G8B8A8_SSCALED;
156
157 /* y, cr, cb empty block element bottom right block */
158 vertex_elems[VS_I_EB_1_1].src_format = PIPE_FORMAT_R8G8B8A8_SSCALED;
159
160 for (i = 0; i < 4; ++i)
161 /* motion vector 0..4 element */
162 vertex_elems[VS_I_MV0 + i].src_format = PIPE_FORMAT_R16G16_SSCALED;
163
164 vl_vb_element_helper(&vertex_elems[VS_I_VPOS], NUM_VS_INPUTS - (include_mvs ? 1 : 5), 1);
165
166 return pipe->create_vertex_elements_state(pipe, NUM_VS_INPUTS - (include_mvs ? 0 : 4), vertex_elems);
167 }
168
169 struct pipe_vertex_buffer
170 vl_vb_init(struct vl_vertex_buffer *buffer, struct pipe_context *pipe, unsigned size)
171 {
172 struct pipe_vertex_buffer buf;
173
174 assert(buffer);
175
176 buffer->size = size;
177 buffer->num_not_empty = 0;
178 buffer->num_empty = 0;
179
180 buf.stride = sizeof(struct vl_vertex_stream);
181 buf.buffer_offset = 0;
182 buf.buffer = pipe_buffer_create
183 (
184 pipe->screen,
185 PIPE_BIND_VERTEX_BUFFER,
186 PIPE_USAGE_STREAM,
187 sizeof(struct vl_vertex_stream) * size
188 );
189
190 pipe_resource_reference(&buffer->resource, buf.buffer);
191
192 vl_vb_map(buffer, pipe);
193
194 return buf;
195 }
196
197 void
198 vl_vb_map(struct vl_vertex_buffer *buffer, struct pipe_context *pipe)
199 {
200 assert(buffer && pipe);
201
202 buffer->start = pipe_buffer_map
203 (
204 pipe,
205 buffer->resource,
206 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
207 &buffer->transfer
208 );
209 buffer->end = buffer->start + buffer->resource->width0 / sizeof(struct vl_vertex_stream);
210 }
211
212 static void
213 get_motion_vectors(struct pipe_mpeg12_macroblock *mb, struct vertex2s mv[4])
214 {
215 switch (mb->mb_type) {
216 case PIPE_MPEG12_MACROBLOCK_TYPE_BI:
217 {
218 if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME) {
219 mv[2].x = mb->pmv[0][1][0];
220 mv[2].y = mb->pmv[0][1][1];
221
222 } else {
223 mv[2].x = mb->pmv[0][1][0];
224 mv[2].y = mb->pmv[0][1][1] - (mb->pmv[0][1][1] % 4);
225
226 mv[3].x = mb->pmv[1][1][0];
227 mv[3].y = mb->pmv[1][1][1] - (mb->pmv[1][1][1] % 4);
228
229 if(mb->mvfs[0][1]) mv[2].y += 2;
230 if(!mb->mvfs[1][1]) mv[3].y -= 2;
231 }
232
233 /* fall-through */
234 }
235 case PIPE_MPEG12_MACROBLOCK_TYPE_FWD:
236 case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD:
237 {
238 if (mb->mb_type == PIPE_MPEG12_MACROBLOCK_TYPE_BKWD) {
239
240 if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME) {
241 mv[0].x = mb->pmv[0][1][0];
242 mv[0].y = mb->pmv[0][1][1];
243
244 } else {
245 mv[0].x = mb->pmv[0][1][0];
246 mv[0].y = mb->pmv[0][1][1] - (mb->pmv[0][1][1] % 4);
247
248 mv[1].x = mb->pmv[1][1][0];
249 mv[1].y = mb->pmv[1][1][1] - (mb->pmv[1][1][1] % 4);
250
251 if(mb->mvfs[0][1]) mv[0].y += 2;
252 if(!mb->mvfs[1][1]) mv[1].y -= 2;
253 }
254
255 } else {
256
257 if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME) {
258 mv[0].x = mb->pmv[0][0][0];
259 mv[0].y = mb->pmv[0][0][1];
260
261 } else {
262 mv[0].x = mb->pmv[0][0][0];
263 mv[0].y = mb->pmv[0][0][1] - (mb->pmv[0][0][1] % 4);
264
265 mv[1].x = mb->pmv[1][0][0];
266 mv[1].y = mb->pmv[1][0][1] - (mb->pmv[1][0][1] % 4);
267
268 if(mb->mvfs[0][0]) mv[0].y += 2;
269 if(!mb->mvfs[1][0]) mv[1].y -= 2;
270 }
271 }
272 }
273 default:
274 break;
275 }
276 }
277
278 void
279 vl_vb_add_block(struct vl_vertex_buffer *buffer, struct pipe_mpeg12_macroblock *mb,
280 const unsigned (*empty_block_mask)[3][2][2])
281 {
282 struct vl_vertex_stream *stream;
283 unsigned i, j;
284
285 assert(buffer);
286 assert(mb);
287
288 if(mb->cbp)
289 stream = buffer->start + buffer->num_not_empty++;
290 else
291 stream = buffer->end - ++buffer->num_empty;
292
293 stream->pos.x = mb->mbx;
294 stream->pos.y = mb->mby;
295
296 for ( i = 0; i < 2; ++i) {
297 for ( j = 0; j < 2; ++j) {
298 stream->eb[i][j].y = !(mb->cbp & (*empty_block_mask)[0][i][j]);
299 stream->eb[i][j].cr = !(mb->cbp & (*empty_block_mask)[1][i][j]);
300 stream->eb[i][j].cb = !(mb->cbp & (*empty_block_mask)[2][i][j]);
301 }
302 }
303 stream->eb[0][0].flag = mb->dct_type == PIPE_MPEG12_DCT_TYPE_FIELD;
304 stream->eb[0][1].flag = mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME;
305 stream->eb[1][0].flag = mb->mb_type == PIPE_MPEG12_MACROBLOCK_TYPE_BKWD;
306 switch (mb->mb_type) {
307 case PIPE_MPEG12_MACROBLOCK_TYPE_INTRA:
308 stream->eb[1][1].flag = -1;
309 break;
310
311 case PIPE_MPEG12_MACROBLOCK_TYPE_FWD:
312 case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD:
313 stream->eb[1][1].flag = 1;
314 break;
315
316 case PIPE_MPEG12_MACROBLOCK_TYPE_BI:
317 stream->eb[1][1].flag = 0;
318 break;
319
320 default:
321 assert(0);
322 }
323
324 get_motion_vectors(mb, stream->mv);
325 }
326
327 void
328 vl_vb_unmap(struct vl_vertex_buffer *buffer, struct pipe_context *pipe)
329 {
330 assert(buffer && pipe);
331
332 pipe_buffer_unmap(pipe, buffer->transfer);
333 }
334
335 void
336 vl_vb_restart(struct vl_vertex_buffer *buffer,
337 unsigned *not_empty_start_instance, unsigned *not_empty_num_instances,
338 unsigned *empty_start_instance, unsigned *empty_num_instances)
339 {
340 assert(buffer);
341
342 *not_empty_start_instance = 0;
343 *not_empty_num_instances = buffer->num_not_empty;
344 *empty_start_instance = buffer->size - buffer->num_empty;
345 *empty_num_instances = buffer->num_empty;
346
347 buffer->num_not_empty = 0;
348 buffer->num_empty = 0;
349 }
350
351 void
352 vl_vb_cleanup(struct vl_vertex_buffer *buffer)
353 {
354 assert(buffer);
355
356 pipe_resource_reference(&buffer->resource, NULL);
357 }