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 "radeon_buffer_objects.h"
30 #include "main/imports.h"
31 #include "main/mtypes.h"
32 #include "main/bufferobj.h"
34 #include "radeon_common.h"
36 struct radeon_buffer_object
*
37 get_radeon_buffer_object(struct gl_buffer_object
*obj
)
39 return (struct radeon_buffer_object
*) obj
;
42 static struct gl_buffer_object
*
43 radeonNewBufferObject(struct gl_context
* ctx
,
47 struct radeon_buffer_object
*obj
= CALLOC_STRUCT(radeon_buffer_object
);
49 _mesa_initialize_buffer_object(&obj
->Base
, name
, target
);
57 * Called via glDeleteBuffersARB().
60 radeonDeleteBufferObject(struct gl_context
* ctx
,
61 struct gl_buffer_object
*obj
)
63 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
66 radeon_bo_unmap(radeon_obj
->bo
);
70 radeon_bo_unref(radeon_obj
->bo
);
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.
81 * Called via ctx->Driver.BufferData().
82 * \return GL_TRUE for success, GL_FALSE if out of memory
85 radeonBufferData(struct gl_context
* ctx
,
90 struct gl_buffer_object
*obj
)
92 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
93 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
95 radeon_obj
->Base
.Size
= size
;
96 radeon_obj
->Base
.Usage
= usage
;
98 if (radeon_obj
->bo
!= NULL
) {
99 radeon_bo_unref(radeon_obj
->bo
);
100 radeon_obj
->bo
= NULL
;
104 radeon_obj
->bo
= radeon_bo_open(radeon
->radeonScreen
->bom
,
108 RADEON_GEM_DOMAIN_GTT
,
115 radeon_bo_map(radeon_obj
->bo
, GL_TRUE
);
117 memcpy(radeon_obj
->bo
->ptr
, data
, size
);
119 radeon_bo_unmap(radeon_obj
->bo
);
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().
132 radeonBufferSubData(struct gl_context
* ctx
,
136 struct gl_buffer_object
*obj
)
138 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
139 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
141 if (radeon_bo_is_referenced_by_cs(radeon_obj
->bo
, radeon
->cmdbuf
.cs
)) {
142 radeon_firevertices(radeon
);
145 radeon_bo_map(radeon_obj
->bo
, GL_TRUE
);
147 memcpy(radeon_obj
->bo
->ptr
+ offset
, data
, size
);
149 radeon_bo_unmap(radeon_obj
->bo
);
153 * Called via glGetBufferSubDataARB()
156 radeonGetBufferSubData(struct gl_context
* ctx
,
160 struct gl_buffer_object
*obj
)
162 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
164 radeon_bo_map(radeon_obj
->bo
, GL_FALSE
);
166 memcpy(data
, radeon_obj
->bo
->ptr
+ offset
, size
);
168 radeon_bo_unmap(radeon_obj
->bo
);
172 * Called via glMapBuffer() and glMapBufferRange()
175 radeonMapBufferRange(struct gl_context
* ctx
,
176 GLintptr offset
, GLsizeiptr length
,
177 GLbitfield access
, struct gl_buffer_object
*obj
)
179 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
180 const GLboolean write_only
=
181 (access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == GL_MAP_WRITE_BIT
;
184 ctx
->Driver
.Flush(ctx
);
187 if (radeon_obj
->bo
== NULL
) {
192 obj
->Offset
= offset
;
193 obj
->Length
= length
;
194 obj
->AccessFlags
= access
;
196 radeon_bo_map(radeon_obj
->bo
, write_only
);
198 obj
->Pointer
= radeon_obj
->bo
->ptr
+ offset
;
204 * Called via glUnmapBufferARB()
207 radeonUnmapBuffer(struct gl_context
* ctx
,
208 struct gl_buffer_object
*obj
)
210 struct radeon_buffer_object
*radeon_obj
= get_radeon_buffer_object(obj
);
212 if (radeon_obj
->bo
!= NULL
) {
213 radeon_bo_unmap(radeon_obj
->bo
);
224 radeonInitBufferObjectFuncs(struct dd_function_table
*functions
)
226 functions
->NewBufferObject
= radeonNewBufferObject
;
227 functions
->DeleteBuffer
= radeonDeleteBufferObject
;
228 functions
->BufferData
= radeonBufferData
;
229 functions
->BufferSubData
= radeonBufferSubData
;
230 functions
->GetBufferSubData
= radeonGetBufferSubData
;
231 functions
->MapBufferRange
= radeonMapBufferRange
;
232 functions
->UnmapBuffer
= radeonUnmapBuffer
;