2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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/context.h"
31 #include "main/imports.h"
32 #include "main/mtypes.h"
33 #include "main/macros.h"
34 #include "main/light.h"
35 #include "main/state.h"
37 #include "vbo_context.h"
41 * After playback, copy everything but the position from the
42 * last vertex to the saved state
44 static void _playback_copy_to_current( GLcontext
*ctx
,
45 const struct vbo_save_vertex_list
*node
)
47 struct vbo_context
*vbo
= vbo_context(ctx
);
48 GLfloat vertex
[VBO_ATTRIB_MAX
* 4], *data
= vertex
;
52 offset
= (node
->buffer_offset
+
53 (node
->count
-1) * node
->vertex_size
* sizeof(GLfloat
));
55 offset
= node
->buffer_offset
;
57 ctx
->Driver
.GetBufferSubData( ctx
, 0, offset
,
58 node
->vertex_size
* sizeof(GLfloat
),
59 data
, node
->vertex_store
->bufferobj
);
61 data
+= node
->attrsz
[0]; /* skip vertex position */
63 for (i
= VBO_ATTRIB_POS
+1 ; i
< VBO_ATTRIB_MAX
; i
++) {
64 if (node
->attrsz
[i
]) {
65 GLfloat
*current
= (GLfloat
*)vbo
->currval
[i
].Ptr
;
67 COPY_CLEAN_4V(current
,
71 vbo
->currval
[i
].Size
= node
->attrsz
[i
];
73 data
+= node
->attrsz
[i
];
75 if (i
>= VBO_ATTRIB_FIRST_MATERIAL
&&
76 i
<= VBO_ATTRIB_LAST_MATERIAL
)
77 ctx
->NewState
|= _NEW_LIGHT
;
81 /* Colormaterial -- this kindof sucks.
83 if (ctx
->Light
.ColorMaterialEnabled
) {
84 _mesa_update_color_material(ctx
, ctx
->Current
.Attrib
[VBO_ATTRIB_COLOR0
]);
87 /* CurrentExecPrimitive
89 if (node
->prim_count
) {
90 const struct _mesa_prim
*prim
= &node
->prim
[node
->prim_count
- 1];
92 ctx
->Driver
.CurrentExecPrimitive
= PRIM_OUTSIDE_BEGIN_END
;
94 ctx
->Driver
.CurrentExecPrimitive
= prim
->mode
;
100 /* Treat the vertex storage as a VBO, define vertex arrays pointing
103 static void vbo_bind_vertex_list( GLcontext
*ctx
,
104 const struct vbo_save_vertex_list
*node
)
106 struct vbo_context
*vbo
= vbo_context(ctx
);
107 struct vbo_save_context
*save
= &vbo
->save
;
108 struct gl_client_array
*arrays
= save
->arrays
;
109 GLuint data
= node
->buffer_offset
;
113 /* Install the default (ie Current) attributes first, then overlay
116 switch (get_program_mode(ctx
)) {
118 memcpy(arrays
, vbo
->legacy_currval
, 16 * sizeof(arrays
[0]));
119 memcpy(arrays
+ 16, vbo
->mat_currval
, MAT_ATTRIB_MAX
* sizeof(arrays
[0]));
120 map
= vbo
->map_vp_none
;
124 /* The aliasing of attributes for NV vertex programs has already
125 * occurred. NV vertex programs cannot access material values,
126 * nor attributes greater than VERT_ATTRIB_TEX7.
128 memcpy(arrays
, vbo
->legacy_currval
, 16 * sizeof(arrays
[0]));
129 memcpy(arrays
+ 16, vbo
->generic_currval
, 16 * sizeof(arrays
[0]));
130 map
= vbo
->map_vp_arb
;
134 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
135 GLuint src
= map
[attr
];
137 if (node
->attrsz
[src
]) {
138 arrays
[attr
].Ptr
= (const GLubyte
*)data
;
139 arrays
[attr
].Size
= node
->attrsz
[src
];
140 arrays
[attr
].StrideB
= node
->vertex_size
* sizeof(GLfloat
);
141 arrays
[attr
].Stride
= node
->vertex_size
* sizeof(GLfloat
);
142 arrays
[attr
].Type
= GL_FLOAT
;
143 arrays
[attr
].Enabled
= 1;
144 arrays
[attr
].BufferObj
= node
->vertex_store
->bufferobj
;
145 arrays
[attr
]._MaxElement
= node
->count
; /* ??? */
147 assert(arrays
[attr
].BufferObj
->Name
);
149 data
+= node
->attrsz
[src
] * sizeof(GLfloat
);
154 static void vbo_save_loopback_vertex_list( GLcontext
*ctx
,
155 const struct vbo_save_vertex_list
*list
)
157 const char *buffer
= ctx
->Driver
.MapBuffer(ctx
,
159 GL_READ_ONLY
, /* ? */
160 list
->vertex_store
->bufferobj
);
162 vbo_loopback_vertex_list( ctx
,
163 (const GLfloat
*)(buffer
+ list
->buffer_offset
),
170 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
171 list
->vertex_store
->bufferobj
);
176 * Execute the buffer and save copied verts.
178 void vbo_save_playback_vertex_list( GLcontext
*ctx
, void *data
)
180 const struct vbo_save_vertex_list
*node
= (const struct vbo_save_vertex_list
*) data
;
181 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
183 FLUSH_CURRENT(ctx
, 0);
185 if (node
->prim_count
> 0 && node
->count
> 0) {
187 if (ctx
->Driver
.CurrentExecPrimitive
!= PRIM_OUTSIDE_BEGIN_END
&&
188 node
->prim
[0].begin
) {
190 /* Degenerate case: list is called inside begin/end pair and
191 * includes operations such as glBegin or glDrawArrays.
194 _mesa_printf("displaylist recursive begin");
196 vbo_save_loopback_vertex_list( ctx
, node
);
199 else if (save
->replay_flags
) {
200 /* Various degnerate cases: translate into immediate mode
201 * calls rather than trying to execute in place.
203 vbo_save_loopback_vertex_list( ctx
, node
);
208 _mesa_update_state( ctx
);
210 /* XXX also need to check if shader enabled, but invalid */
211 if ((ctx
->VertexProgram
.Enabled
&& !ctx
->VertexProgram
._Enabled
) ||
212 (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
)) {
213 _mesa_error(ctx
, GL_INVALID_OPERATION
,
214 "glBegin (invalid vertex/fragment program)");
218 vbo_bind_vertex_list( ctx
, node
);
220 vbo_context(ctx
)->draw_prims( ctx
,
225 0, /* Node is a VBO, so this is ok */
231 _playback_copy_to_current( ctx
, node
);