mesa: allow buffers to be mapped multiple times
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_bufferobj.c
1 /*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "nouveau_driver.h"
28 #include "nouveau_bufferobj.h"
29 #include "nouveau_context.h"
30
31 #include "main/bufferobj.h"
32
33 static inline char *
34 get_bufferobj_map(struct gl_context *ctx, struct gl_buffer_object *obj,
35 unsigned flags)
36 {
37 struct nouveau_bufferobj *nbo = to_nouveau_bufferobj(obj);
38 void *map = NULL;
39
40 if (nbo->sys) {
41 map = nbo->sys;
42 } else if (nbo->bo) {
43 nouveau_bo_map(nbo->bo, flags, context_client(ctx));
44 map = nbo->bo->map;
45 }
46
47 return map;
48 }
49
50 static struct gl_buffer_object *
51 nouveau_bufferobj_new(struct gl_context *ctx, GLuint buffer, GLenum target)
52 {
53 struct nouveau_bufferobj *nbo;
54
55 nbo = CALLOC_STRUCT(nouveau_bufferobj);
56 if (!nbo)
57 return NULL;
58
59 _mesa_initialize_buffer_object(ctx, &nbo->base, buffer, target);
60
61 return &nbo->base;
62 }
63
64 static void
65 nouveau_bufferobj_del(struct gl_context *ctx, struct gl_buffer_object *obj)
66 {
67 struct nouveau_bufferobj *nbo = to_nouveau_bufferobj(obj);
68
69 nouveau_bo_ref(NULL, &nbo->bo);
70 free(nbo->sys);
71 free(nbo);
72 }
73
74 static GLboolean
75 nouveau_bufferobj_data(struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
76 const GLvoid *data, GLenum usage, GLbitfield storageFlags,
77 struct gl_buffer_object *obj)
78 {
79 struct nouveau_bufferobj *nbo = to_nouveau_bufferobj(obj);
80 int ret;
81
82 obj->Size = size;
83 obj->Usage = usage;
84 obj->StorageFlags = storageFlags;
85
86 /* Free previous storage */
87 nouveau_bo_ref(NULL, &nbo->bo);
88 free(nbo->sys);
89
90 if (target == GL_ELEMENT_ARRAY_BUFFER_ARB ||
91 (size < 512 && usage == GL_DYNAMIC_DRAW_ARB) ||
92 context_chipset(ctx) < 0x10) {
93 /* Heuristic: keep it in system ram */
94 nbo->sys = malloc(size);
95
96 } else {
97 /* Get a hardware BO */
98 ret = nouveau_bo_new(context_dev(ctx),
99 NOUVEAU_BO_GART | NOUVEAU_BO_MAP,
100 ctx->Const.MinMapBufferAlignment,
101 size, NULL, &nbo->bo);
102 assert(!ret);
103 }
104
105 if (data)
106 memcpy(get_bufferobj_map(ctx, obj, NOUVEAU_BO_WR), data, size);
107
108 return GL_TRUE;
109 }
110
111 static void
112 nouveau_bufferobj_subdata(struct gl_context *ctx, GLintptrARB offset,
113 GLsizeiptrARB size, const GLvoid *data,
114 struct gl_buffer_object *obj)
115 {
116 memcpy(get_bufferobj_map(ctx, obj, NOUVEAU_BO_WR) + offset, data, size);
117 }
118
119 static void
120 nouveau_bufferobj_get_subdata(struct gl_context *ctx, GLintptrARB offset,
121 GLsizeiptrARB size, GLvoid *data,
122 struct gl_buffer_object *obj)
123 {
124 memcpy(data, get_bufferobj_map(ctx, obj, NOUVEAU_BO_RD) + offset, size);
125 }
126
127 static void *
128 nouveau_bufferobj_map_range(struct gl_context *ctx, GLintptr offset,
129 GLsizeiptr length, GLbitfield access,
130 struct gl_buffer_object *obj,
131 gl_map_buffer_index index)
132 {
133 unsigned flags = 0;
134 char *map;
135
136 assert(!obj->Mappings[index].Pointer);
137
138 if (!(access & GL_MAP_UNSYNCHRONIZED_BIT)) {
139 if (access & GL_MAP_READ_BIT)
140 flags |= NOUVEAU_BO_RD;
141 if (access & GL_MAP_WRITE_BIT)
142 flags |= NOUVEAU_BO_WR;
143 }
144
145 map = get_bufferobj_map(ctx, obj, flags);
146 if (!map)
147 return NULL;
148
149 obj->Mappings[index].Pointer = map + offset;
150 obj->Mappings[index].Offset = offset;
151 obj->Mappings[index].Length = length;
152 obj->Mappings[index].AccessFlags = access;
153
154 return obj->Mappings[index].Pointer;
155 }
156
157 static GLboolean
158 nouveau_bufferobj_unmap(struct gl_context *ctx, struct gl_buffer_object *obj,
159 gl_map_buffer_index index)
160 {
161 assert(obj->Mappings[index].Pointer);
162
163 obj->Mappings[index].Pointer = NULL;
164 obj->Mappings[index].Offset = 0;
165 obj->Mappings[index].Length = 0;
166 obj->Mappings[index].AccessFlags = 0;
167
168 return GL_TRUE;
169 }
170
171 void
172 nouveau_bufferobj_functions_init(struct dd_function_table *functions)
173 {
174 functions->NewBufferObject = nouveau_bufferobj_new;
175 functions->DeleteBuffer = nouveau_bufferobj_del;
176 functions->BufferData = nouveau_bufferobj_data;
177 functions->BufferSubData = nouveau_bufferobj_subdata;
178 functions->GetBufferSubData = nouveau_bufferobj_get_subdata;
179 functions->MapBufferRange = nouveau_bufferobj_map_range;
180 functions->UnmapBuffer = nouveau_bufferobj_unmap;
181 }