mesa: add storage flags parameter to Driver.BufferData
[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 "main/imports.h"
29 #include "main/mtypes.h"
30 #include "main/bufferobj.h"
31
32 #include "radeon_common.h"
33 #include "radeon_buffer_objects.h"
34
35 struct radeon_buffer_object *
36 get_radeon_buffer_object(struct gl_buffer_object *obj)
37 {
38 return (struct radeon_buffer_object *) obj;
39 }
40
41 static struct gl_buffer_object *
42 radeonNewBufferObject(struct gl_context * ctx,
43 GLuint name,
44 GLenum target)
45 {
46 struct radeon_buffer_object *obj = CALLOC_STRUCT(radeon_buffer_object);
47
48 _mesa_initialize_buffer_object(ctx, &obj->Base, name, target);
49
50 obj->bo = NULL;
51
52 return &obj->Base;
53 }
54
55 /**
56 * Called via glDeleteBuffersARB().
57 */
58 static void
59 radeonDeleteBufferObject(struct gl_context * ctx,
60 struct gl_buffer_object *obj)
61 {
62 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
63
64 if (obj->Pointer) {
65 radeon_bo_unmap(radeon_obj->bo);
66 }
67
68 if (radeon_obj->bo) {
69 radeon_bo_unref(radeon_obj->bo);
70 }
71
72 free(radeon_obj);
73 }
74
75
76 /**
77 * Allocate space for and store data in a buffer object. Any data that was
78 * previously stored in the buffer object is lost. If data is NULL,
79 * memory will be allocated, but no copy will occur.
80 * Called via ctx->Driver.BufferData().
81 * \return GL_TRUE for success, GL_FALSE if out of memory
82 */
83 static GLboolean
84 radeonBufferData(struct gl_context * ctx,
85 GLenum target,
86 GLsizeiptrARB size,
87 const GLvoid * data,
88 GLenum usage,
89 GLbitfield storageFlags,
90 struct gl_buffer_object *obj)
91 {
92 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
93 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
94
95 radeon_obj->Base.Size = size;
96 radeon_obj->Base.Usage = usage;
97 radeon_obj->Base.StorageFlags = storageFlags;
98
99 if (radeon_obj->bo != NULL) {
100 radeon_bo_unref(radeon_obj->bo);
101 radeon_obj->bo = NULL;
102 }
103
104 if (size != 0) {
105 radeon_obj->bo = radeon_bo_open(radeon->radeonScreen->bom,
106 0,
107 size,
108 ctx->Const.MinMapBufferAlignment,
109 RADEON_GEM_DOMAIN_GTT,
110 0);
111
112 if (!radeon_obj->bo)
113 return GL_FALSE;
114
115 if (data != NULL) {
116 radeon_bo_map(radeon_obj->bo, GL_TRUE);
117
118 memcpy(radeon_obj->bo->ptr, data, size);
119
120 radeon_bo_unmap(radeon_obj->bo);
121 }
122 }
123 return GL_TRUE;
124 }
125
126 /**
127 * Replace data in a subrange of buffer object. If the data range
128 * specified by size + offset extends beyond the end of the buffer or
129 * if data is NULL, no copy is performed.
130 * Called via glBufferSubDataARB().
131 */
132 static void
133 radeonBufferSubData(struct gl_context * ctx,
134 GLintptrARB offset,
135 GLsizeiptrARB size,
136 const GLvoid * data,
137 struct gl_buffer_object *obj)
138 {
139 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
140 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
141
142 if (radeon_bo_is_referenced_by_cs(radeon_obj->bo, radeon->cmdbuf.cs)) {
143 radeon_firevertices(radeon);
144 }
145
146 radeon_bo_map(radeon_obj->bo, GL_TRUE);
147
148 memcpy(radeon_obj->bo->ptr + offset, data, size);
149
150 radeon_bo_unmap(radeon_obj->bo);
151 }
152
153 /**
154 * Called via glGetBufferSubDataARB()
155 */
156 static void
157 radeonGetBufferSubData(struct gl_context * ctx,
158 GLintptrARB offset,
159 GLsizeiptrARB size,
160 GLvoid * data,
161 struct gl_buffer_object *obj)
162 {
163 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
164
165 radeon_bo_map(radeon_obj->bo, GL_FALSE);
166
167 memcpy(data, radeon_obj->bo->ptr + offset, size);
168
169 radeon_bo_unmap(radeon_obj->bo);
170 }
171
172 /**
173 * Called via glMapBuffer() and glMapBufferRange()
174 */
175 static void *
176 radeonMapBufferRange(struct gl_context * ctx,
177 GLintptr offset, GLsizeiptr length,
178 GLbitfield access, struct gl_buffer_object *obj)
179 {
180 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
181 const GLboolean write_only =
182 (access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == GL_MAP_WRITE_BIT;
183
184 if (write_only) {
185 ctx->Driver.Flush(ctx);
186 }
187
188 if (radeon_obj->bo == NULL) {
189 obj->Pointer = NULL;
190 return NULL;
191 }
192
193 obj->Offset = offset;
194 obj->Length = length;
195 obj->AccessFlags = access;
196
197 radeon_bo_map(radeon_obj->bo, write_only);
198
199 obj->Pointer = radeon_obj->bo->ptr + offset;
200 return obj->Pointer;
201 }
202
203
204 /**
205 * Called via glUnmapBufferARB()
206 */
207 static GLboolean
208 radeonUnmapBuffer(struct gl_context * ctx,
209 struct gl_buffer_object *obj)
210 {
211 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
212
213 if (radeon_obj->bo != NULL) {
214 radeon_bo_unmap(radeon_obj->bo);
215 }
216
217 obj->Pointer = NULL;
218 obj->Offset = 0;
219 obj->Length = 0;
220
221 return GL_TRUE;
222 }
223
224 void
225 radeonInitBufferObjectFuncs(struct dd_function_table *functions)
226 {
227 functions->NewBufferObject = radeonNewBufferObject;
228 functions->DeleteBuffer = radeonDeleteBufferObject;
229 functions->BufferData = radeonBufferData;
230 functions->BufferSubData = radeonBufferSubData;
231 functions->GetBufferSubData = radeonGetBufferSubData;
232 functions->MapBufferRange = radeonMapBufferRange;
233 functions->UnmapBuffer = radeonUnmapBuffer;
234 }