• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2010 Christian König
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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 VMWARE 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.
25  *
26  **************************************************************************/
27 
28 #include <assert.h>
29 #include "util/format/u_format.h"
30 #include "vl_vertex_buffers.h"
31 #include "vl_types.h"
32 
33 /* vertices for a quad covering a block */
34 static const struct vertex2f block_quad[4] = {
35    {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
36 };
37 
38 struct pipe_vertex_buffer
vl_vb_upload_quads(struct pipe_context * pipe)39 vl_vb_upload_quads(struct pipe_context *pipe)
40 {
41    struct pipe_vertex_buffer quad;
42    struct pipe_transfer *buf_transfer;
43    struct vertex2f *v;
44 
45    unsigned i;
46 
47    assert(pipe);
48 
49    /* create buffer */
50    quad.stride = sizeof(struct vertex2f);
51    quad.buffer_offset = 0;
52    quad.buffer.resource = pipe_buffer_create
53    (
54       pipe->screen,
55       PIPE_BIND_VERTEX_BUFFER,
56       PIPE_USAGE_DEFAULT,
57       sizeof(struct vertex2f) * 4
58    );
59    quad.is_user_buffer = false;
60 
61    if(!quad.buffer.resource)
62       return quad;
63 
64    /* and fill it */
65    v = pipe_buffer_map
66    (
67       pipe,
68       quad.buffer.resource,
69       PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE,
70       &buf_transfer
71    );
72 
73    for (i = 0; i < 4; ++i, ++v) {
74       v->x = block_quad[i].x;
75       v->y = block_quad[i].y;
76    }
77 
78    pipe_buffer_unmap(pipe, buf_transfer);
79 
80    return quad;
81 }
82 
83 struct pipe_vertex_buffer
vl_vb_upload_pos(struct pipe_context * pipe,unsigned width,unsigned height)84 vl_vb_upload_pos(struct pipe_context *pipe, unsigned width, unsigned height)
85 {
86    struct pipe_vertex_buffer pos;
87    struct pipe_transfer *buf_transfer;
88    struct vertex2s *v;
89 
90    unsigned x, y;
91 
92    assert(pipe);
93 
94    /* create buffer */
95    pos.stride = sizeof(struct vertex2s);
96    pos.buffer_offset = 0;
97    pos.buffer.resource = pipe_buffer_create
98    (
99       pipe->screen,
100       PIPE_BIND_VERTEX_BUFFER,
101       PIPE_USAGE_DEFAULT,
102       sizeof(struct vertex2s) * width * height
103    );
104    pos.is_user_buffer = false;
105 
106    if(!pos.buffer.resource)
107       return pos;
108 
109    /* and fill it */
110    v = pipe_buffer_map
111    (
112       pipe,
113       pos.buffer.resource,
114       PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE,
115       &buf_transfer
116    );
117 
118    for ( y = 0; y < height; ++y) {
119       for ( x = 0; x < width; ++x, ++v) {
120          v->x = x;
121          v->y = y;
122       }
123    }
124 
125    pipe_buffer_unmap(pipe, buf_transfer);
126 
127    return pos;
128 }
129 
130 static struct pipe_vertex_element
vl_vb_get_quad_vertex_element(void)131 vl_vb_get_quad_vertex_element(void)
132 {
133    struct pipe_vertex_element element;
134 
135    /* setup rectangle element */
136    element.src_offset = 0;
137    element.instance_divisor = 0;
138    element.vertex_buffer_index = 0;
139    element.src_format = PIPE_FORMAT_R32G32_FLOAT;
140 
141    return element;
142 }
143 
144 static void
vl_vb_element_helper(struct pipe_vertex_element * elements,unsigned num_elements,unsigned vertex_buffer_index)145 vl_vb_element_helper(struct pipe_vertex_element* elements, unsigned num_elements,
146                      unsigned vertex_buffer_index)
147 {
148    unsigned i, offset = 0;
149 
150    assert(elements && num_elements);
151 
152    for ( i = 0; i < num_elements; ++i ) {
153       elements[i].src_offset = offset;
154       elements[i].instance_divisor = 1;
155       elements[i].vertex_buffer_index = vertex_buffer_index;
156       offset += util_format_get_blocksize(elements[i].src_format);
157    }
158 }
159 
160 void *
vl_vb_get_ves_ycbcr(struct pipe_context * pipe)161 vl_vb_get_ves_ycbcr(struct pipe_context *pipe)
162 {
163    struct pipe_vertex_element vertex_elems[NUM_VS_INPUTS];
164 
165    assert(pipe);
166 
167    memset(&vertex_elems, 0, sizeof(vertex_elems));
168    vertex_elems[VS_I_RECT] = vl_vb_get_quad_vertex_element();
169 
170    /* Position element */
171    vertex_elems[VS_I_VPOS].src_format = PIPE_FORMAT_R8G8B8A8_USCALED;
172 
173    /* block num element */
174    vertex_elems[VS_I_BLOCK_NUM].src_format = PIPE_FORMAT_R32_FLOAT;
175 
176    vl_vb_element_helper(&vertex_elems[VS_I_VPOS], 2, 1);
177 
178    return pipe->create_vertex_elements_state(pipe, 3, vertex_elems);
179 }
180 
181 void *
vl_vb_get_ves_mv(struct pipe_context * pipe)182 vl_vb_get_ves_mv(struct pipe_context *pipe)
183 {
184    struct pipe_vertex_element vertex_elems[NUM_VS_INPUTS];
185 
186    assert(pipe);
187 
188    memset(&vertex_elems, 0, sizeof(vertex_elems));
189    vertex_elems[VS_I_RECT] = vl_vb_get_quad_vertex_element();
190 
191    /* Position element */
192    vertex_elems[VS_I_VPOS].src_format = PIPE_FORMAT_R16G16_SSCALED;
193 
194    vl_vb_element_helper(&vertex_elems[VS_I_VPOS], 1, 1);
195 
196    /* motion vector TOP element */
197    vertex_elems[VS_I_MV_TOP].src_format = PIPE_FORMAT_R16G16B16A16_SSCALED;
198 
199    /* motion vector BOTTOM element */
200    vertex_elems[VS_I_MV_BOTTOM].src_format = PIPE_FORMAT_R16G16B16A16_SSCALED;
201 
202    vl_vb_element_helper(&vertex_elems[VS_I_MV_TOP], 2, 2);
203 
204    return pipe->create_vertex_elements_state(pipe, NUM_VS_INPUTS, vertex_elems);
205 }
206 
207 bool
vl_vb_init(struct vl_vertex_buffer * buffer,struct pipe_context * pipe,unsigned width,unsigned height)208 vl_vb_init(struct vl_vertex_buffer *buffer, struct pipe_context *pipe,
209            unsigned width, unsigned height)
210 {
211    unsigned i, size;
212 
213    assert(buffer);
214 
215    buffer->width = width;
216    buffer->height = height;
217 
218    size = width * height;
219 
220    for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
221       buffer->ycbcr[i].resource = pipe_buffer_create
222       (
223          pipe->screen,
224          PIPE_BIND_VERTEX_BUFFER,
225          PIPE_USAGE_STREAM,
226          sizeof(struct vl_ycbcr_block) * size * 4
227       );
228       if (!buffer->ycbcr[i].resource)
229          goto error_ycbcr;
230    }
231 
232    for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
233       buffer->mv[i].resource = pipe_buffer_create
234       (
235          pipe->screen,
236          PIPE_BIND_VERTEX_BUFFER,
237          PIPE_USAGE_STREAM,
238          sizeof(struct vl_motionvector) * size
239       );
240       if (!buffer->mv[i].resource)
241          goto error_mv;
242    }
243 
244    return true;
245 
246 error_mv:
247    for (i = 0; i < VL_NUM_COMPONENTS; ++i)
248       pipe_resource_reference(&buffer->mv[i].resource, NULL);
249 
250 error_ycbcr:
251    for (i = 0; i < VL_NUM_COMPONENTS; ++i)
252       pipe_resource_reference(&buffer->ycbcr[i].resource, NULL);
253    return false;
254 }
255 
256 unsigned
vl_vb_attributes_per_plock(struct vl_vertex_buffer * buffer)257 vl_vb_attributes_per_plock(struct vl_vertex_buffer *buffer)
258 {
259    return 1;
260 }
261 
262 struct pipe_vertex_buffer
vl_vb_get_ycbcr(struct vl_vertex_buffer * buffer,int component)263 vl_vb_get_ycbcr(struct vl_vertex_buffer *buffer, int component)
264 {
265    struct pipe_vertex_buffer buf;
266 
267    assert(buffer);
268 
269    buf.stride = sizeof(struct vl_ycbcr_block);
270    buf.buffer_offset = 0;
271    buf.buffer.resource = buffer->ycbcr[component].resource;
272    buf.is_user_buffer = false;
273 
274    return buf;
275 }
276 
277 struct pipe_vertex_buffer
vl_vb_get_mv(struct vl_vertex_buffer * buffer,int motionvector)278 vl_vb_get_mv(struct vl_vertex_buffer *buffer, int motionvector)
279 {
280    struct pipe_vertex_buffer buf;
281 
282    assert(buffer);
283 
284    buf.stride = sizeof(struct vl_motionvector);
285    buf.buffer_offset = 0;
286    buf.buffer.resource = buffer->mv[motionvector].resource;
287    buf.is_user_buffer = false;
288 
289    return buf;
290 }
291 
292 void
vl_vb_map(struct vl_vertex_buffer * buffer,struct pipe_context * pipe)293 vl_vb_map(struct vl_vertex_buffer *buffer, struct pipe_context *pipe)
294 {
295    unsigned i;
296 
297    assert(buffer && pipe);
298 
299    for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
300       buffer->ycbcr[i].vertex_stream = pipe_buffer_map
301       (
302          pipe,
303          buffer->ycbcr[i].resource,
304          PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE,
305          &buffer->ycbcr[i].transfer
306       );
307    }
308 
309    for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
310       buffer->mv[i].vertex_stream = pipe_buffer_map
311       (
312          pipe,
313          buffer->mv[i].resource,
314          PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE,
315          &buffer->mv[i].transfer
316       );
317    }
318 
319 }
320 
321 struct vl_ycbcr_block *
vl_vb_get_ycbcr_stream(struct vl_vertex_buffer * buffer,int component)322 vl_vb_get_ycbcr_stream(struct vl_vertex_buffer *buffer, int component)
323 {
324    assert(buffer);
325    assert(component < VL_NUM_COMPONENTS);
326 
327    return buffer->ycbcr[component].vertex_stream;
328 }
329 
330 unsigned
vl_vb_get_mv_stream_stride(struct vl_vertex_buffer * buffer)331 vl_vb_get_mv_stream_stride(struct vl_vertex_buffer *buffer)
332 {
333    assert(buffer);
334 
335    return buffer->width;
336 }
337 
338 struct vl_motionvector *
vl_vb_get_mv_stream(struct vl_vertex_buffer * buffer,int ref_frame)339 vl_vb_get_mv_stream(struct vl_vertex_buffer *buffer, int ref_frame)
340 {
341    assert(buffer);
342    assert(ref_frame < VL_MAX_REF_FRAMES);
343 
344    return buffer->mv[ref_frame].vertex_stream;
345 }
346 
347 void
vl_vb_unmap(struct vl_vertex_buffer * buffer,struct pipe_context * pipe)348 vl_vb_unmap(struct vl_vertex_buffer *buffer, struct pipe_context *pipe)
349 {
350    unsigned i;
351 
352    assert(buffer && pipe);
353 
354    for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
355       if (buffer->ycbcr[i].transfer)
356          pipe_buffer_unmap(pipe, buffer->ycbcr[i].transfer);
357    }
358 
359    for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
360       if (buffer->mv[i].transfer)
361          pipe_buffer_unmap(pipe, buffer->mv[i].transfer);
362    }
363 }
364 
365 void
vl_vb_cleanup(struct vl_vertex_buffer * buffer)366 vl_vb_cleanup(struct vl_vertex_buffer *buffer)
367 {
368    unsigned i;
369 
370    assert(buffer);
371 
372    for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
373       pipe_resource_reference(&buffer->ycbcr[i].resource, NULL);
374    }
375 
376    for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
377       pipe_resource_reference(&buffer->mv[i].resource, NULL);
378    }
379 }
380