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
,
46 struct radeon_buffer_object
*obj
= CALLOC_STRUCT(radeon_buffer_object
);
48 _mesa_initialize_buffer_object(ctx
, &obj
->Base
, name
, target
);
56 * Called via glDeleteBuffersARB().
59 radeonDeleteBufferObject(struct gl_context
* ctx
,
60 struct gl_buffer_object
*obj
)
62 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
65 for (i
= 0; i
< MAP_COUNT
; i
++) {
66 if (obj
->Mappings
[i
].Pointer
) {
67 radeon_bo_unmap(radeon_obj
->bo
);
72 radeon_bo_unref(radeon_obj
->bo
);
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
87 radeonBufferData(struct gl_context
* ctx
,
92 GLbitfield storageFlags
,
93 struct gl_buffer_object
*obj
)
95 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
96 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
98 radeon_obj
->Base
.Size
= size
;
99 radeon_obj
->Base
.Usage
= usage
;
100 radeon_obj
->Base
.StorageFlags
= storageFlags
;
102 if (radeon_obj
->bo
!= NULL
) {
103 radeon_bo_unref(radeon_obj
->bo
);
104 radeon_obj
->bo
= NULL
;
108 radeon_obj
->bo
= radeon_bo_open(radeon
->radeonScreen
->bom
,
111 ctx
->Const
.MinMapBufferAlignment
,
112 RADEON_GEM_DOMAIN_GTT
,
119 radeon_bo_map(radeon_obj
->bo
, GL_TRUE
);
121 memcpy(radeon_obj
->bo
->ptr
, data
, size
);
123 radeon_bo_unmap(radeon_obj
->bo
);
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().
136 radeonBufferSubData(struct gl_context
* ctx
,
140 struct gl_buffer_object
*obj
)
142 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
143 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
145 if (radeon_bo_is_referenced_by_cs(radeon_obj
->bo
, radeon
->cmdbuf
.cs
)) {
146 radeon_firevertices(radeon
);
149 radeon_bo_map(radeon_obj
->bo
, GL_TRUE
);
151 memcpy(radeon_obj
->bo
->ptr
+ offset
, data
, size
);
153 radeon_bo_unmap(radeon_obj
->bo
);
157 * Called via glGetBufferSubDataARB()
160 radeonGetBufferSubData(struct gl_context
* ctx
,
164 struct gl_buffer_object
*obj
)
166 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
168 radeon_bo_map(radeon_obj
->bo
, GL_FALSE
);
170 memcpy(data
, radeon_obj
->bo
->ptr
+ offset
, size
);
172 radeon_bo_unmap(radeon_obj
->bo
);
176 * Called via glMapBuffer() and glMapBufferRange()
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
)
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
;
189 ctx
->Driver
.Flush(ctx
);
192 if (radeon_obj
->bo
== NULL
) {
193 obj
->Mappings
[index
].Pointer
= NULL
;
197 obj
->Mappings
[index
].Offset
= offset
;
198 obj
->Mappings
[index
].Length
= length
;
199 obj
->Mappings
[index
].AccessFlags
= access
;
201 radeon_bo_map(radeon_obj
->bo
, write_only
);
203 obj
->Mappings
[index
].Pointer
= radeon_obj
->bo
->ptr
+ offset
;
204 return obj
->Mappings
[index
].Pointer
;
209 * Called via glUnmapBufferARB()
212 radeonUnmapBuffer(struct gl_context
* ctx
,
213 struct gl_buffer_object
*obj
,
214 gl_map_buffer_index index
)
216 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
218 if (radeon_obj
->bo
!= NULL
) {
219 radeon_bo_unmap(radeon_obj
->bo
);
222 obj
->Mappings
[index
].Pointer
= NULL
;
223 obj
->Mappings
[index
].Offset
= 0;
224 obj
->Mappings
[index
].Length
= 0;
230 radeonInitBufferObjectFuncs(struct dd_function_table
*functions
)
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
;