gallium: free bitmap fragment shaders, misc clean-up
[mesa.git] / src / mesa / state_tracker / st_cb_bufferobjects.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 "main/imports.h"
30 #include "main/mtypes.h"
31 #include "main/bufferobj.h"
32
33 #include "st_context.h"
34 #include "st_cb_bufferobjects.h"
35
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
38 #include "pipe/p_winsys.h"
39 #include "pipe/p_inlines.h"
40
41
42
43 /* Pixel buffers and Vertex/index buffers are handled through these
44 * mesa callbacks. Framebuffer/Renderbuffer objects are
45 * created/managed elsewhere.
46 */
47
48
49
50 /**
51 * There is some duplication between mesa's bufferobjects and our
52 * bufmgr buffers. Both have an integer handle and a hashtable to
53 * lookup an opaque structure. It would be nice if the handles and
54 * internal structure where somehow shared.
55 */
56 static struct gl_buffer_object *
57 st_bufferobj_alloc(GLcontext *ctx, GLuint name, GLenum target)
58 {
59 struct st_buffer_object *st_obj = CALLOC_STRUCT(st_buffer_object);
60
61 if (!st_obj)
62 return NULL;
63
64 _mesa_initialize_buffer_object(&st_obj->Base, name, target);
65
66 return &st_obj->Base;
67 }
68
69
70
71 /**
72 * Deallocate/free a vertex/pixel buffer object.
73 * Called via glDeleteBuffersARB().
74 */
75 static void
76 st_bufferobj_free(GLcontext *ctx, struct gl_buffer_object *obj)
77 {
78 struct pipe_context *pipe = st_context(ctx)->pipe;
79 struct st_buffer_object *st_obj = st_buffer_object(obj);
80
81 if (st_obj->buffer)
82 pipe_buffer_reference(pipe->winsys, &st_obj->buffer, NULL);
83
84 free(st_obj);
85 }
86
87
88
89 /**
90 * Replace data in a subrange of buffer object. If the data range
91 * specified by size + offset extends beyond the end of the buffer or
92 * if data is NULL, no copy is performed.
93 * Called via glBufferSubDataARB().
94 */
95 static void
96 st_bufferobj_subdata(GLcontext *ctx,
97 GLenum target,
98 GLintptrARB offset,
99 GLsizeiptrARB size,
100 const GLvoid * data, struct gl_buffer_object *obj)
101 {
102 struct pipe_context *pipe = st_context(ctx)->pipe;
103 struct st_buffer_object *st_obj = st_buffer_object(obj);
104 char *map;
105
106 if (offset >= st_obj->size || size > (st_obj->size - offset))
107 return;
108
109 map = pipe->winsys->buffer_map(pipe->winsys, st_obj->buffer,
110 PIPE_BUFFER_USAGE_CPU_WRITE);
111 memcpy(map + offset, data, size);
112 pipe->winsys->buffer_unmap(pipe->winsys, st_obj->buffer);
113 }
114
115
116 /**
117 * Called via glGetBufferSubDataARB().
118 */
119 static void
120 st_bufferobj_get_subdata(GLcontext *ctx,
121 GLenum target,
122 GLintptrARB offset,
123 GLsizeiptrARB size,
124 GLvoid * data, struct gl_buffer_object *obj)
125 {
126 struct pipe_context *pipe = st_context(ctx)->pipe;
127 struct st_buffer_object *st_obj = st_buffer_object(obj);
128 char *map;
129
130 if (offset >= st_obj->size || size > (st_obj->size - offset))
131 return;
132
133 map = pipe->winsys->buffer_map(pipe->winsys, st_obj->buffer,
134 PIPE_BUFFER_USAGE_CPU_READ);
135 memcpy(data, map + offset, size);
136 pipe->winsys->buffer_unmap(pipe->winsys, st_obj->buffer);
137 }
138
139
140 /**
141 * Allocate space for and store data in a buffer object. Any data that was
142 * previously stored in the buffer object is lost. If data is NULL,
143 * memory will be allocated, but no copy will occur.
144 * Called via glBufferDataARB().
145 */
146 static void
147 st_bufferobj_data(GLcontext *ctx,
148 GLenum target,
149 GLsizeiptrARB size,
150 const GLvoid * data,
151 GLenum usage,
152 struct gl_buffer_object *obj)
153 {
154 struct st_context *st = st_context(ctx);
155 struct pipe_context *pipe = st->pipe;
156 struct st_buffer_object *st_obj = st_buffer_object(obj);
157 unsigned buffer_usage;
158
159 st_obj->Base.Size = size;
160 st_obj->Base.Usage = usage;
161
162 switch(target) {
163 case GL_PIXEL_PACK_BUFFER_ARB:
164 case GL_PIXEL_UNPACK_BUFFER_ARB:
165 buffer_usage = PIPE_BUFFER_USAGE_PIXEL;
166 break;
167 case GL_ARRAY_BUFFER_ARB:
168 buffer_usage = PIPE_BUFFER_USAGE_VERTEX;
169 break;
170 case GL_ELEMENT_ARRAY_BUFFER_ARB:
171 buffer_usage = PIPE_BUFFER_USAGE_INDEX;
172 break;
173 default:
174 buffer_usage = 0;
175 }
176
177 pipe_buffer_reference( pipe->winsys, &st_obj->buffer, NULL );
178
179 st_obj->buffer = pipe->winsys->buffer_create( pipe->winsys, 32, buffer_usage,
180 size );
181
182 st_obj->size = size;
183
184 if (data)
185 st_bufferobj_subdata(ctx, target, 0, size, data, obj);
186 }
187
188
189 /**
190 * Called via glMapBufferARB().
191 */
192 static void *
193 st_bufferobj_map(GLcontext *ctx, GLenum target, GLenum access,
194 struct gl_buffer_object *obj)
195 {
196 struct pipe_context *pipe = st_context(ctx)->pipe;
197 struct st_buffer_object *st_obj = st_buffer_object(obj);
198 GLuint flags;
199
200 switch (access) {
201 case GL_WRITE_ONLY:
202 flags = PIPE_BUFFER_USAGE_CPU_WRITE;
203 break;
204 case GL_READ_ONLY:
205 flags = PIPE_BUFFER_USAGE_CPU_READ;
206 break;
207 case GL_READ_WRITE:
208 /* fall-through */
209 default:
210 flags = PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE;
211 break;
212 }
213
214 obj->Pointer = pipe->winsys->buffer_map(pipe->winsys, st_obj->buffer, flags);
215 return obj->Pointer;
216 }
217
218
219 /**
220 * Called via glMapBufferARB().
221 */
222 static GLboolean
223 st_bufferobj_unmap(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
224 {
225 struct pipe_context *pipe = st_context(ctx)->pipe;
226 struct st_buffer_object *st_obj = st_buffer_object(obj);
227
228 pipe->winsys->buffer_unmap(pipe->winsys, st_obj->buffer);
229 obj->Pointer = NULL;
230 return GL_TRUE;
231 }
232
233
234 void
235 st_init_bufferobject_functions(struct dd_function_table *functions)
236 {
237 functions->NewBufferObject = st_bufferobj_alloc;
238 functions->DeleteBuffer = st_bufferobj_free;
239 functions->BufferData = st_bufferobj_data;
240 functions->BufferSubData = st_bufferobj_subdata;
241 functions->GetBufferSubData = st_bufferobj_get_subdata;
242 functions->MapBuffer = st_bufferobj_map;
243 functions->UnmapBuffer = st_bufferobj_unmap;
244 }