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 uint8_t dct_type_field
;
38 uint8_t mb_type_intra
;
39 uint8_t mv_wheights
[2];
40 struct vertex2s 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_elems_state(struct pipe_context
*pipe
, int component
)
135 struct pipe_vertex_element vertex_elems
[NUM_VS_INPUTS
];
137 memset(&vertex_elems
, 0, sizeof(vertex_elems
));
138 vertex_elems
[VS_I_RECT
] = vl_vb_get_quad_vertex_element();
140 /* Position element */
141 vertex_elems
[VS_I_VPOS
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
143 /* empty block element of selected component */
144 vertex_elems
[VS_I_EB
].src_offset
= 4 + component
* 4;
145 vertex_elems
[VS_I_EB
].src_format
= PIPE_FORMAT_R8G8B8A8_USCALED
;
148 vertex_elems
[VS_I_FLAGS
].src_offset
= 16;
149 vertex_elems
[VS_I_FLAGS
].src_format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
151 /* motion vector 0 TOP element */
152 vertex_elems
[VS_I_MV0_TOP
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
154 /* motion vector 0 BOTTOM element */
155 vertex_elems
[VS_I_MV0_BOTTOM
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
157 /* motion vector 1 TOP element */
158 vertex_elems
[VS_I_MV1_TOP
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
160 /* motion vector 1 BOTTOM element */
161 vertex_elems
[VS_I_MV1_BOTTOM
].src_format
= PIPE_FORMAT_R16G16_SSCALED
;
163 vl_vb_element_helper(&vertex_elems
[VS_I_VPOS
], NUM_VS_INPUTS
- 1, 1);
165 return pipe
->create_vertex_elements_state(pipe
, NUM_VS_INPUTS
, vertex_elems
);
168 struct pipe_vertex_buffer
169 vl_vb_init(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
, unsigned size
)
171 struct pipe_vertex_buffer buf
;
176 buffer
->num_not_empty
= 0;
177 buffer
->num_empty
= 0;
179 buf
.stride
= sizeof(struct vl_vertex_stream
);
180 buf
.buffer_offset
= 0;
181 buf
.buffer
= pipe_buffer_create
184 PIPE_BIND_VERTEX_BUFFER
,
186 sizeof(struct vl_vertex_stream
) * size
189 pipe_resource_reference(&buffer
->resource
, buf
.buffer
);
191 vl_vb_map(buffer
, pipe
);
197 vl_vb_map(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
)
199 assert(buffer
&& pipe
);
201 buffer
->start
= pipe_buffer_map
205 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
208 buffer
->end
= buffer
->start
+ buffer
->resource
->width0
/ sizeof(struct vl_vertex_stream
);
212 get_motion_vectors(struct pipe_mpeg12_macroblock
*mb
, struct vertex2s mv
[4])
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
;
217 mv
[2].x
= mv
[3].x
= mb
->mv
[1].top
.x
;
218 mv
[2].y
= mv
[3].y
= mb
->mv
[1].top
.y
;
221 mv
[0].x
= mb
->mv
[0].top
.x
;
222 mv
[0].y
= mb
->mv
[0].top
.y
- (mb
->mv
[0].top
.y
% 4);
224 mv
[1].x
= mb
->mv
[0].bottom
.x
;
225 mv
[1].y
= mb
->mv
[0].bottom
.y
- (mb
->mv
[0].bottom
.y
% 4);
227 if (mb
->mv
[0].top
.field_select
) mv
[0].y
+= 2;
228 if (!mb
->mv
[0].bottom
.field_select
) mv
[1].y
-= 2;
230 mv
[2].x
= mb
->mv
[1].top
.x
;
231 mv
[2].y
= mb
->mv
[1].top
.y
- (mb
->mv
[1].top
.y
% 4);
233 mv
[3].x
= mb
->mv
[1].bottom
.x
;
234 mv
[3].y
= mb
->mv
[1].bottom
.y
- (mb
->mv
[1].bottom
.y
% 4);
236 if (mb
->mv
[1].top
.field_select
) mv
[2].y
+= 2;
237 if (!mb
->mv
[1].bottom
.field_select
) mv
[3].y
-= 2;
242 vl_vb_add_block(struct vl_vertex_buffer
*buffer
, struct pipe_mpeg12_macroblock
*mb
,
243 const unsigned (*empty_block_mask
)[3][2][2])
245 struct vl_vertex_stream
*stream
;
250 assert(buffer
->num_not_empty
+ buffer
->num_empty
< buffer
->size
);
253 stream
= buffer
->start
+ buffer
->num_not_empty
++;
255 stream
= buffer
->end
- ++buffer
->num_empty
;
257 stream
->pos
.x
= mb
->mbx
;
258 stream
->pos
.y
= mb
->mby
;
260 for ( i
= 0; i
< 3; ++i
)
261 for ( j
= 0; j
< 2; ++j
)
262 for ( k
= 0; k
< 2; ++k
)
263 stream
->eb
[i
][j
][k
] = !(mb
->cbp
& (*empty_block_mask
)[i
][j
][k
]);
265 stream
->dct_type_field
= mb
->dct_type
== PIPE_MPEG12_DCT_TYPE_FIELD
;
266 stream
->mb_type_intra
= !mb
->dct_intra
;
268 stream
->mv_wheights
[0] = mb
->mv
[0].wheight
;
269 stream
->mv_wheights
[1] = mb
->mv
[1].wheight
;
270 get_motion_vectors(mb
, stream
->mv
);
274 vl_vb_unmap(struct vl_vertex_buffer
*buffer
, struct pipe_context
*pipe
)
276 assert(buffer
&& pipe
);
278 pipe_buffer_unmap(pipe
, buffer
->transfer
);
282 vl_vb_restart(struct vl_vertex_buffer
*buffer
,
283 unsigned *not_empty_start_instance
, unsigned *not_empty_num_instances
,
284 unsigned *empty_start_instance
, unsigned *empty_num_instances
)
288 *not_empty_start_instance
= 0;
289 *not_empty_num_instances
= buffer
->num_not_empty
;
290 *empty_start_instance
= buffer
->size
- buffer
->num_empty
;
291 *empty_num_instances
= buffer
->num_empty
;
293 buffer
->num_not_empty
= 0;
294 buffer
->num_empty
= 0;
298 vl_vb_cleanup(struct vl_vertex_buffer
*buffer
)
302 pipe_resource_reference(&buffer
->resource
, NULL
);