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