summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/vl/vl_vertex_buffers.c
blob: d2e03988acfbe7c1754847f74ebc7b53b3ff7a18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**************************************************************************
 *
 * Copyright 2010 Christian König
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include <assert.h>
#include <util/u_format.h>
#include "vl_vertex_buffers.h"
#include "vl_types.h"

/* vertices for a quad covering a block */
static const struct vertex2f block_quad[4] = {
   {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
};

struct pipe_vertex_buffer
vl_vb_upload_quads(struct pipe_context *pipe)
{
   struct pipe_vertex_buffer quad;
   struct pipe_transfer *buf_transfer;
   struct vertex2f *v;

   unsigned i;

   assert(pipe);

   /* create buffer */
   quad.stride = sizeof(struct vertex2f);
   quad.buffer_offset = 0;
   quad.buffer = pipe_buffer_create
   (
      pipe->screen,
      PIPE_BIND_VERTEX_BUFFER,
      PIPE_USAGE_STATIC,
      sizeof(struct vertex2f) * 4
   );

   if(!quad.buffer)
      return quad;

   /* and fill it */
   v = pipe_buffer_map
   (
      pipe,
      quad.buffer,
      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
      &buf_transfer
   );

   for (i = 0; i < 4; ++i, ++v) {
      v->x = block_quad[i].x;
      v->y = block_quad[i].y;
   }

   pipe_buffer_unmap(pipe, buf_transfer);

   return quad;
}

struct pipe_vertex_buffer
vl_vb_upload_pos(struct pipe_context *pipe, unsigned width, unsigned height)
{
   struct pipe_vertex_buffer pos;
   struct pipe_transfer *buf_transfer;
   struct vertex2s *v;

   unsigned x, y;

   assert(pipe);

   /* create buffer */
   pos.stride = sizeof(struct vertex2s);
   pos.buffer_offset = 0;
   pos.buffer = pipe_buffer_create
   (
      pipe->screen,
      PIPE_BIND_VERTEX_BUFFER,
      PIPE_USAGE_STATIC,
      sizeof(struct vertex2s) * width * height
   );

   if(!pos.buffer)
      return pos;

   /* and fill it */
   v = pipe_buffer_map
   (
      pipe,
      pos.buffer,
      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
      &buf_transfer
   );

   for ( y = 0; y < height; ++y) {
      for ( x = 0; x < width; ++x, ++v) {
         v->x = x;
         v->y = y;
      }
   }

   pipe_buffer_unmap(pipe, buf_transfer);

   return pos;
}

static struct pipe_vertex_element
vl_vb_get_quad_vertex_element(void)
{
   struct pipe_vertex_element element;

   /* setup rectangle element */
   element.src_offset = 0;
   element.instance_divisor = 0;
   element.vertex_buffer_index = 0;
   element.src_format = PIPE_FORMAT_R32G32_FLOAT;

   return element;
}

static void
vl_vb_element_helper(struct pipe_vertex_element* elements, unsigned num_elements,
                     unsigned vertex_buffer_index)
{
   unsigned i, offset = 0;

   assert(elements && num_elements);

   for ( i = 0; i < num_elements; ++i ) {
      elements[i].src_offset = offset;
      elements[i].instance_divisor = 1;
      elements[i].vertex_buffer_index = vertex_buffer_index;
      offset += util_format_get_blocksize(elements[i].src_format);
   }
}

void *
vl_vb_get_ves_ycbcr(struct pipe_context *pipe)
{
   struct pipe_vertex_element vertex_elems[NUM_VS_INPUTS];

   assert(pipe);

   memset(&vertex_elems, 0, sizeof(vertex_elems));
   vertex_elems[VS_I_RECT] = vl_vb_get_quad_vertex_element();

   /* Position element */
   vertex_elems[VS_I_VPOS].src_format = PIPE_FORMAT_R8G8B8A8_USCALED;

   vl_vb_element_helper(&vertex_elems[VS_I_VPOS], 1, 1);

   return pipe->create_vertex_elements_state(pipe, 2, vertex_elems);
}

void *
vl_vb_get_ves_mv(struct pipe_context *pipe)
{
   struct pipe_vertex_element vertex_elems[NUM_VS_INPUTS];

   assert(pipe);

   memset(&vertex_elems, 0, sizeof(vertex_elems));
   vertex_elems[VS_I_RECT] = vl_vb_get_quad_vertex_element();

   /* Position element */
   vertex_elems[VS_I_VPOS].src_format = PIPE_FORMAT_R16G16_SSCALED;

   vl_vb_element_helper(&vertex_elems[VS_I_VPOS], 1, 1);

   /* motion vector TOP element */
   vertex_elems[VS_I_MV_TOP].src_format = PIPE_FORMAT_R16G16B16A16_SSCALED;

   /* motion vector BOTTOM element */
   vertex_elems[VS_I_MV_BOTTOM].src_format = PIPE_FORMAT_R16G16B16A16_SSCALED;

   vl_vb_element_helper(&vertex_elems[VS_I_MV_TOP], 2, 2);

   return pipe->create_vertex_elements_state(pipe, NUM_VS_INPUTS, vertex_elems);
}

void
vl_vb_init(struct vl_vertex_buffer *buffer, struct pipe_context *pipe,
           unsigned width, unsigned height)
{
   unsigned i, size;

   assert(buffer);

   buffer->width = width;
   buffer->height = height;

   size = width * height;

   for (i = 0; i < VL_MAX_PLANES; ++i) {
      buffer->ycbcr[i].resource = pipe_buffer_create
      (
         pipe->screen,
         PIPE_BIND_VERTEX_BUFFER,
         PIPE_USAGE_STREAM,
         sizeof(struct pipe_ycbcr_block) * size * 4
      );
   }

   for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
      buffer->mv[i].resource = pipe_buffer_create
      (
         pipe->screen,
         PIPE_BIND_VERTEX_BUFFER,
         PIPE_USAGE_STREAM,
         sizeof(struct pipe_motionvector) * size
      );
   }

   vl_vb_map(buffer, pipe);
}

struct pipe_vertex_buffer
vl_vb_get_ycbcr(struct vl_vertex_buffer *buffer, int component)
{
   struct pipe_vertex_buffer buf;

   assert(buffer);

   buf.stride = sizeof(struct pipe_ycbcr_block);
   buf.buffer_offset = 0;
   buf.buffer = buffer->ycbcr[component].resource;

   return buf;
}

struct pipe_vertex_buffer
vl_vb_get_mv(struct vl_vertex_buffer *buffer, int motionvector)
{
   struct pipe_vertex_buffer buf;

   assert(buffer);

   buf.stride = sizeof(struct pipe_motionvector);
   buf.buffer_offset = 0;
   buf.buffer = buffer->mv[motionvector].resource;

   return buf;
}

void
vl_vb_map(struct vl_vertex_buffer *buffer, struct pipe_context *pipe)
{
   unsigned i;

   assert(buffer && pipe);

   for (i = 0; i < VL_MAX_PLANES; ++i) {
      buffer->ycbcr[i].vertex_stream = pipe_buffer_map
      (
         pipe,
         buffer->ycbcr[i].resource,
         PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
         &buffer->ycbcr[i].transfer
      );
   }

   for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
      buffer->mv[i].vertex_stream = pipe_buffer_map
      (
         pipe,
         buffer->mv[i].resource,
         PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
         &buffer->mv[i].transfer
      );
   }

}

struct pipe_ycbcr_block *
vl_vb_get_ycbcr_stream(struct vl_vertex_buffer *buffer, int component)
{
   assert(buffer);
   assert(component < VL_MAX_PLANES);

   return buffer->ycbcr[component].vertex_stream;
}

unsigned
vl_vb_get_mv_stream_stride(struct vl_vertex_buffer *buffer)
{
   assert(buffer);

   return buffer->width;
}

struct pipe_motionvector *
vl_vb_get_mv_stream(struct vl_vertex_buffer *buffer, int ref_frame)
{
   assert(buffer);
   assert(ref_frame < VL_MAX_REF_FRAMES);

   return buffer->mv[ref_frame].vertex_stream;
}

void
vl_vb_unmap(struct vl_vertex_buffer *buffer, struct pipe_context *pipe)
{
   unsigned i;

   assert(buffer && pipe);

   for (i = 0; i < VL_MAX_PLANES; ++i) {
      pipe_buffer_unmap(pipe, buffer->ycbcr[i].transfer);
   }

   for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
      pipe_buffer_unmap(pipe, buffer->mv[i].transfer);
   }
}

void
vl_vb_cleanup(struct vl_vertex_buffer *buffer)
{
   unsigned i;

   assert(buffer);

   for (i = 0; i < VL_MAX_PLANES; ++i) {
      pipe_resource_reference(&buffer->ycbcr[i].resource, NULL);
   }

   for (i = 0; i < VL_MAX_REF_FRAMES; ++i) {
      pipe_resource_reference(&buffer->mv[i].resource, NULL);
   }
}