2 * Copyright (C) 2009 Francisco Jerez.
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:
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.
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.
27 #include "nouveau_driver.h"
28 #include "nouveau_bufferobj.h"
29 #include "nouveau_context.h"
31 #include "main/bufferobj.h"
34 get_bufferobj_map(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
37 struct nouveau_bufferobj
*nbo
= to_nouveau_bufferobj(obj
);
43 nouveau_bo_map(nbo
->bo
, flags
, context_client(ctx
));
50 static struct gl_buffer_object
*
51 nouveau_bufferobj_new(struct gl_context
*ctx
, GLuint buffer
, GLenum target
)
53 struct nouveau_bufferobj
*nbo
;
55 nbo
= CALLOC_STRUCT(nouveau_bufferobj
);
59 _mesa_initialize_buffer_object(ctx
, &nbo
->base
, buffer
, target
);
65 nouveau_bufferobj_del(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
67 struct nouveau_bufferobj
*nbo
= to_nouveau_bufferobj(obj
);
69 nouveau_bo_ref(NULL
, &nbo
->bo
);
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
)
79 struct nouveau_bufferobj
*nbo
= to_nouveau_bufferobj(obj
);
84 obj
->StorageFlags
= storageFlags
;
86 /* Free previous storage */
87 nouveau_bo_ref(NULL
, &nbo
->bo
);
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
);
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
);
106 memcpy(get_bufferobj_map(ctx
, obj
, NOUVEAU_BO_WR
), data
, size
);
112 nouveau_bufferobj_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
113 GLsizeiptrARB size
, const GLvoid
*data
,
114 struct gl_buffer_object
*obj
)
116 memcpy(get_bufferobj_map(ctx
, obj
, NOUVEAU_BO_WR
) + offset
, data
, size
);
120 nouveau_bufferobj_get_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
121 GLsizeiptrARB size
, GLvoid
*data
,
122 struct gl_buffer_object
*obj
)
124 memcpy(data
, get_bufferobj_map(ctx
, obj
, NOUVEAU_BO_RD
) + offset
, size
);
128 nouveau_bufferobj_map_range(struct gl_context
*ctx
, GLintptr offset
,
129 GLsizeiptr length
, GLbitfield access
,
130 struct gl_buffer_object
*obj
)
135 assert(!obj
->Pointer
);
137 if (!(access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
138 if (access
& GL_MAP_READ_BIT
)
139 flags
|= NOUVEAU_BO_RD
;
140 if (access
& GL_MAP_WRITE_BIT
)
141 flags
|= NOUVEAU_BO_WR
;
144 map
= get_bufferobj_map(ctx
, obj
, flags
);
148 obj
->Pointer
= map
+ offset
;
149 obj
->Offset
= offset
;
150 obj
->Length
= length
;
151 obj
->AccessFlags
= access
;
157 nouveau_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
159 assert(obj
->Pointer
);
164 obj
->AccessFlags
= 0;
170 nouveau_bufferobj_functions_init(struct dd_function_table
*functions
)
172 functions
->NewBufferObject
= nouveau_bufferobj_new
;
173 functions
->DeleteBuffer
= nouveau_bufferobj_del
;
174 functions
->BufferData
= nouveau_bufferobj_data
;
175 functions
->BufferSubData
= nouveau_bufferobj_subdata
;
176 functions
->GetBufferSubData
= nouveau_bufferobj_get_subdata
;
177 functions
->MapBufferRange
= nouveau_bufferobj_map_range
;
178 functions
->UnmapBuffer
= nouveau_bufferobj_unmap
;