blob: 8fac5c6c512984ddd69fc842f63eb8d771bb0789 [file] [log] [blame]
Maciej Cencora6bcbeb02009-07-11 15:00:52 +02001/*
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
36struct radeon_buffer_object *
37get_radeon_buffer_object(struct gl_buffer_object *obj)
38{
39 return (struct radeon_buffer_object *) obj;
40}
41
42static struct gl_buffer_object *
43radeonNewBufferObject(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 */
59static void
60radeonDeleteBufferObject(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.
Brian Paul2f6d2a92009-09-03 09:41:41 -060081 * Called via ctx->Driver.BufferData().
82 * \return GL_TRUE for success, GL_FALSE if out of memory
Maciej Cencora6bcbeb02009-07-11 15:00:52 +020083 */
Brian Paul2f6d2a92009-09-03 09:41:41 -060084static GLboolean
Maciej Cencora6bcbeb02009-07-11 15:00:52 +020085radeonBufferData(GLcontext * ctx,
86 GLenum target,
87 GLsizeiptrARB size,
88 const GLvoid * data,
89 GLenum usage,
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
98 if (radeon_obj->bo != NULL) {
99 radeon_bo_unref(radeon_obj->bo);
100 radeon_obj->bo = NULL;
101 }
102
103 if (size != 0) {
104 radeon_obj->bo = radeon_bo_open(radeon->radeonScreen->bom,
105 0,
106 size,
107 32,
108 RADEON_GEM_DOMAIN_GTT,
109 0);
Alex Deucherc3380de2009-08-17 15:42:19 -0400110
Brian Paul2f6d2a92009-09-03 09:41:41 -0600111 if (!radeon_obj->bo)
112 return GL_FALSE;
113
Maciej Cencora6bcbeb02009-07-11 15:00:52 +0200114 if (data != NULL) {
115 radeon_bo_map(radeon_obj->bo, GL_TRUE);
116
117 _mesa_memcpy(radeon_obj->bo->ptr, data, size);
118
119 radeon_bo_unmap(radeon_obj->bo);
120 }
121 }
Brian Paul2f6d2a92009-09-03 09:41:41 -0600122 return GL_TRUE;
Maciej Cencora6bcbeb02009-07-11 15:00:52 +0200123}
124
125/**
126 * Replace data in a subrange of buffer object. If the data range
127 * specified by size + offset extends beyond the end of the buffer or
128 * if data is NULL, no copy is performed.
129 * Called via glBufferSubDataARB().
130 */
131static void
132radeonBufferSubData(GLcontext * ctx,
133 GLenum target,
134 GLintptrARB offset,
135 GLsizeiptrARB size,
136 const GLvoid * data,
137 struct gl_buffer_object *obj)
138{
139 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
140
141 radeon_bo_map(radeon_obj->bo, GL_TRUE);
142
143 _mesa_memcpy(radeon_obj->bo->ptr + offset, data, size);
144
145 radeon_bo_unmap(radeon_obj->bo);
146}
147
148/**
149 * Called via glGetBufferSubDataARB()
150 */
151static void
152radeonGetBufferSubData(GLcontext * ctx,
153 GLenum target,
154 GLintptrARB offset,
155 GLsizeiptrARB size,
156 GLvoid * data,
157 struct gl_buffer_object *obj)
158{
159 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
160
161 radeon_bo_map(radeon_obj->bo, GL_FALSE);
162
163 _mesa_memcpy(data, radeon_obj->bo->ptr + offset, size);
164
165 radeon_bo_unmap(radeon_obj->bo);
166}
167
168/**
169 * Called via glMapBufferARB()
170 */
171static void *
172radeonMapBuffer(GLcontext * ctx,
173 GLenum target,
174 GLenum access,
175 struct gl_buffer_object *obj)
176{
177 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
178
179 if (access == GL_WRITE_ONLY_ARB) {
Maciej Cencora8d60c0b2009-06-11 16:00:03 +0200180 ctx->Driver.Flush(ctx);
Maciej Cencora6bcbeb02009-07-11 15:00:52 +0200181 }
182
183 if (radeon_obj->bo == NULL) {
184 obj->Pointer = NULL;
185 return NULL;
186 }
187
188 radeon_bo_map(radeon_obj->bo, access == GL_WRITE_ONLY_ARB);
189
Maciej Cencora4916a5a2009-09-20 13:54:59 +0200190 obj->Pointer = radeon_obj->bo->ptr;
191 obj->Length = obj->Size;
192 obj->Offset = 0;
193
194 return obj->Pointer;
Maciej Cencora6bcbeb02009-07-11 15:00:52 +0200195}
196
197
198/**
199 * Called via glUnmapBufferARB()
200 */
201static GLboolean
202radeonUnmapBuffer(GLcontext * ctx,
203 GLenum target,
204 struct gl_buffer_object *obj)
205{
206 struct radeon_buffer_object *radeon_obj = get_radeon_buffer_object(obj);
207
208 if (radeon_obj->bo != NULL) {
209 radeon_bo_unmap(radeon_obj->bo);
Maciej Cencora6bcbeb02009-07-11 15:00:52 +0200210 }
211
Maciej Cencora4916a5a2009-09-20 13:54:59 +0200212 obj->Pointer = NULL;
213 obj->Offset = 0;
214 obj->Length = 0;
215
Maciej Cencora6bcbeb02009-07-11 15:00:52 +0200216 return GL_TRUE;
217}
218
219void
220radeonInitBufferObjectFuncs(struct dd_function_table *functions)
221{
222 functions->NewBufferObject = radeonNewBufferObject;
223 functions->DeleteBuffer = radeonDeleteBufferObject;
224 functions->BufferData = radeonBufferData;
225 functions->BufferSubData = radeonBufferSubData;
226 functions->GetBufferSubData = radeonGetBufferSubData;
227 functions->MapBuffer = radeonMapBuffer;
228 functions->UnmapBuffer = radeonUnmapBuffer;
229}