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
37 int8_t dct_type_field
;
41 struct vertex2s mv
[4];
44 /* vertices for a quad covering a block */
45 static const struct vertex2f block_quad
[4] = {
46 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
49 struct pipe_vertex_buffer
50 vl_vb_upload_quads(struct pipe_context
*pipe
, unsigned blocks_x
, unsigned blocks_y
)
52 struct pipe_vertex_buffer quad
;
53 struct pipe_transfer
*buf_transfer
;
61 quad
.stride
= sizeof(struct vertex4f
);
62 quad
.buffer_offset
= 0;
63 quad
.buffer
= pipe_buffer_create
66 PIPE_BIND_VERTEX_BUFFER
,
68 sizeof(struct vertex4f
) * 4 * blocks_x
* blocks_y
79 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
83 for ( y
= 0; y
< blocks_y
; ++y
) {
84 for ( x
= 0; x
< blocks_x
; ++x
) {
85 for (i
= 0; i
< 4; ++i
, ++v
) {
86 v
->x
= block_quad
[i
].x
;
87 v
->y
= block_quad
[i
].y
;
95 pipe_buffer_unmap(pipe
, buf_transfer
);
100 static struct pipe_vertex_element
101 vl_vb_get_quad_vertex_element(void)
103 struct pipe_vertex_element element
;
105 /* setup rectangle element */
106 element
.src_offset
= 0;
107 element
.instance_divisor
= 0;
108 element
.vertex_buffer_index
= 0;
109 element
.src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
115 vl_vb_element_helper(struct pipe_vertex_element
* elements
, unsigned num_elements
,
116 unsigned vertex_buffer_index
)
118 unsigned i
, offset
= 0;
120 assert(elements
&& num_elements
);
122 for ( i
= 0; i
< num_elements
; ++i
) {
123 if (elements
[i
].src_offset
)
124 offset
= elements
[i
].src_offset
;
126 elements
[i
].src_offset
= offset
;
127 elements
[i
].instance_divisor
= 1;
128 elements
[i
].vertex_buffer_index
= vertex_buffer_index
;
129 offset
+= util_format_get_blocksize(elements
[i
].src_format
);
134 vl_vb_get_elems_state(struct pipe_context
*pipe
, int component
)
136 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 /* empty block element of selected component */
145 vertex_elems
[VS_I_EB
].src_offset
= 4 + component
* 4;
146 vertex_elems
[VS_I_EB
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
149 vertex_elems
[VS_I_FLAGS
].src_offset
= 16;
150 vertex_elems
[VS_I_FLAGS
].src_format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
152 /* motion vector 0 TOP element */
153 vertex_elems
[VS_I_MV0_TOP
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
155 /* motion vector 0 BOTTOM element */
156 vertex_elems
[VS_I_MV0_BOTTOM
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
158 /* motion vector 1 TOP element */
159 vertex_elems
[VS_I_MV1_TOP
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
161 /* motion vector 1 BOTTOM element */
162 vertex_elems
[VS_I_MV1_BOTTOM
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
164 vl_vb_element_helper(&vertex_elems
[VS_I_VPOS
], NUM_VS_INPUTS
- 1, 1);
166 return pipe
->create_vertex_elements_state(pipe
, NUM_VS_INPUTS
, 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 if (mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_BI
||
216 mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_FWD
) {
218 if (mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
) {
219 mv
[0].x
= mv
[1].x
= mb
->mv
[0].top
.x
;
220 mv
[0].y
= mv
[1].y
= mb
->mv
[0].top
.y
;
223 mv
[0].x
= mb
->mv
[0].top
.x
;
224 mv
[0].y
= mb
->mv
[0].top
.y
- (mb
->mv
[0].top
.y
% 4);
226 mv
[1].x
= mb
->mv
[0].bottom
.x
;
227 mv
[1].y
= mb
->mv
[0].bottom
.y
- (mb
->mv
[0].bottom
.y
% 4);
229 if (mb
->mv
[0].top
.field_select
) mv
[0].y
+= 2;
230 if (!mb
->mv
[0].bottom
.field_select
) mv
[1].y
-= 2;
233 mv
[0].x
= mv
[0].y
= mv
[1].x
= mv
[1].y
= 0x8000;
236 if (mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_BI
||
237 mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
) {
239 if (mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
) {
240 mv
[2].x
= mv
[3].x
= mb
->mv
[1].top
.x
;
241 mv
[2].y
= mv
[3].y
= mb
->mv
[1].top
.y
;
244 mv
[2].x
= mb
->mv
[1].top
.x
;
245 mv
[2].y
= mb
->mv
[1].top
.y
- (mb
->mv
[1].top
.y
% 4);
247 mv
[3].x
= mb
->mv
[1].bottom
.x
;
248 mv
[3].y
= mb
->mv
[1].bottom
.y
- (mb
->mv
[1].bottom
.y
% 4);
250 if (mb
->mv
[1].top
.field_select
) mv
[2].y
+= 2;
251 if (!mb
->mv
[1].bottom
.field_select
) mv
[3].y
-= 2;
254 mv
[2].x
= mv
[2].y
= mv
[3].x
= mv
[3].y
= 0x8000;
259 vl_vb_add_block(struct vl_vertex_buffer
*buffer
, struct pipe_mpeg12_macroblock
*mb
,
260 const unsigned (*empty_block_mask
)[3][2][2])
262 struct vl_vertex_stream
*stream
;
267 assert(buffer
->num_not_empty
+ buffer
->num_empty
< buffer
->size
);
270 stream
= buffer
->start
+ buffer
->num_not_empty
++;
272 stream
= buffer
->end
- ++buffer
->num_empty
;
274 stream
->pos
.x
= mb
->mbx
;
275 stream
->pos
.y
= mb
->mby
;
277 for ( i
= 0; i
< 3; ++i
)
278 for ( j
= 0; j
< 2; ++j
)
279 for ( k
= 0; k
< 2; ++k
)
280 stream
->eb
[i
][j
][k
] = !(mb
->cbp
& (*empty_block_mask
)[i
][j
][k
]);
282 stream
->dct_type_field
= mb
->dct_type
== PIPE_MPEG12_DCT_TYPE_FIELD
;
283 stream
->mo_type_frame
= mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
;
284 stream
->mb_type_intra
= mb
->mb_type
!= PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
;
285 switch (mb
->mb_type
) {
286 case PIPE_MPEG12_MACROBLOCK_TYPE_FWD
:
287 stream
->mv_wheights
= 0;
290 case PIPE_MPEG12_MACROBLOCK_TYPE_BI
:
291 stream
->mv_wheights
= 1;
294 case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
:
295 stream
->mv_wheights
= 2;
299 stream
->mv_wheights
= 0;
302 get_motion_vectors(mb
, stream
->mv
);
306 vl_vb_unmap(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
)
308 assert(buffer
&& pipe
);
310 pipe_buffer_unmap(pipe
, buffer
->transfer
);
314 vl_vb_restart(struct vl_vertex_buffer
*buffer
,
315 unsigned *not_empty_start_instance
, unsigned *not_empty_num_instances
,
316 unsigned *empty_start_instance
, unsigned *empty_num_instances
)
320 *not_empty_start_instance
= 0;
321 *not_empty_num_instances
= buffer
->num_not_empty
;
322 *empty_start_instance
= buffer
->size
- buffer
->num_empty
;
323 *empty_num_instances
= buffer
->num_empty
;
325 buffer
->num_not_empty
= 0;
326 buffer
->num_empty
= 0;
330 vl_vb_cleanup(struct vl_vertex_buffer
*buffer
)
334 pipe_resource_reference(&buffer
->resource
, NULL
);