2 * Copyright 2009 Maciej Cencora <m.cencora@gmail.com>
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:
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.
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.
28 #include "main/imports.h"
29 #include "main/mtypes.h"
30 #include "main/bufferobj.h"
32 #include "radeon_common.h"
33 #include "radeon_buffer_objects.h"
35 struct radeon_buffer_object
*
36 get_radeon_buffer_object(struct gl_buffer_object
*obj
)
38 return (struct radeon_buffer_object
*) obj
;
41 static struct gl_buffer_object
*
42 radeonNewBufferObject(struct gl_context
* ctx
,
45 struct radeon_buffer_object
*obj
= CALLOC_STRUCT(radeon_buffer_object
);
47 _mesa_initialize_buffer_object(ctx
, &obj
->Base
, name
);
55 * Called via glDeleteBuffersARB().
58 radeonDeleteBufferObject(struct gl_context
* ctx
,
59 struct gl_buffer_object
*obj
)
61 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
64 for (i
= 0; i
< MAP_COUNT
; i
++) {
65 if (obj
->Mappings
[i
].Pointer
) {
66 radeon_bo_unmap(radeon_obj
->bo
);
71 radeon_bo_unref(radeon_obj
->bo
);
79 * Allocate space for and store data in a buffer object. Any data that was
80 * previously stored in the buffer object is lost. If data is NULL,
81 * memory will be allocated, but no copy will occur.
82 * Called via ctx->Driver.BufferData().
83 * \return GL_TRUE for success, GL_FALSE if out of memory
86 radeonBufferData(struct gl_context
* ctx
,
91 GLbitfield storageFlags
,
92 struct gl_buffer_object
*obj
)
94 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
95 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
97 radeon_obj
->Base
.Size
= size
;
98 radeon_obj
->Base
.Usage
= usage
;
99 radeon_obj
->Base
.StorageFlags
= storageFlags
;
101 if (radeon_obj
->bo
!= NULL
) {
102 radeon_bo_unref(radeon_obj
->bo
);
103 radeon_obj
->bo
= NULL
;
107 radeon_obj
->bo
= radeon_bo_open(radeon
->radeonScreen
->bom
,
110 ctx
->Const
.MinMapBufferAlignment
,
111 RADEON_GEM_DOMAIN_GTT
,
118 radeon_bo_map(radeon_obj
->bo
, GL_TRUE
);
120 memcpy(radeon_obj
->bo
->ptr
, data
, size
);
122 radeon_bo_unmap(radeon_obj
->bo
);
129 * Replace data in a subrange of buffer object. If the data range
130 * specified by size + offset extends beyond the end of the buffer or
131 * if data is NULL, no copy is performed.
132 * Called via glBufferSubDataARB().
135 radeonBufferSubData(struct gl_context
* ctx
,
139 struct gl_buffer_object
*obj
)
141 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
142 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
144 if (radeon_bo_is_referenced_by_cs(radeon_obj
->bo
, radeon
->cmdbuf
.cs
)) {
145 radeon_firevertices(radeon
);
148 radeon_bo_map(radeon_obj
->bo
, GL_TRUE
);
150 memcpy(radeon_obj
->bo
->ptr
+ offset
, data
, size
);
152 radeon_bo_unmap(radeon_obj
->bo
);
156 * Called via glGetBufferSubDataARB()
159 radeonGetBufferSubData(struct gl_context
* ctx
,
163 struct gl_buffer_object
*obj
)
165 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
167 radeon_bo_map(radeon_obj
->bo
, GL_FALSE
);
169 memcpy(data
, radeon_obj
->bo
->ptr
+ offset
, size
);
171 radeon_bo_unmap(radeon_obj
->bo
);
175 * Called via glMapBuffer() and glMapBufferRange()
178 radeonMapBufferRange(struct gl_context
* ctx
,
179 GLintptr offset
, GLsizeiptr length
,
180 GLbitfield access
, struct gl_buffer_object
*obj
,
181 gl_map_buffer_index index
)
183 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
184 const GLboolean write_only
=
185 (access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == GL_MAP_WRITE_BIT
;
188 ctx
->Driver
.Flush(ctx
);
191 if (radeon_obj
->bo
== NULL
) {
192 obj
->Mappings
[index
].Pointer
= NULL
;
196 obj
->Mappings
[index
].Offset
= offset
;
197 obj
->Mappings
[index
].Length
= length
;
198 obj
->Mappings
[index
].AccessFlags
= access
;
200 radeon_bo_map(radeon_obj
->bo
, write_only
);
202 obj
->Mappings
[index
].Pointer
= radeon_obj
->bo
->ptr
+ offset
;
203 return obj
->Mappings
[index
].Pointer
;
208 * Called via glUnmapBufferARB()
211 radeonUnmapBuffer(struct gl_context
* ctx
,
212 struct gl_buffer_object
*obj
,
213 gl_map_buffer_index index
)
215 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
217 if (radeon_obj
->bo
!= NULL
) {
218 radeon_bo_unmap(radeon_obj
->bo
);
221 obj
->Mappings
[index
].Pointer
= NULL
;
222 obj
->Mappings
[index
].Offset
= 0;
223 obj
->Mappings
[index
].Length
= 0;
229 radeonInitBufferObjectFuncs(struct dd_function_table
*functions
)
231 functions
->NewBufferObject
= radeonNewBufferObject
;
232 functions
->DeleteBuffer
= radeonDeleteBufferObject
;
233 functions
->BufferData
= radeonBufferData
;
234 functions
->BufferSubData
= radeonBufferSubData
;
235 functions
->GetBufferSubData
= radeonGetBufferSubData
;
236 functions
->MapBufferRange
= radeonMapBufferRange
;
237 functions
->UnmapBuffer
= radeonUnmapBuffer
;