r300: Zero-initialize register for NV_vertex_program
[mesa.git] / src / gallium / winsys / drm / radeon / core / radeon_buffer.c
1 /*
2 * Copyright © 2008 Jérôme Glisse
3 * 2009 Corbin Simpson
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27 /*
28 * Authors:
29 * Jérôme Glisse <glisse@freedesktop.org>
30 * Corbin Simpson <MostAwesomeDude@gmail.com>
31 */
32
33 #include "radeon_buffer.h"
34
35 static const char *radeon_get_name(struct pipe_winsys *ws)
36 {
37 return "Radeon/GEM+KMS";
38 }
39
40 static struct pipe_buffer *radeon_buffer_create(struct pipe_winsys *ws,
41 unsigned alignment,
42 unsigned usage,
43 unsigned size)
44 {
45 struct radeon_winsys *radeon_ws = (struct radeon_winsys *)ws;
46 struct radeon_pipe_buffer *radeon_buffer;
47 uint32_t domain;
48
49 radeon_buffer = CALLOC_STRUCT(radeon_pipe_buffer);
50 if (radeon_buffer == NULL) {
51 return NULL;
52 }
53
54 pipe_reference_init(&radeon_buffer->base.reference, 1);
55 radeon_buffer->base.alignment = alignment;
56 radeon_buffer->base.usage = usage;
57 radeon_buffer->base.size = size;
58
59 domain = 0;
60
61 if (usage & PIPE_BUFFER_USAGE_PIXEL) {
62 domain |= RADEON_GEM_DOMAIN_VRAM;
63 }
64 if (usage & PIPE_BUFFER_USAGE_VERTEX) {
65 domain |= RADEON_GEM_DOMAIN_GTT;
66 }
67 if (usage & PIPE_BUFFER_USAGE_INDEX) {
68 domain |= RADEON_GEM_DOMAIN_GTT;
69 }
70
71 radeon_buffer->bo = radeon_bo_open(radeon_ws->priv->bom, 0, size,
72 alignment, domain, 0);
73 if (radeon_buffer->bo == NULL) {
74 FREE(radeon_buffer);
75 return NULL;
76 }
77 return &radeon_buffer->base;
78 }
79
80 static struct pipe_buffer *radeon_buffer_user_create(struct pipe_winsys *ws,
81 void *ptr,
82 unsigned bytes)
83 {
84 struct radeon_pipe_buffer *radeon_buffer;
85
86 radeon_buffer =
87 (struct radeon_pipe_buffer*)radeon_buffer_create(ws, 0, 0, bytes);
88 if (radeon_buffer == NULL) {
89 return NULL;
90 }
91 radeon_bo_map(radeon_buffer->bo, 1);
92 memcpy(radeon_buffer->bo->ptr, ptr, bytes);
93 radeon_bo_unmap(radeon_buffer->bo);
94 return &radeon_buffer->base;
95 }
96
97 static struct pipe_buffer *radeon_surface_buffer_create(struct pipe_winsys *ws,
98 unsigned width,
99 unsigned height,
100 enum pipe_format format,
101 unsigned usage,
102 unsigned *stride)
103 {
104 struct pipe_format_block block;
105 unsigned nblocksx, nblocksy, size;
106
107 pf_get_block(format, &block);
108
109 nblocksx = pf_get_nblocksx(&block, width);
110 nblocksy = pf_get_nblocksy(&block, height);
111
112 /* Radeons enjoy things in multiples of 32. */
113 /* XXX this can be 32 when POT */
114 *stride = (nblocksx * block.size + 63) & ~63;
115 size = *stride * nblocksy;
116
117 return radeon_buffer_create(ws, 64, usage, size);
118 }
119
120 static void radeon_buffer_del(struct pipe_buffer *buffer)
121 {
122 struct radeon_pipe_buffer *radeon_buffer =
123 (struct radeon_pipe_buffer*)buffer;
124
125 radeon_bo_unref(radeon_buffer->bo);
126 free(radeon_buffer);
127 }
128
129 static void *radeon_buffer_map(struct pipe_winsys *ws,
130 struct pipe_buffer *buffer,
131 unsigned flags)
132 {
133 struct radeon_pipe_buffer *radeon_buffer =
134 (struct radeon_pipe_buffer*)buffer;
135 int write = 0;
136
137 if (!(flags & PIPE_BUFFER_USAGE_DONTBLOCK)) {
138 radeon_bo_wait(radeon_buffer->bo);
139 }
140 if (flags & PIPE_BUFFER_USAGE_CPU_WRITE) {
141 write = 1;
142 }
143
144 if (radeon_bo_map(radeon_buffer->bo, write)) {
145 return NULL;
146 }
147
148 return radeon_buffer->bo->ptr;
149 }
150
151 static void radeon_buffer_unmap(struct pipe_winsys *ws,
152 struct pipe_buffer *buffer)
153 {
154 struct radeon_pipe_buffer *radeon_buffer =
155 (struct radeon_pipe_buffer*)buffer;
156
157 radeon_bo_unmap(radeon_buffer->bo);
158 }
159
160 static void radeon_fence_reference(struct pipe_winsys *ws,
161 struct pipe_fence_handle **ptr,
162 struct pipe_fence_handle *pfence)
163 {
164 }
165
166 static int radeon_fence_signalled(struct pipe_winsys *ws,
167 struct pipe_fence_handle *pfence,
168 unsigned flag)
169 {
170 return 1;
171 }
172
173 static int radeon_fence_finish(struct pipe_winsys *ws,
174 struct pipe_fence_handle *pfence,
175 unsigned flag)
176 {
177 return 0;
178 }
179
180 static void radeon_flush_frontbuffer(struct pipe_winsys *pipe_winsys,
181 struct pipe_surface *pipe_surface,
182 void *context_private)
183 {
184 /* XXX TODO: call dri2CopyRegion */
185 }
186
187 struct radeon_winsys* radeon_pipe_winsys(int fd)
188 {
189 struct radeon_winsys* radeon_ws;
190 struct radeon_bo_manager* bom;
191
192 radeon_ws = CALLOC_STRUCT(radeon_winsys);
193 if (radeon_ws == NULL) {
194 return NULL;
195 }
196
197 radeon_ws->priv = CALLOC_STRUCT(radeon_winsys_priv);
198 if (radeon_ws->priv == NULL) {
199 FREE(radeon_ws);
200 return NULL;
201 }
202
203 radeon_ws->priv->fd = fd;
204 radeon_ws->priv->bom = radeon_bo_manager_gem_ctor(fd);
205
206 radeon_ws->base.flush_frontbuffer = radeon_flush_frontbuffer;
207
208 radeon_ws->base.buffer_create = radeon_buffer_create;
209 radeon_ws->base.user_buffer_create = radeon_buffer_user_create;
210 radeon_ws->base.surface_buffer_create = radeon_surface_buffer_create;
211 radeon_ws->base.buffer_map = radeon_buffer_map;
212 radeon_ws->base.buffer_unmap = radeon_buffer_unmap;
213 radeon_ws->base.buffer_destroy = radeon_buffer_del;
214
215 radeon_ws->base.fence_reference = radeon_fence_reference;
216 radeon_ws->base.fence_signalled = radeon_fence_signalled;
217 radeon_ws->base.fence_finish = radeon_fence_finish;
218
219 radeon_ws->base.get_name = radeon_get_name;
220
221 return radeon_ws;
222 }
223 #if 0
224 static struct pipe_buffer *radeon_buffer_from_handle(struct radeon_screen *radeon_screen,
225 uint32_t handle)
226 {
227 struct radeon_pipe_buffer *radeon_buffer;
228 struct radeon_bo *bo = NULL;
229
230 bo = radeon_bo_open(radeon_screen->bom, handle, 0, 0, 0, 0);
231 if (bo == NULL) {
232 return NULL;
233 }
234 radeon_buffer = calloc(1, sizeof(struct radeon_pipe_buffer));
235 if (radeon_buffer == NULL) {
236 radeon_bo_unref(bo);
237 return NULL;
238 }
239 pipe_reference_init(&radeon_buffer->base.reference, 1);
240 radeon_buffer->base.usage = PIPE_BUFFER_USAGE_PIXEL;
241 radeon_buffer->bo = bo;
242 return &radeon_buffer->base;
243 }
244
245 struct pipe_surface *radeon_surface_from_handle(struct radeon_context *radeon_context,
246 uint32_t handle,
247 enum pipe_format format,
248 int w, int h, int pitch)
249 {
250 struct pipe_screen *pipe_screen = radeon_context->pipe_screen;
251 struct pipe_winsys *pipe_winsys = radeon_context->pipe_winsys;
252 struct pipe_texture tmpl;
253 struct pipe_surface *ps;
254 struct pipe_texture *pt;
255 struct pipe_buffer *pb;
256
257 pb = radeon_buffer_from_handle(radeon_context->radeon_screen, handle);
258 if (pb == NULL) {
259 return NULL;
260 }
261 memset(&tmpl, 0, sizeof(tmpl));
262 tmpl.tex_usage = PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
263 tmpl.target = PIPE_TEXTURE_2D;
264 tmpl.width[0] = w;
265 tmpl.height[0] = h;
266 tmpl.depth[0] = 1;
267 tmpl.format = format;
268 pf_get_block(tmpl.format, &tmpl.block);
269 tmpl.nblocksx[0] = pf_get_nblocksx(&tmpl.block, w);
270 tmpl.nblocksy[0] = pf_get_nblocksy(&tmpl.block, h);
271
272 pt = pipe_screen->texture_blanket(pipe_screen, &tmpl, &pitch, pb);
273 if (pt == NULL) {
274 pipe_buffer_reference(&pb, NULL);
275 }
276 ps = pipe_screen->get_tex_surface(pipe_screen, pt, 0, 0, 0,
277 PIPE_BUFFER_USAGE_GPU_WRITE);
278 return ps;
279 }
280 #endif