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