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 <util/u_format.h>
30 #include "vl_vertex_buffers.h"
33 struct vl_vertex_stream
42 struct vertex2s mv
[4];
45 /* vertices for a quad covering a block */
46 static const struct vertex2f block_quad
[4] = {
47 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
50 struct pipe_vertex_buffer
51 vl_vb_upload_quads(struct pipe_context
*pipe
, unsigned blocks_x
, unsigned blocks_y
)
53 struct pipe_vertex_buffer quad
;
54 struct pipe_transfer
*buf_transfer
;
62 quad
.stride
= sizeof(struct vertex4f
);
63 quad
.buffer_offset
= 0;
64 quad
.buffer
= pipe_buffer_create
67 PIPE_BIND_VERTEX_BUFFER
,
69 sizeof(struct vertex4f
) * 4 * blocks_x
* blocks_y
80 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
84 for ( y
= 0; y
< blocks_y
; ++y
) {
85 for ( x
= 0; x
< blocks_x
; ++x
) {
86 for (i
= 0; i
< 4; ++i
, ++v
) {
87 v
->x
= block_quad
[i
].x
;
88 v
->y
= block_quad
[i
].y
;
96 pipe_buffer_unmap(pipe
, buf_transfer
);
101 static struct pipe_vertex_element
102 vl_vb_get_quad_vertex_element(void)
104 struct pipe_vertex_element element
;
106 /* setup rectangle element */
107 element
.src_offset
= 0;
108 element
.instance_divisor
= 0;
109 element
.vertex_buffer_index
= 0;
110 element
.src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
116 vl_vb_element_helper(struct pipe_vertex_element
* elements
, unsigned num_elements
,
117 unsigned vertex_buffer_index
)
119 unsigned i
, offset
= 0;
121 assert(elements
&& num_elements
);
123 for ( i
= 0; i
< num_elements
; ++i
) {
124 elements
[i
].src_offset
= offset
;
125 elements
[i
].instance_divisor
= 1;
126 elements
[i
].vertex_buffer_index
= vertex_buffer_index
;
127 offset
+= util_format_get_blocksize(elements
[i
].src_format
);
132 vl_vb_get_elems_state(struct pipe_context
*pipe
, bool include_mvs
)
134 struct pipe_vertex_element vertex_elems
[NUM_VS_INPUTS
];
138 memset(&vertex_elems
, 0, sizeof(vertex_elems
));
139 vertex_elems
[VS_I_RECT
] = vl_vb_get_quad_vertex_element();
141 /* Position element */
142 vertex_elems
[VS_I_VPOS
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
144 /* y, cr, cb empty block element top left block */
145 vertex_elems
[VS_I_EB_0_0
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
147 /* y, cr, cb empty block element top right block */
148 vertex_elems
[VS_I_EB_0_1
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
150 /* y, cr, cb empty block element bottom left block */
151 vertex_elems
[VS_I_EB_1_0
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
153 /* y, cr, cb empty block element bottom right block */
154 vertex_elems
[VS_I_EB_1_1
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
156 for (i
= 0; i
< 4; ++i
)
157 /* motion vector 0..4 element */
158 vertex_elems
[VS_I_MV0
+ i
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
160 vl_vb_element_helper(&vertex_elems
[VS_I_VPOS
], NUM_VS_INPUTS
- (include_mvs
? 1 : 5), 1);
162 return pipe
->create_vertex_elements_state(pipe
, NUM_VS_INPUTS
- (include_mvs
? 0 : 4), vertex_elems
);
165 struct pipe_vertex_buffer
166 vl_vb_init(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
, unsigned size
)
168 struct pipe_vertex_buffer buf
;
173 buffer
->num_not_empty
= 0;
174 buffer
->num_empty
= 0;
176 buf
.stride
= sizeof(struct vl_vertex_stream
);
177 buf
.buffer_offset
= 0;
178 buf
.buffer
= pipe_buffer_create
181 PIPE_BIND_VERTEX_BUFFER
,
183 sizeof(struct vl_vertex_stream
) * size
186 pipe_resource_reference(&buffer
->resource
, buf
.buffer
);
188 vl_vb_map(buffer
, pipe
);
194 vl_vb_map(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
)
196 assert(buffer
&& pipe
);
198 buffer
->start
= pipe_buffer_map
202 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
205 buffer
->end
= buffer
->start
+ buffer
->resource
->width0
/ sizeof(struct vl_vertex_stream
);
209 get_motion_vectors(struct pipe_mpeg12_macroblock
*mb
, struct vertex2s mv
[4])
211 if (mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_BI
||
212 mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_FWD
) {
214 if (mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
) {
215 mv
[0].x
= mv
[1].x
= mb
->mv
[0].top
.x
;
216 mv
[0].y
= mv
[1].y
= mb
->mv
[0].top
.y
;
219 mv
[0].x
= mb
->mv
[0].top
.x
;
220 mv
[0].y
= mb
->mv
[0].top
.y
- (mb
->mv
[0].top
.y
% 4);
222 mv
[1].x
= mb
->mv
[0].bottom
.x
;
223 mv
[1].y
= mb
->mv
[0].bottom
.y
- (mb
->mv
[0].bottom
.y
% 4);
225 if (mb
->mv
[0].top
.field_select
) mv
[0].y
+= 2;
226 if (!mb
->mv
[0].bottom
.field_select
) mv
[1].y
-= 2;
229 mv
[0].x
= mv
[0].y
= mv
[1].x
= mv
[1].y
= 0x8000;
232 if (mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_BI
||
233 mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
) {
235 if (mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
) {
236 mv
[2].x
= mv
[3].x
= mb
->mv
[1].top
.x
;
237 mv
[2].y
= mv
[3].y
= mb
->mv
[1].top
.y
;
240 mv
[2].x
= mb
->mv
[1].top
.x
;
241 mv
[2].y
= mb
->mv
[1].top
.y
- (mb
->mv
[1].top
.y
% 4);
243 mv
[3].x
= mb
->mv
[1].bottom
.x
;
244 mv
[3].y
= mb
->mv
[1].bottom
.y
- (mb
->mv
[1].bottom
.y
% 4);
246 if (mb
->mv
[1].top
.field_select
) mv
[2].y
+= 2;
247 if (!mb
->mv
[1].bottom
.field_select
) mv
[3].y
-= 2;
250 mv
[2].x
= mv
[2].y
= mv
[3].x
= mv
[3].y
= 0x8000;
255 vl_vb_add_block(struct vl_vertex_buffer
*buffer
, struct pipe_mpeg12_macroblock
*mb
,
256 const unsigned (*empty_block_mask
)[3][2][2])
258 struct vl_vertex_stream
*stream
;
265 stream
= buffer
->start
+ buffer
->num_not_empty
++;
267 stream
= buffer
->end
- ++buffer
->num_empty
;
269 stream
->pos
.x
= mb
->mbx
;
270 stream
->pos
.y
= mb
->mby
;
272 for ( i
= 0; i
< 2; ++i
) {
273 for ( j
= 0; j
< 2; ++j
) {
274 stream
->eb
[i
][j
].y
= !(mb
->cbp
& (*empty_block_mask
)[0][i
][j
]);
275 stream
->eb
[i
][j
].cr
= !(mb
->cbp
& (*empty_block_mask
)[1][i
][j
]);
276 stream
->eb
[i
][j
].cb
= !(mb
->cbp
& (*empty_block_mask
)[2][i
][j
]);
279 stream
->eb
[0][0].flag
= mb
->dct_type
== PIPE_MPEG12_DCT_TYPE_FIELD
;
280 stream
->eb
[0][1].flag
= mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
;
281 stream
->eb
[1][0].flag
= mb
->mb_type
!= PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
;
282 switch (mb
->mb_type
) {
283 case PIPE_MPEG12_MACROBLOCK_TYPE_FWD
:
284 stream
->eb
[1][1].flag
= 0;
287 case PIPE_MPEG12_MACROBLOCK_TYPE_BI
:
288 stream
->eb
[1][1].flag
= 1;
291 case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
:
292 stream
->eb
[1][1].flag
= 2;
296 get_motion_vectors(mb
, stream
->mv
);
300 vl_vb_unmap(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
)
302 assert(buffer
&& pipe
);
304 pipe_buffer_unmap(pipe
, buffer
->transfer
);
308 vl_vb_restart(struct vl_vertex_buffer
*buffer
,
309 unsigned *not_empty_start_instance
, unsigned *not_empty_num_instances
,
310 unsigned *empty_start_instance
, unsigned *empty_num_instances
)
314 *not_empty_start_instance
= 0;
315 *not_empty_num_instances
= buffer
->num_not_empty
;
316 *empty_start_instance
= buffer
->size
- buffer
->num_empty
;
317 *empty_num_instances
= buffer
->num_empty
;
319 buffer
->num_not_empty
= 0;
320 buffer
->num_empty
= 0;
324 vl_vb_cleanup(struct vl_vertex_buffer
*buffer
)
328 pipe_resource_reference(&buffer
->resource
, NULL
);