2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keith@tungstengraphics.com>
29 #include "main/glheader.h"
30 #include "main/bufferobj.h"
31 #include "main/context.h"
32 #include "main/imports.h"
33 #include "main/mtypes.h"
34 #include "main/macros.h"
35 #include "main/light.h"
36 #include "main/state.h"
38 #include "vbo_context.h"
45 * After playback, copy everything but the position from the
46 * last vertex to the saved state
49 _playback_copy_to_current(struct gl_context
*ctx
,
50 const struct vbo_save_vertex_list
*node
)
52 struct vbo_context
*vbo
= vbo_context(ctx
);
53 GLfloat vertex
[VBO_ATTRIB_MAX
* 4];
57 if (node
->current_size
== 0)
60 if (node
->current_data
) {
61 data
= node
->current_data
;
67 offset
= (node
->buffer_offset
+
68 (node
->count
-1) * node
->vertex_size
* sizeof(GLfloat
));
70 offset
= node
->buffer_offset
;
72 ctx
->Driver
.GetBufferSubData( ctx
, 0, offset
,
73 node
->vertex_size
* sizeof(GLfloat
),
74 data
, node
->vertex_store
->bufferobj
);
76 data
+= node
->attrsz
[0]; /* skip vertex position */
79 for (i
= VBO_ATTRIB_POS
+1 ; i
< VBO_ATTRIB_MAX
; i
++) {
80 if (node
->attrsz
[i
]) {
81 GLfloat
*current
= (GLfloat
*)vbo
->currval
[i
].Ptr
;
88 if (memcmp(current
, tmp
, 4 * sizeof(GLfloat
)) != 0) {
89 memcpy(current
, tmp
, 4 * sizeof(GLfloat
));
91 vbo
->currval
[i
].Size
= node
->attrsz
[i
];
93 if (i
>= VBO_ATTRIB_FIRST_MATERIAL
&&
94 i
<= VBO_ATTRIB_LAST_MATERIAL
)
95 ctx
->NewState
|= _NEW_LIGHT
;
97 ctx
->NewState
|= _NEW_CURRENT_ATTRIB
;
100 data
+= node
->attrsz
[i
];
104 /* Colormaterial -- this kindof sucks.
106 if (ctx
->Light
.ColorMaterialEnabled
) {
107 _mesa_update_color_material(ctx
, ctx
->Current
.Attrib
[VBO_ATTRIB_COLOR0
]);
110 /* CurrentExecPrimitive
112 if (node
->prim_count
) {
113 const struct _mesa_prim
*prim
= &node
->prim
[node
->prim_count
- 1];
115 ctx
->Driver
.CurrentExecPrimitive
= PRIM_OUTSIDE_BEGIN_END
;
117 ctx
->Driver
.CurrentExecPrimitive
= prim
->mode
;
124 * Treat the vertex storage as a VBO, define vertex arrays pointing
127 static void vbo_bind_vertex_list(struct gl_context
*ctx
,
128 const struct vbo_save_vertex_list
*node
)
130 struct vbo_context
*vbo
= vbo_context(ctx
);
131 struct vbo_save_context
*save
= &vbo
->save
;
132 struct gl_client_array
*arrays
= save
->arrays
;
133 GLuint buffer_offset
= node
->buffer_offset
;
136 GLubyte node_attrsz
[VBO_ATTRIB_MAX
]; /* copy of node->attrsz[] */
137 GLbitfield varying_inputs
= 0x0;
139 memcpy(node_attrsz
, node
->attrsz
, sizeof(node
->attrsz
));
141 /* Install the default (ie Current) attributes first, then overlay
144 switch (get_program_mode(ctx
)) {
146 for (attr
= 0; attr
< 16; attr
++) {
147 save
->inputs
[attr
] = &vbo
->legacy_currval
[attr
];
149 for (attr
= 0; attr
< MAT_ATTRIB_MAX
; attr
++) {
150 save
->inputs
[attr
+ 16] = &vbo
->mat_currval
[attr
];
152 map
= vbo
->map_vp_none
;
156 /* The aliasing of attributes for NV vertex programs has already
157 * occurred. NV vertex programs cannot access material values,
158 * nor attributes greater than VERT_ATTRIB_TEX7.
160 for (attr
= 0; attr
< 16; attr
++) {
161 save
->inputs
[attr
] = &vbo
->legacy_currval
[attr
];
162 save
->inputs
[attr
+ 16] = &vbo
->generic_currval
[attr
];
164 map
= vbo
->map_vp_arb
;
166 /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
167 * In that case we effectively need to route the data from
168 * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
170 if ((ctx
->VertexProgram
._Current
->Base
.InputsRead
& VERT_BIT_POS
) == 0 &&
171 (ctx
->VertexProgram
._Current
->Base
.InputsRead
& VERT_BIT_GENERIC0
)) {
172 save
->inputs
[16] = save
->inputs
[0];
173 node_attrsz
[16] = node_attrsz
[0];
181 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
182 const GLuint src
= map
[attr
];
184 if (node_attrsz
[src
]) {
185 /* override the default array set above */
186 save
->inputs
[attr
] = &arrays
[attr
];
188 arrays
[attr
].Ptr
= (const GLubyte
*) NULL
+ buffer_offset
;
189 arrays
[attr
].Size
= node
->attrsz
[src
];
190 arrays
[attr
].StrideB
= node
->vertex_size
* sizeof(GLfloat
);
191 arrays
[attr
].Stride
= node
->vertex_size
* sizeof(GLfloat
);
192 arrays
[attr
].Type
= GL_FLOAT
;
193 arrays
[attr
].Format
= GL_RGBA
;
194 arrays
[attr
].Enabled
= 1;
195 _mesa_reference_buffer_object(ctx
,
196 &arrays
[attr
].BufferObj
,
197 node
->vertex_store
->bufferobj
);
198 arrays
[attr
]._MaxElement
= node
->count
; /* ??? */
200 assert(arrays
[attr
].BufferObj
->Name
);
202 buffer_offset
+= node
->attrsz
[src
] * sizeof(GLfloat
);
203 varying_inputs
|= 1<<attr
;
207 _mesa_set_varying_vp_inputs( ctx
, varying_inputs
);
212 vbo_save_loopback_vertex_list(struct gl_context
*ctx
,
213 const struct vbo_save_vertex_list
*list
)
215 const char *buffer
= ctx
->Driver
.MapBuffer(ctx
,
217 GL_READ_ONLY
, /* ? */
218 list
->vertex_store
->bufferobj
);
220 vbo_loopback_vertex_list(ctx
,
221 (const GLfloat
*)(buffer
+ list
->buffer_offset
),
228 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
229 list
->vertex_store
->bufferobj
);
234 * Execute the buffer and save copied verts.
235 * This is called from the display list code when executing
239 vbo_save_playback_vertex_list(struct gl_context
*ctx
, void *data
)
241 const struct vbo_save_vertex_list
*node
=
242 (const struct vbo_save_vertex_list
*) data
;
243 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
245 FLUSH_CURRENT(ctx
, 0);
247 if (node
->prim_count
> 0 && node
->count
> 0) {
249 if (ctx
->Driver
.CurrentExecPrimitive
!= PRIM_OUTSIDE_BEGIN_END
&&
250 node
->prim
[0].begin
) {
252 /* Degenerate case: list is called inside begin/end pair and
253 * includes operations such as glBegin or glDrawArrays.
256 printf("displaylist recursive begin");
258 vbo_save_loopback_vertex_list( ctx
, node
);
261 else if (save
->replay_flags
) {
262 /* Various degnerate cases: translate into immediate mode
263 * calls rather than trying to execute in place.
265 vbo_save_loopback_vertex_list( ctx
, node
);
270 _mesa_update_state( ctx
);
272 /* XXX also need to check if shader enabled, but invalid */
273 if ((ctx
->VertexProgram
.Enabled
&& !ctx
->VertexProgram
._Enabled
) ||
274 (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
)) {
275 _mesa_error(ctx
, GL_INVALID_OPERATION
,
276 "glBegin (invalid vertex/fragment program)");
280 vbo_bind_vertex_list( ctx
, node
);
285 _mesa_update_state( ctx
);
287 vbo_context(ctx
)->draw_prims(ctx
,
293 0, /* Node is a VBO, so this is ok */
299 _playback_copy_to_current( ctx
, node
);
303 #endif /* FEATURE_dlist */