Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / drivers / r600 / r600_upload.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse <jglisse@redhat.com>
25 */
26 #include <errno.h>
27 #include "util/u_inlines.h"
28 #include "util/u_memory.h"
29 #include "r600.h"
30 #include "r600_pipe.h"
31 #include "r600_resource.h"
32
33 struct r600_upload {
34 struct r600_pipe_context *rctx;
35 struct r600_bo *buffer;
36 char *ptr;
37 unsigned size;
38 unsigned default_size;
39 unsigned total_alloc_size;
40 unsigned offset;
41 unsigned alignment;
42 };
43
44 struct r600_upload *r600_upload_create(struct r600_pipe_context *rctx,
45 unsigned default_size,
46 unsigned alignment)
47 {
48 struct r600_upload *upload = CALLOC_STRUCT(r600_upload);
49
50 if (upload == NULL)
51 return NULL;
52
53 upload->rctx = rctx;
54 upload->size = 0;
55 upload->default_size = default_size;
56 upload->alignment = alignment;
57 upload->ptr = NULL;
58 upload->buffer = NULL;
59 upload->total_alloc_size = 0;
60
61 return upload;
62 }
63
64 void r600_upload_flush(struct r600_upload *upload)
65 {
66 if (upload->buffer) {
67 r600_bo_reference(upload->rctx->radeon, &upload->buffer, NULL);
68 }
69 upload->default_size = MAX2(upload->total_alloc_size, upload->default_size);
70 upload->total_alloc_size = 0;
71 upload->size = 0;
72 upload->offset = 0;
73 upload->ptr = NULL;
74 upload->buffer = NULL;
75 }
76
77 void r600_upload_destroy(struct r600_upload *upload)
78 {
79 r600_upload_flush(upload);
80 FREE(upload);
81 }
82
83 int r600_upload_buffer(struct r600_upload *upload, unsigned offset,
84 unsigned size, struct r600_resource_buffer *in_buffer,
85 unsigned *out_offset, unsigned *out_size,
86 struct r600_bo **out_buffer)
87 {
88 unsigned alloc_size = align(size, upload->alignment);
89 const void *in_ptr = NULL;
90
91 if (upload->offset + alloc_size > upload->size) {
92 if (upload->size) {
93 r600_bo_reference(upload->rctx->radeon, &upload->buffer, NULL);
94 }
95 upload->size = align(MAX2(upload->default_size, alloc_size), 4096);
96 upload->total_alloc_size += upload->size;
97 upload->offset = 0;
98 upload->buffer = r600_bo(upload->rctx->radeon, upload->size, 4096, PIPE_BIND_VERTEX_BUFFER, 0);
99 if (upload->buffer == NULL) {
100 return -ENOMEM;
101 }
102 upload->ptr = r600_bo_map(upload->rctx->radeon, upload->buffer, 0, NULL);
103 }
104
105 in_ptr = in_buffer->user_buffer;
106 memcpy(upload->ptr + upload->offset, (uint8_t *) in_ptr + offset, size);
107 *out_offset = upload->offset;
108 *out_size = upload->size;
109 *out_buffer = NULL;
110 r600_bo_reference(upload->rctx->radeon, out_buffer, upload->buffer);
111 upload->offset += alloc_size;
112
113 return 0;
114 }