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