Merge branch '965-glsl'
[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 bmBufferData(intel, intel_obj->buffer, size, data, 0);
107 }
108
109
110 /**
111 * Replace data in a subrange of buffer object. If the data range
112 * specified by size + offset extends beyond the end of the buffer or
113 * if data is NULL, no copy is performed.
114 * Called via glBufferSubDataARB().
115 */
116 static void intel_bufferobj_subdata( GLcontext *ctx,
117 GLenum target,
118 GLintptrARB offset,
119 GLsizeiptrARB size,
120 const GLvoid * data,
121 struct gl_buffer_object * obj )
122 {
123 struct intel_context *intel = intel_context(ctx);
124 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
125
126 assert(intel_obj);
127 bmBufferSubData(intel, intel_obj->buffer, offset, size, data);
128 }
129
130
131 /**
132 * Called via glGetBufferSubDataARB().
133 */
134 static void intel_bufferobj_get_subdata( GLcontext *ctx,
135 GLenum target,
136 GLintptrARB offset,
137 GLsizeiptrARB size,
138 GLvoid * data,
139 struct gl_buffer_object * obj )
140 {
141 struct intel_context *intel = intel_context(ctx);
142 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
143
144 assert(intel_obj);
145 bmBufferGetSubData(intel, intel_obj->buffer, offset, size, data);
146 }
147
148
149
150 /**
151 * Called via glMapBufferARB().
152 */
153 static void *intel_bufferobj_map( GLcontext *ctx,
154 GLenum target,
155 GLenum access,
156 struct gl_buffer_object *obj )
157 {
158 struct intel_context *intel = intel_context(ctx);
159 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
160
161 /* XXX: Translate access to flags arg below:
162 */
163 assert(intel_obj);
164 assert(intel_obj->buffer);
165 obj->Pointer = bmMapBuffer(intel, intel_obj->buffer, 0);
166 return obj->Pointer;
167 }
168
169
170 /**
171 * Called via glMapBufferARB().
172 */
173 static GLboolean intel_bufferobj_unmap( GLcontext *ctx,
174 GLenum target,
175 struct gl_buffer_object *obj )
176 {
177 struct intel_context *intel = intel_context(ctx);
178 struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
179
180 assert(intel_obj);
181 assert(intel_obj->buffer);
182 assert(obj->Pointer);
183 bmUnmapBuffer(intel, intel_obj->buffer);
184 obj->Pointer = NULL;
185 return GL_TRUE;
186 }
187
188 struct buffer *intel_bufferobj_buffer( const struct intel_buffer_object *intel_obj )
189 {
190 assert(intel_obj->Base.Name);
191 assert(intel_obj->buffer);
192 return intel_obj->buffer;
193 }
194
195 void intel_bufferobj_init( struct intel_context *intel )
196 {
197 GLcontext *ctx = &intel->ctx;
198
199 ctx->Driver.NewBufferObject = intel_bufferobj_alloc;
200 ctx->Driver.DeleteBuffer = intel_bufferobj_free;
201 ctx->Driver.BufferData = intel_bufferobj_data;
202 ctx->Driver.BufferSubData = intel_bufferobj_subdata;
203 ctx->Driver.GetBufferSubData = intel_bufferobj_get_subdata;
204 ctx->Driver.MapBuffer = intel_bufferobj_map;
205 ctx->Driver.UnmapBuffer = intel_bufferobj_unmap;
206 }