st_cb_bufferobjects.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  * 
00003  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  * 
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  * 
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  * 
00026  **************************************************************************/
00027 
00028 
00029 #include "main/imports.h"
00030 #include "main/mtypes.h"
00031 #include "main/bufferobj.h"
00032 
00033 #include "st_context.h"
00034 #include "st_cb_bufferobjects.h"
00035 
00036 #include "pipe/p_context.h"
00037 #include "pipe/p_defines.h"
00038 #include "pipe/p_inlines.h"
00039 
00040 
00041 
00042 /* Pixel buffers and Vertex/index buffers are handled through these
00043  * mesa callbacks.  Framebuffer/Renderbuffer objects are
00044  * created/managed elsewhere.
00045  */
00046 
00047 
00048 
00055 static struct gl_buffer_object *
00056 st_bufferobj_alloc(GLcontext *ctx, GLuint name, GLenum target)
00057 {
00058    struct st_buffer_object *st_obj = CALLOC_STRUCT(st_buffer_object);
00059 
00060    if (!st_obj)
00061       return NULL;
00062 
00063    _mesa_initialize_buffer_object(&st_obj->Base, name, target);
00064 
00065    return &st_obj->Base;
00066 }
00067 
00068 
00069 
00074 static void
00075 st_bufferobj_free(GLcontext *ctx, struct gl_buffer_object *obj)
00076 {
00077    struct pipe_context *pipe = st_context(ctx)->pipe;
00078    struct st_buffer_object *st_obj = st_buffer_object(obj);
00079 
00080    if (st_obj->buffer) 
00081       pipe_buffer_reference(pipe->screen, &st_obj->buffer, NULL);
00082 
00083    free(st_obj);
00084 }
00085 
00086 
00087 
00094 static void
00095 st_bufferobj_subdata(GLcontext *ctx,
00096                      GLenum target,
00097                      GLintptrARB offset,
00098                      GLsizeiptrARB size,
00099                      const GLvoid * data, struct gl_buffer_object *obj)
00100 {
00101    struct pipe_context *pipe = st_context(ctx)->pipe;
00102    struct st_buffer_object *st_obj = st_buffer_object(obj);
00103    char *map;
00104 
00105    if (offset >= st_obj->size || size > (st_obj->size - offset))
00106       return;
00107 
00108    map = pipe_buffer_map(pipe->screen, st_obj->buffer, PIPE_BUFFER_USAGE_CPU_WRITE);
00109    memcpy(map + offset, data, size);
00110    pipe_buffer_unmap(pipe->screen, st_obj->buffer);
00111 }
00112 
00113 
00117 static void
00118 st_bufferobj_get_subdata(GLcontext *ctx,
00119                          GLenum target,
00120                          GLintptrARB offset,
00121                          GLsizeiptrARB size,
00122                          GLvoid * data, struct gl_buffer_object *obj)
00123 {
00124    struct pipe_context *pipe = st_context(ctx)->pipe;
00125    struct st_buffer_object *st_obj = st_buffer_object(obj);
00126    char *map;
00127 
00128    if (offset >= st_obj->size || size > (st_obj->size - offset))
00129       return;
00130 
00131    map = pipe_buffer_map(pipe->screen, st_obj->buffer, PIPE_BUFFER_USAGE_CPU_READ);
00132    memcpy(data, map + offset, size);
00133    pipe_buffer_unmap(pipe->screen, st_obj->buffer);
00134 }
00135 
00136 
00143 static void
00144 st_bufferobj_data(GLcontext *ctx,
00145                   GLenum target,
00146                   GLsizeiptrARB size,
00147                   const GLvoid * data,
00148                   GLenum usage, 
00149                   struct gl_buffer_object *obj)
00150 {
00151    struct st_context *st = st_context(ctx);
00152    struct pipe_context *pipe = st->pipe;
00153    struct st_buffer_object *st_obj = st_buffer_object(obj);
00154    unsigned buffer_usage;
00155 
00156    st_obj->Base.Size = size;
00157    st_obj->Base.Usage = usage;
00158    
00159    switch(target) {
00160    case GL_PIXEL_PACK_BUFFER_ARB:
00161    case GL_PIXEL_UNPACK_BUFFER_ARB:
00162       buffer_usage = PIPE_BUFFER_USAGE_PIXEL;
00163       break;
00164    case GL_ARRAY_BUFFER_ARB:
00165       buffer_usage = PIPE_BUFFER_USAGE_VERTEX;
00166       break;
00167    case GL_ELEMENT_ARRAY_BUFFER_ARB:
00168       buffer_usage = PIPE_BUFFER_USAGE_INDEX;
00169       break;
00170    default:
00171       buffer_usage = 0;
00172    }
00173 
00174    pipe_buffer_reference( pipe->screen, &st_obj->buffer, NULL );
00175 
00176    st_obj->buffer = pipe_buffer_create( pipe->screen, 32, buffer_usage, size );
00177 
00178    st_obj->size = size;
00179 
00180    if (data)
00181       st_bufferobj_subdata(ctx, target, 0, size, data, obj);
00182 }
00183 
00184 
00188 static void *
00189 st_bufferobj_map(GLcontext *ctx, GLenum target, GLenum access,
00190                  struct gl_buffer_object *obj)
00191 {
00192    struct pipe_context *pipe = st_context(ctx)->pipe;
00193    struct st_buffer_object *st_obj = st_buffer_object(obj);
00194    GLuint flags;
00195 
00196    switch (access) {
00197    case GL_WRITE_ONLY:
00198       flags = PIPE_BUFFER_USAGE_CPU_WRITE;
00199       break;
00200    case GL_READ_ONLY:
00201       flags = PIPE_BUFFER_USAGE_CPU_READ;
00202       break;
00203    case GL_READ_WRITE:
00204       /* fall-through */
00205    default:
00206       flags = PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE;
00207       break;      
00208    }
00209 
00210    obj->Pointer = pipe_buffer_map(pipe->screen, st_obj->buffer, flags);
00211    return obj->Pointer;
00212 }
00213 
00214 
00218 static GLboolean
00219 st_bufferobj_unmap(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
00220 {
00221    struct pipe_context *pipe = st_context(ctx)->pipe;
00222    struct st_buffer_object *st_obj = st_buffer_object(obj);
00223 
00224    pipe_buffer_unmap(pipe->screen, st_obj->buffer);
00225    obj->Pointer = NULL;
00226    return GL_TRUE;
00227 }
00228 
00229 
00230 void
00231 st_init_bufferobject_functions(struct dd_function_table *functions)
00232 {
00233    functions->NewBufferObject = st_bufferobj_alloc;
00234    functions->DeleteBuffer = st_bufferobj_free;
00235    functions->BufferData = st_bufferobj_data;
00236    functions->BufferSubData = st_bufferobj_subdata;
00237    functions->GetBufferSubData = st_bufferobj_get_subdata;
00238    functions->MapBuffer = st_bufferobj_map;
00239    functions->UnmapBuffer = st_bufferobj_unmap;
00240 }

Generated on Tue Sep 29 06:25:18 2009 for Gallium3D by  doxygen 1.5.4