Merge branch 'master' into asm-shader-rework-1
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_buffer_objects.c
1 /*
2 * Copyright 2009 Maciej Cencora <m.cencora@gmail.com>
3 *
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, sublicense, 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
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #include "radeon_buffer_objects.h"
29
30 #include "main/imports.h"
31 #include "main/mtypes.h"
32 #include "main/bufferobj.h"
33
34 #include "radeon_common.h"
35
36 struct radeon_buffer_object *
37 get_radeon_buffer_object(struct gl_buffer_object *obj)
38 {
39 return (struct radeon_buffer_object *) obj;
40 }
41
42 static struct gl_buffer_object *
43 radeonNewBufferObject(GLcontext * ctx,
44 GLuint name,
45 GLenum target)
46 {
47 struct radeon_buffer_object *obj = CALLOC_STRUCT(radeon_buffer_object);
48
49 _mesa_initialize_buffer_object(&obj->Base, name, target);
50
51 obj->bo = NULL;
52
53 return &obj->Base;
54 }
55
56 /**
57 * Called via glDeleteBuffersARB().
58 */
59 static void
60 radeonDeleteBufferObject(GLcontext * ctx,
61 struct gl_buffer_object *obj)
62 {
63 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
64
65 if (obj->Pointer) {
66 radeon_bo_unmap(radeon_obj->bo);
67 }
68
69 if (radeon_obj->bo) {
70 radeon_bo_unref(radeon_obj->bo);
71 }
72
73 _mesa_free(radeon_obj);
74 }
75
76
77 /**
78 * Allocate space for and store data in a buffer object. Any data that was
79 * previously stored in the buffer object is lost. If data is NULL,
80 * memory will be allocated, but no copy will occur.
81 * Called via glBufferDataARB().
82 */
83 static void
84 radeonBufferData(GLcontext * ctx,
85 GLenum target,
86 GLsizeiptrARB size,
87 const GLvoid * data,
88 GLenum usage,
89 struct gl_buffer_object *obj)
90 {
91 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
92 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
93
94 radeon_obj->Base.Size = size;
95 radeon_obj->Base.Usage = usage;
96
97 if (radeon_obj->bo != NULL) {
98 radeon_bo_unref(radeon_obj->bo);
99 radeon_obj->bo = NULL;
100 }
101
102 if (size != 0) {
103 radeon_obj->bo = radeon_bo_open(radeon->radeonScreen->bom,
104 0,
105 size,
106 32,
107 RADEON_GEM_DOMAIN_GTT,
108 0);
109
110 if (data != NULL) {
111 radeon_bo_map(radeon_obj->bo, GL_TRUE);
112
113 _mesa_memcpy(radeon_obj->bo->ptr, data, size);
114
115 radeon_bo_unmap(radeon_obj->bo);
116 }
117 }
118 }
119
120 /**
121 * Replace data in a subrange of buffer object. If the data range
122 * specified by size + offset extends beyond the end of the buffer or
123 * if data is NULL, no copy is performed.
124 * Called via glBufferSubDataARB().
125 */
126 static void
127 radeonBufferSubData(GLcontext * ctx,
128 GLenum target,
129 GLintptrARB offset,
130 GLsizeiptrARB size,
131 const GLvoid * data,
132 struct gl_buffer_object *obj)
133 {
134 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
135
136 radeon_bo_map(radeon_obj->bo, GL_TRUE);
137
138 _mesa_memcpy(radeon_obj->bo->ptr + offset, data, size);
139
140 radeon_bo_unmap(radeon_obj->bo);
141 }
142
143 /**
144 * Called via glGetBufferSubDataARB()
145 */
146 static void
147 radeonGetBufferSubData(GLcontext * ctx,
148 GLenum target,
149 GLintptrARB offset,
150 GLsizeiptrARB size,
151 GLvoid * data,
152 struct gl_buffer_object *obj)
153 {
154 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
155
156 radeon_bo_map(radeon_obj->bo, GL_FALSE);
157
158 _mesa_memcpy(data, radeon_obj->bo->ptr + offset, size);
159
160 radeon_bo_unmap(radeon_obj->bo);
161 }
162
163 /**
164 * Called via glMapBufferARB()
165 */
166 static void *
167 radeonMapBuffer(GLcontext * ctx,
168 GLenum target,
169 GLenum access,
170 struct gl_buffer_object *obj)
171 {
172 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
173
174 if (access == GL_WRITE_ONLY_ARB) {
175 ctx->Driver.Flush(ctx);
176 }
177
178 if (radeon_obj->bo == NULL) {
179 obj->Pointer = NULL;
180 return NULL;
181 }
182
183 radeon_bo_map(radeon_obj->bo, access == GL_WRITE_ONLY_ARB);
184
185 return obj->Pointer = radeon_obj->bo->ptr;
186 }
187
188
189 /**
190 * Called via glUnmapBufferARB()
191 */
192 static GLboolean
193 radeonUnmapBuffer(GLcontext * ctx,
194 GLenum target,
195 struct gl_buffer_object *obj)
196 {
197 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
198
199 if (radeon_obj->bo != NULL) {
200 radeon_bo_unmap(radeon_obj->bo);
201 obj->Pointer = NULL;
202 }
203
204 return GL_TRUE;
205 }
206
207 void
208 radeonInitBufferObjectFuncs(struct dd_function_table *functions)
209 {
210 functions->NewBufferObject = radeonNewBufferObject;
211 functions->DeleteBuffer = radeonDeleteBufferObject;
212 functions->BufferData = radeonBufferData;
213 functions->BufferSubData = radeonBufferSubData;
214 functions->GetBufferSubData = radeonGetBufferSubData;
215 functions->MapBuffer = radeonMapBuffer;
216 functions->UnmapBuffer = radeonUnmapBuffer;
217 }