Plug in selection/feedback code.
[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
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_context *st = st_context(ctx);
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 st_obj->buffer = st->pipe->winsys->buffer_create( st->pipe->winsys, 32 );
67
68 return &st_obj->Base;
69 }
70
71
72
73 /**
74 * Deallocate/free a vertex/pixel buffer object.
75 * Called via glDeleteBuffersARB().
76 */
77 static void
78 st_bufferobj_free(GLcontext *ctx, struct gl_buffer_object *obj)
79 {
80 struct pipe_context *pipe = st_context(ctx)->pipe;
81 struct st_buffer_object *st_obj = st_buffer_object(obj);
82
83 if (st_obj->buffer)
84 pipe->winsys->buffer_reference(pipe->winsys, &st_obj->buffer, NULL);
85
86 free(st_obj);
87 }
88
89
90
91 /**
92 * Allocate space for and store data in a buffer object. Any data that was
93 * previously stored in the buffer object is lost. If data is NULL,
94 * memory will be allocated, but no copy will occur.
95 * Called via glBufferDataARB().
96 */
97 static void
98 st_bufferobj_data(GLcontext *ctx,
99 GLenum target,
100 GLsizeiptrARB size,
101 const GLvoid * data,
102 GLenum usage,
103 struct gl_buffer_object *obj)
104 {
105 struct pipe_context *pipe = st_context(ctx)->pipe;
106 struct st_buffer_object *st_obj = st_buffer_object(obj);
107
108 st_obj->Base.Size = size;
109 st_obj->Base.Usage = usage;
110
111 pipe->winsys->buffer_data( pipe->winsys, st_obj->buffer, size, data );
112 }
113
114
115 /**
116 * Replace data in a subrange of buffer object. If the data range
117 * specified by size + offset extends beyond the end of the buffer or
118 * if data is NULL, no copy is performed.
119 * Called via glBufferSubDataARB().
120 */
121 static void
122 st_bufferobj_subdata(GLcontext *ctx,
123 GLenum target,
124 GLintptrARB offset,
125 GLsizeiptrARB size,
126 const GLvoid * data, struct gl_buffer_object *obj)
127 {
128 struct pipe_context *pipe = st_context(ctx)->pipe;
129 struct st_buffer_object *st_obj = st_buffer_object(obj);
130
131 pipe->winsys->buffer_subdata(pipe->winsys, st_obj->buffer, offset, size, data);
132 }
133
134
135 /**
136 * Called via glGetBufferSubDataARB().
137 */
138 static void
139 st_bufferobj_get_subdata(GLcontext *ctx,
140 GLenum target,
141 GLintptrARB offset,
142 GLsizeiptrARB size,
143 GLvoid * data, struct gl_buffer_object *obj)
144 {
145 struct pipe_context *pipe = st_context(ctx)->pipe;
146 struct st_buffer_object *st_obj = st_buffer_object(obj);
147
148 pipe->winsys->buffer_get_subdata(pipe->winsys, st_obj->buffer, offset, size, data);
149 }
150
151
152 /**
153 * Called via glMapBufferARB().
154 */
155 static void *
156 st_bufferobj_map(GLcontext *ctx, GLenum target, GLenum access,
157 struct gl_buffer_object *obj)
158 {
159 struct pipe_context *pipe = st_context(ctx)->pipe;
160 struct st_buffer_object *st_obj = st_buffer_object(obj);
161 GLuint flags;
162
163 switch (access) {
164 case GL_WRITE_ONLY:
165 flags = PIPE_BUFFER_FLAG_WRITE;
166 break;
167 case GL_READ_ONLY:
168 flags = PIPE_BUFFER_FLAG_READ;
169 break;
170 case GL_READ_WRITE:
171 /* fall-through */
172 default:
173 flags = PIPE_BUFFER_FLAG_READ | PIPE_BUFFER_FLAG_WRITE;
174 break;
175 }
176
177 obj->Pointer = pipe->winsys->buffer_map(pipe->winsys, st_obj->buffer, flags);
178 return obj->Pointer;
179 }
180
181
182 /**
183 * Called via glMapBufferARB().
184 */
185 static GLboolean
186 st_bufferobj_unmap(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
187 {
188 struct pipe_context *pipe = st_context(ctx)->pipe;
189 struct st_buffer_object *st_obj = st_buffer_object(obj);
190
191 pipe->winsys->buffer_unmap(pipe->winsys, st_obj->buffer);
192 obj->Pointer = NULL;
193 return GL_TRUE;
194 }
195
196
197 void
198 st_init_bufferobject_functions(struct dd_function_table *functions)
199 {
200 functions->NewBufferObject = st_bufferobj_alloc;
201 functions->DeleteBuffer = st_bufferobj_free;
202 functions->BufferData = st_bufferobj_data;
203 functions->BufferSubData = st_bufferobj_subdata;
204 functions->GetBufferSubData = st_bufferobj_get_subdata;
205 functions->MapBuffer = st_bufferobj_map;
206 functions->UnmapBuffer = st_bufferobj_unmap;
207 }