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
36 uint8_t mb_type_intra
;
37 uint8_t dct_type_field
;
40 struct vertex4s mv
[4];
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
}
48 struct pipe_vertex_buffer
49 vl_vb_upload_quads(struct pipe_context
*pipe
, unsigned blocks_x
, unsigned blocks_y
)
51 struct pipe_vertex_buffer quad
;
52 struct pipe_transfer
*buf_transfer
;
60 quad
.stride
= sizeof(struct vertex4f
);
61 quad
.buffer_offset
= 0;
62 quad
.buffer
= pipe_buffer_create
65 PIPE_BIND_VERTEX_BUFFER
,
67 sizeof(struct vertex4f
) * 4 * blocks_x
* blocks_y
78 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
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
;
94 pipe_buffer_unmap(pipe
, buf_transfer
);
99 static struct pipe_vertex_element
100 vl_vb_get_quad_vertex_element(void)
102 struct pipe_vertex_element element
;
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
;
114 vl_vb_element_helper(struct pipe_vertex_element
* elements
, unsigned num_elements
,
115 unsigned vertex_buffer_index
)
117 unsigned i
, offset
= 0;
119 assert(elements
&& num_elements
);
121 for ( i
= 0; i
< num_elements
; ++i
) {
122 if (elements
[i
].src_offset
)
123 offset
= elements
[i
].src_offset
;
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
);
133 vl_vb_get_ves_eb(struct pipe_context
*pipe
, int component
)
135 struct pipe_vertex_element vertex_elems
[NUM_VS_INPUTS
];
139 memset(&vertex_elems
, 0, sizeof(vertex_elems
));
140 vertex_elems
[VS_I_RECT
] = vl_vb_get_quad_vertex_element();
142 /* Position element */
143 vertex_elems
[VS_I_VPOS
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
146 vertex_elems
[VS_I_FLAGS
].src_format
= PIPE_FORMAT_R8G8B8A8_USCALED
;
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
;
152 vl_vb_element_helper(&vertex_elems
[VS_I_VPOS
], NUM_VS_INPUTS
- 1, 1);
154 return pipe
->create_vertex_elements_state(pipe
, NUM_VS_INPUTS
, vertex_elems
);
158 vl_vb_get_ves_mv(struct pipe_context
*pipe
, int motionvector
)
160 struct pipe_vertex_element vertex_elems
[NUM_VS_INPUTS
];
162 memset(&vertex_elems
, 0, sizeof(vertex_elems
));
163 vertex_elems
[VS_I_RECT
] = vl_vb_get_quad_vertex_element();
167 /* Position element */
168 vertex_elems
[VS_I_VPOS
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
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
;
174 /* motion vector BOTTOM element */
175 vertex_elems
[VS_I_MV_BOTTOM
].src_format
= PIPE_FORMAT_R16G16B16A16_SSCALED
;
177 vl_vb_element_helper(&vertex_elems
[VS_I_VPOS
], NUM_VS_INPUTS
- 1, 1);
179 return pipe
->create_vertex_elements_state(pipe
, NUM_VS_INPUTS
, vertex_elems
);
182 struct pipe_vertex_buffer
183 vl_vb_init(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
, unsigned size
)
185 struct pipe_vertex_buffer buf
;
190 buffer
->num_not_empty
= 0;
191 buffer
->num_empty
= 0;
193 buf
.stride
= sizeof(struct vl_vertex_stream
);
194 buf
.buffer_offset
= 0;
195 buf
.buffer
= pipe_buffer_create
198 PIPE_BIND_VERTEX_BUFFER
,
200 sizeof(struct vl_vertex_stream
) * size
203 pipe_resource_reference(&buffer
->resource
, buf
.buffer
);
205 vl_vb_map(buffer
, pipe
);
211 vl_vb_map(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
)
213 assert(buffer
&& pipe
);
215 buffer
->start
= pipe_buffer_map
219 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
222 buffer
->end
= buffer
->start
+ buffer
->resource
->width0
/ sizeof(struct vl_vertex_stream
);
226 get_motion_vectors(struct pipe_mpeg12_macroblock
*mb
, struct vertex4s mv
[4])
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;
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;
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
;
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
;
246 if (mb
->mv
[0].top
.field_select
) mv
[0].y
+= 2;
247 if (!mb
->mv
[0].bottom
.field_select
) mv
[1].y
-= 2;
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
;
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
;
257 if (mb
->mv
[1].top
.field_select
) mv
[2].y
+= 2;
258 if (!mb
->mv
[1].bottom
.field_select
) mv
[3].y
-= 2;
261 mv
[0].w
= mv
[1].w
= mb
->mv
[0].wheight
;
262 mv
[2].w
= mv
[3].w
= mb
->mv
[1].wheight
;
266 vl_vb_add_block(struct vl_vertex_buffer
*buffer
, struct pipe_mpeg12_macroblock
*mb
,
267 const unsigned (*empty_block_mask
)[3][2][2])
269 struct vl_vertex_stream
*stream
;
274 assert(buffer
->num_not_empty
+ buffer
->num_empty
< buffer
->size
);
277 stream
= buffer
->start
+ buffer
->num_not_empty
++;
279 stream
= buffer
->end
- ++buffer
->num_empty
;
281 stream
->pos
.x
= mb
->mbx
;
282 stream
->pos
.y
= mb
->mby
;
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
]);
289 stream
->dct_type_field
= mb
->dct_type
== PIPE_MPEG12_DCT_TYPE_FIELD
;
290 stream
->mb_type_intra
= mb
->dct_intra
;
292 get_motion_vectors(mb
, stream
->mv
);
296 vl_vb_unmap(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
)
298 assert(buffer
&& pipe
);
300 pipe_buffer_unmap(pipe
, buffer
->transfer
);
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
)
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
;
315 buffer
->num_not_empty
= 0;
316 buffer
->num_empty
= 0;
320 vl_vb_cleanup(struct vl_vertex_buffer
*buffer
)
324 pipe_resource_reference(&buffer
->resource
, NULL
);