Merge branch 'nouveau-import'
[mesa.git] / src / mesa / drivers / dri / i965 / intel_buffer_objects.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include "imports.h"
30 #include "mtypes.h"
31 #include "bufferobj.h"
32
33 #include "intel_context.h"
34 #include "intel_buffer_objects.h"
35 #include "bufmgr.h"
36
37
38 /**
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.
43 */
44 static struct gl_buffer_object *intel_bufferobj_alloc( GLcontext *ctx,
45 GLuint name,
46 GLenum target )
47 {
48 struct intel_context *intel = intel_context(ctx);
49 struct intel_buffer_object *obj = MALLOC_STRUCT(intel_buffer_object);
50
51 _mesa_initialize_buffer_object(&obj->Base, name, target);
52
53 /* XXX: We generate our own handle, which is different to 'name' above.
54 */
55 bmGenBuffers(intel, "bufferobj", 1, &obj->buffer, 6);
56 assert(obj->buffer);
57
58 return &obj->Base;
59 }
60
61
62 /**
63 * Deallocate/free a vertex/pixel buffer object.
64 * Called via glDeleteBuffersARB().
65 */
66 static void intel_bufferobj_free( GLcontext *ctx,
67 struct gl_buffer_object *obj )
68 {
69 struct intel_context *intel = intel_context(ctx);
70 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
71
72 assert(intel_obj);
73
74 if (intel_obj->buffer)
75 bmDeleteBuffers( intel, 1, &intel_obj->buffer );
76
77 _mesa_free(intel_obj);
78 }
79
80
81
82 /**
83 * Allocate space for and store data in a buffer object. Any data that was
84 * previously stored in the buffer object is lost. If data is NULL,
85 * memory will be allocated, but no copy will occur.
86 * Called via glBufferDataARB().
87 */
88 static void intel_bufferobj_data( GLcontext *ctx,
89 GLenum target,
90 GLsizeiptrARB size,
91 const GLvoid *data,
92 GLenum usage,
93 struct gl_buffer_object *obj )
94 {
95 struct intel_context *intel = intel_context(ctx);
96 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
97
98 /* XXX: do something useful with 'usage' (eg. populate flags
99 * argument below)
100 */
101 assert(intel_obj);
102
103 obj->Size = size;
104 obj->Usage = usage;
105
106 bmBufferDataAUB(intel, intel_obj->buffer, size, data, 0,
107 0, 0);
108 }
109
110
111 /**
112 * Replace data in a subrange of buffer object. If the data range
113 * specified by size + offset extends beyond the end of the buffer or
114 * if data is NULL, no copy is performed.
115 * Called via glBufferSubDataARB().
116 */
117 static void intel_bufferobj_subdata( GLcontext *ctx,
118 GLenum target,
119 GLintptrARB offset,
120 GLsizeiptrARB size,
121 const GLvoid * data,
122 struct gl_buffer_object * obj )
123 {
124 struct intel_context *intel = intel_context(ctx);
125 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
126
127 assert(intel_obj);
128 bmBufferSubDataAUB(intel, intel_obj->buffer, offset, size, data, 0, 0);
129 }
130
131
132 /**
133 * Called via glGetBufferSubDataARB().
134 */
135 static void intel_bufferobj_get_subdata( GLcontext *ctx,
136 GLenum target,
137 GLintptrARB offset,
138 GLsizeiptrARB size,
139 GLvoid * data,
140 struct gl_buffer_object * obj )
141 {
142 struct intel_context *intel = intel_context(ctx);
143 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
144
145 assert(intel_obj);
146 bmBufferGetSubData(intel, intel_obj->buffer, offset, size, data);
147 }
148
149
150
151 /**
152 * Called via glMapBufferARB().
153 */
154 static void *intel_bufferobj_map( GLcontext *ctx,
155 GLenum target,
156 GLenum access,
157 struct gl_buffer_object *obj )
158 {
159 struct intel_context *intel = intel_context(ctx);
160 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
161
162 /* XXX: Translate access to flags arg below:
163 */
164 assert(intel_obj);
165 assert(intel_obj->buffer);
166 obj->Pointer = bmMapBuffer(intel, intel_obj->buffer, 0);
167 return obj->Pointer;
168 }
169
170
171 /**
172 * Called via glMapBufferARB().
173 */
174 static GLboolean intel_bufferobj_unmap( GLcontext *ctx,
175 GLenum target,
176 struct gl_buffer_object *obj )
177 {
178 struct intel_context *intel = intel_context(ctx);
179 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
180
181 assert(intel_obj);
182 assert(intel_obj->buffer);
183 assert(obj->Pointer);
184 bmUnmapBufferAUB(intel, intel_obj->buffer, 0, 0);
185 obj->Pointer = NULL;
186 return GL_TRUE;
187 }
188
189 struct buffer *intel_bufferobj_buffer( const struct intel_buffer_object *intel_obj )
190 {
191 assert(intel_obj->Base.Name);
192 assert(intel_obj->buffer);
193 return intel_obj->buffer;
194 }
195
196 void intel_bufferobj_init( struct intel_context *intel )
197 {
198 GLcontext *ctx = &intel->ctx;
199
200 ctx->Driver.NewBufferObject = intel_bufferobj_alloc;
201 ctx->Driver.DeleteBuffer = intel_bufferobj_free;
202 ctx->Driver.BufferData = intel_bufferobj_data;
203 ctx->Driver.BufferSubData = intel_bufferobj_subdata;
204 ctx->Driver.GetBufferSubData = intel_bufferobj_get_subdata;
205 ctx->Driver.MapBuffer = intel_bufferobj_map;
206 ctx->Driver.UnmapBuffer = intel_bufferobj_unmap;
207 }