1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 #include "bufferobj.h"
33 #include "intel_context.h"
34 #include "intel_buffer_objects.h"
35 #include "intel_regions.h"
36 #include "dri_bufmgr.h"
39 * There is some duplication between mesa's bufferobjects and our
40 * bufmgr buffers. Both have an integer handle and a hashtable to
41 * lookup an opaque structure. It would be nice if the handles and
42 * internal structure where somehow shared.
44 static struct gl_buffer_object
*
45 intel_bufferobj_alloc(GLcontext
* ctx
, GLuint name
, GLenum target
)
47 struct intel_context
*intel
= intel_context(ctx
);
48 struct intel_buffer_object
*obj
= CALLOC_STRUCT(intel_buffer_object
);
50 _mesa_initialize_buffer_object(&obj
->Base
, name
, target
);
52 driGenBuffers(intel
->intelScreen
->regionPool
,
53 "bufferobj", 1, &obj
->buffer
, 64,
54 DRM_BO_FLAG_MEM_LOCAL
|
55 DRM_BO_FLAG_READ
| DRM_BO_FLAG_WRITE
,
62 /* Break the COW tie to the region. The region gets to keep the data.
65 intel_bufferobj_release_region(struct intel_context
*intel
,
66 struct intel_buffer_object
*intel_obj
)
68 assert(intel_obj
->region
->buffer
== intel_obj
->buffer
);
69 intel_obj
->region
->pbo
= NULL
;
70 intel_obj
->region
= NULL
;
71 driBOUnReference(intel_obj
->buffer
);
72 intel_obj
->buffer
= NULL
;
74 /* This leads to a large number of buffer deletion/creation events.
75 * Currently the drm doesn't like that:
77 driGenBuffers(intel
->intelScreen
->regionPool
,
78 "buffer object", 1, &intel_obj
->buffer
, 64, 0, 0);
80 driBOData(intel_obj
->buffer
, intel_obj
->Base
.Size
, NULL
, 0);
81 UNLOCK_HARDWARE(intel
);
84 /* Break the COW tie to the region. Both the pbo and the region end
85 * up with a copy of the data.
88 intel_bufferobj_cow(struct intel_context
*intel
,
89 struct intel_buffer_object
*intel_obj
)
91 assert(intel_obj
->region
);
92 intel_region_cow(intel
->intelScreen
, intel_obj
->region
);
97 * Deallocate/free a vertex/pixel buffer object.
98 * Called via glDeleteBuffersARB().
101 intel_bufferobj_free(GLcontext
* ctx
, struct gl_buffer_object
*obj
)
103 struct intel_context
*intel
= intel_context(ctx
);
104 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
108 if (intel_obj
->region
) {
109 intel_bufferobj_release_region(intel
, intel_obj
);
111 else if (intel_obj
->buffer
) {
112 driDeleteBuffers(1, &intel_obj
->buffer
);
115 _mesa_free(intel_obj
);
121 * Allocate space for and store data in a buffer object. Any data that was
122 * previously stored in the buffer object is lost. If data is NULL,
123 * memory will be allocated, but no copy will occur.
124 * Called via glBufferDataARB().
127 intel_bufferobj_data(GLcontext
* ctx
,
131 GLenum usage
, struct gl_buffer_object
*obj
)
133 struct intel_context
*intel
= intel_context(ctx
);
134 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
136 intel_obj
->Base
.Size
= size
;
137 intel_obj
->Base
.Usage
= usage
;
139 if (intel_obj
->region
)
140 intel_bufferobj_release_region(intel
, intel_obj
);
142 LOCK_HARDWARE(intel
);
143 driBOData(intel_obj
->buffer
, size
, data
, 0);
144 UNLOCK_HARDWARE(intel
);
149 * Replace data in a subrange of buffer object. If the data range
150 * specified by size + offset extends beyond the end of the buffer or
151 * if data is NULL, no copy is performed.
152 * Called via glBufferSubDataARB().
155 intel_bufferobj_subdata(GLcontext
* ctx
,
159 const GLvoid
* data
, struct gl_buffer_object
*obj
)
161 struct intel_context
*intel
= intel_context(ctx
);
162 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
166 if (intel_obj
->region
)
167 intel_bufferobj_cow(intel
, intel_obj
);
169 driBOSubData(intel_obj
->buffer
, offset
, size
, data
);
174 * Called via glGetBufferSubDataARB().
177 intel_bufferobj_get_subdata(GLcontext
* ctx
,
181 GLvoid
* data
, struct gl_buffer_object
*obj
)
183 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
186 driBOGetSubData(intel_obj
->buffer
, offset
, size
, data
);
192 * Called via glMapBufferARB().
195 intel_bufferobj_map(GLcontext
* ctx
,
197 GLenum access
, struct gl_buffer_object
*obj
)
199 struct intel_context
*intel
= intel_context(ctx
);
200 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
202 /* XXX: Translate access to flags arg below:
206 if (intel_obj
->region
)
207 intel_bufferobj_cow(intel
, intel_obj
);
209 obj
->Pointer
= driBOMap(intel_obj
->buffer
,
210 DRM_BO_FLAG_READ
| DRM_BO_FLAG_WRITE
, 0);
216 * Called via glMapBufferARB().
219 intel_bufferobj_unmap(GLcontext
* ctx
,
220 GLenum target
, struct gl_buffer_object
*obj
)
222 struct intel_buffer_object
*intel_obj
= intel_buffer_object(obj
);
225 assert(obj
->Pointer
);
226 driBOUnmap(intel_obj
->buffer
);
231 struct _DriBufferObject
*
232 intel_bufferobj_buffer(struct intel_context
*intel
,
233 struct intel_buffer_object
*intel_obj
, GLuint flag
)
235 if (intel_obj
->region
) {
236 if (flag
== INTEL_WRITE_PART
)
237 intel_bufferobj_cow(intel
, intel_obj
);
238 else if (flag
== INTEL_WRITE_FULL
)
239 intel_bufferobj_release_region(intel
, intel_obj
);
242 return intel_obj
->buffer
;
246 intel_bufferobj_init(struct intel_context
*intel
)
248 GLcontext
*ctx
= &intel
->ctx
;
250 ctx
->Driver
.NewBufferObject
= intel_bufferobj_alloc
;
251 ctx
->Driver
.DeleteBuffer
= intel_bufferobj_free
;
252 ctx
->Driver
.BufferData
= intel_bufferobj_data
;
253 ctx
->Driver
.BufferSubData
= intel_bufferobj_subdata
;
254 ctx
->Driver
.GetBufferSubData
= intel_bufferobj_get_subdata
;
255 ctx
->Driver
.MapBuffer
= intel_bufferobj_map
;
256 ctx
->Driver
.UnmapBuffer
= intel_bufferobj_unmap
;