1 /**************************************************************************
3 * Copyright 2010 Christian König
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
37 struct vl_vertex_stream
46 struct vertex2s mv
[4];
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
}
54 struct pipe_vertex_buffer
55 vl_vb_upload_quads(struct pipe_context
*pipe
, unsigned blocks_x
, unsigned blocks_y
)
57 struct pipe_vertex_buffer quad
;
58 struct pipe_transfer
*buf_transfer
;
66 quad
.stride
= sizeof(struct vertex4f
);
67 quad
.buffer_offset
= 0;
68 quad
.buffer
= pipe_buffer_create
71 PIPE_BIND_VERTEX_BUFFER
,
73 sizeof(struct vertex4f
) * 4 * blocks_x
* blocks_y
84 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
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
;
100 pipe_buffer_unmap(pipe
, buf_transfer
);
105 static struct pipe_vertex_element
106 vl_vb_get_quad_vertex_element(void)
108 struct pipe_vertex_element element
;
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
;
120 vl_vb_element_helper(struct pipe_vertex_element
* elements
, unsigned num_elements
,
121 unsigned vertex_buffer_index
)
123 unsigned i
, offset
= 0;
125 assert(elements
&& num_elements
);
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
);
136 vl_vb_get_elems_state(struct pipe_context
*pipe
, bool include_mvs
)
138 struct pipe_vertex_element vertex_elems
[NUM_VS_INPUTS
];
142 memset(&vertex_elems
, 0, sizeof(vertex_elems
));
143 vertex_elems
[VS_I_RECT
] = vl_vb_get_quad_vertex_element();
145 /* Position element */
146 vertex_elems
[VS_I_VPOS
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
148 /* y, cr, cb empty block element top left block */
149 vertex_elems
[VS_I_EB_0_0
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
151 /* y, cr, cb empty block element top right block */
152 vertex_elems
[VS_I_EB_0_1
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
154 /* y, cr, cb empty block element bottom left block */
155 vertex_elems
[VS_I_EB_1_0
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
157 /* y, cr, cb empty block element bottom right block */
158 vertex_elems
[VS_I_EB_1_1
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
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
;
164 vl_vb_element_helper(&vertex_elems
[VS_I_VPOS
], NUM_VS_INPUTS
- (include_mvs
? 1 : 5), 1);
166 return pipe
->create_vertex_elements_state(pipe
, NUM_VS_INPUTS
- (include_mvs
? 0 : 4), vertex_elems
);
169 struct pipe_vertex_buffer
170 vl_vb_init(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
, unsigned size
)
172 struct pipe_vertex_buffer buf
;
177 buffer
->num_not_empty
= 0;
178 buffer
->num_empty
= 0;
180 buf
.stride
= sizeof(struct vl_vertex_stream
);
181 buf
.buffer_offset
= 0;
182 buf
.buffer
= pipe_buffer_create
185 PIPE_BIND_VERTEX_BUFFER
,
187 sizeof(struct vl_vertex_stream
) * size
190 pipe_resource_reference(&buffer
->resource
, buf
.buffer
);
192 vl_vb_map(buffer
, pipe
);
198 vl_vb_map(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
)
200 assert(buffer
&& pipe
);
202 buffer
->start
= pipe_buffer_map
206 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
209 buffer
->end
= buffer
->start
+ buffer
->resource
->width0
/ sizeof(struct vl_vertex_stream
);
213 get_motion_vectors(struct pipe_mpeg12_macroblock
*mb
, struct vertex2s mv
[4])
215 switch (mb
->mb_type
) {
216 case PIPE_MPEG12_MACROBLOCK_TYPE_BI
:
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];
223 mv
[2].x
= mb
->pmv
[0][1][0];
224 mv
[2].y
= mb
->pmv
[0][1][1] - (mb
->pmv
[0][1][1] % 4);
226 mv
[3].x
= mb
->pmv
[1][1][0];
227 mv
[3].y
= mb
->pmv
[1][1][1] - (mb
->pmv
[1][1][1] % 4);
229 if(mb
->mvfs
[0][1]) mv
[2].y
+= 2;
230 if(!mb
->mvfs
[1][1]) mv
[3].y
-= 2;
235 case PIPE_MPEG12_MACROBLOCK_TYPE_FWD
:
236 case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
:
238 if (mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
) {
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];
245 mv
[0].x
= mb
->pmv
[0][1][0];
246 mv
[0].y
= mb
->pmv
[0][1][1] - (mb
->pmv
[0][1][1] % 4);
248 mv
[1].x
= mb
->pmv
[1][1][0];
249 mv
[1].y
= mb
->pmv
[1][1][1] - (mb
->pmv
[1][1][1] % 4);
251 if(mb
->mvfs
[0][1]) mv
[0].y
+= 2;
252 if(!mb
->mvfs
[1][1]) mv
[1].y
-= 2;
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];
262 mv
[0].x
= mb
->pmv
[0][0][0];
263 mv
[0].y
= mb
->pmv
[0][0][1] - (mb
->pmv
[0][0][1] % 4);
265 mv
[1].x
= mb
->pmv
[1][0][0];
266 mv
[1].y
= mb
->pmv
[1][0][1] - (mb
->pmv
[1][0][1] % 4);
268 if(mb
->mvfs
[0][0]) mv
[0].y
+= 2;
269 if(!mb
->mvfs
[1][0]) mv
[1].y
-= 2;
279 vl_vb_add_block(struct vl_vertex_buffer
*buffer
, struct pipe_mpeg12_macroblock
*mb
,
280 const unsigned (*empty_block_mask
)[3][2][2])
282 struct vl_vertex_stream
*stream
;
289 stream
= buffer
->start
+ buffer
->num_not_empty
++;
291 stream
= buffer
->end
- ++buffer
->num_empty
;
293 stream
->pos
.x
= mb
->mbx
;
294 stream
->pos
.y
= mb
->mby
;
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
]);
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;
311 case PIPE_MPEG12_MACROBLOCK_TYPE_FWD
:
312 case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
:
313 stream
->eb
[1][1].flag
= 1;
316 case PIPE_MPEG12_MACROBLOCK_TYPE_BI
:
317 stream
->eb
[1][1].flag
= 0;
324 get_motion_vectors(mb
, stream
->mv
);
328 vl_vb_unmap(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
)
330 assert(buffer
&& pipe
);
332 pipe_buffer_unmap(pipe
, buffer
->transfer
);
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
)
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
;
347 buffer
->num_not_empty
= 0;
348 buffer
->num_empty
= 0;
352 vl_vb_cleanup(struct vl_vertex_buffer
*buffer
)
356 pipe_resource_reference(&buffer
->resource
, NULL
);