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"
42 * After playback, copy everything but the position from the
43 * last vertex to the saved state
45 static void _playback_copy_to_current( GLcontext
*ctx
,
46 const struct vbo_save_vertex_list
*node
)
48 struct vbo_context
*vbo
= vbo_context(ctx
);
49 GLfloat vertex
[VBO_ATTRIB_MAX
* 4], *data
= vertex
;
53 offset
= (node
->buffer_offset
+
54 (node
->count
-1) * node
->vertex_size
* sizeof(GLfloat
));
56 offset
= node
->buffer_offset
;
58 ctx
->Driver
.GetBufferSubData( ctx
, 0, offset
,
59 node
->vertex_size
* sizeof(GLfloat
),
60 data
, node
->vertex_store
->bufferobj
);
62 data
+= node
->attrsz
[0]; /* skip vertex position */
64 for (i
= VBO_ATTRIB_POS
+1 ; i
< VBO_ATTRIB_MAX
; i
++) {
65 if (node
->attrsz
[i
]) {
66 GLfloat
*current
= (GLfloat
*)vbo
->currval
[i
].Ptr
;
68 COPY_CLEAN_4V(current
,
72 vbo
->currval
[i
].Size
= node
->attrsz
[i
];
74 data
+= node
->attrsz
[i
];
76 if (i
>= VBO_ATTRIB_FIRST_MATERIAL
&&
77 i
<= VBO_ATTRIB_LAST_MATERIAL
)
78 ctx
->NewState
|= _NEW_LIGHT
;
82 /* Colormaterial -- this kindof sucks.
84 if (ctx
->Light
.ColorMaterialEnabled
) {
85 _mesa_update_color_material(ctx
, ctx
->Current
.Attrib
[VBO_ATTRIB_COLOR0
]);
88 /* CurrentExecPrimitive
90 if (node
->prim_count
) {
91 const struct _mesa_prim
*prim
= &node
->prim
[node
->prim_count
- 1];
93 ctx
->Driver
.CurrentExecPrimitive
= PRIM_OUTSIDE_BEGIN_END
;
95 ctx
->Driver
.CurrentExecPrimitive
= prim
->mode
;
101 /* Treat the vertex storage as a VBO, define vertex arrays pointing
104 static void vbo_bind_vertex_list( GLcontext
*ctx
,
105 const struct vbo_save_vertex_list
*node
)
107 struct vbo_context
*vbo
= vbo_context(ctx
);
108 struct vbo_save_context
*save
= &vbo
->save
;
109 struct gl_client_array
*arrays
= save
->arrays
;
110 GLuint data
= node
->buffer_offset
;
114 /* Install the default (ie Current) attributes first, then overlay
117 switch (get_program_mode(ctx
)) {
119 for (attr
= 0; attr
< 16; attr
++) {
120 save
->inputs
[attr
] = &vbo
->legacy_currval
[attr
];
122 for (attr
= 0; attr
< MAT_ATTRIB_MAX
; attr
++) {
123 save
->inputs
[attr
+ 16] = &vbo
->mat_currval
[attr
];
125 map
= vbo
->map_vp_none
;
129 /* The aliasing of attributes for NV vertex programs has already
130 * occurred. NV vertex programs cannot access material values,
131 * nor attributes greater than VERT_ATTRIB_TEX7.
133 for (attr
= 0; attr
< 16; attr
++) {
134 save
->inputs
[attr
] = &vbo
->legacy_currval
[attr
];
135 save
->inputs
[attr
+ 16] = &vbo
->generic_currval
[attr
];
137 map
= vbo
->map_vp_arb
;
141 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
142 GLuint src
= map
[attr
];
144 if (node
->attrsz
[src
]) {
145 /* override the default array set above */
146 save
->inputs
[attr
] = &arrays
[attr
];
148 arrays
[attr
].Ptr
= (const GLubyte
*) data
;
149 arrays
[attr
].Size
= node
->attrsz
[src
];
150 arrays
[attr
].StrideB
= node
->vertex_size
* sizeof(GLfloat
);
151 arrays
[attr
].Stride
= node
->vertex_size
* sizeof(GLfloat
);
152 arrays
[attr
].Type
= GL_FLOAT
;
153 arrays
[attr
].Format
= GL_RGBA
;
154 arrays
[attr
].Enabled
= 1;
155 _mesa_reference_buffer_object(ctx
,
156 &arrays
[attr
].BufferObj
,
157 node
->vertex_store
->bufferobj
);
158 arrays
[attr
]._MaxElement
= node
->count
; /* ??? */
160 assert(arrays
[attr
].BufferObj
->Name
);
162 data
+= node
->attrsz
[src
] * sizeof(GLfloat
);
167 static void vbo_save_loopback_vertex_list( GLcontext
*ctx
,
168 const struct vbo_save_vertex_list
*list
)
170 const char *buffer
= ctx
->Driver
.MapBuffer(ctx
,
172 GL_READ_ONLY
, /* ? */
173 list
->vertex_store
->bufferobj
);
175 vbo_loopback_vertex_list( ctx
,
176 (const GLfloat
*)(buffer
+ list
->buffer_offset
),
183 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
184 list
->vertex_store
->bufferobj
);
189 * Execute the buffer and save copied verts.
191 void vbo_save_playback_vertex_list( GLcontext
*ctx
, void *data
)
193 const struct vbo_save_vertex_list
*node
= (const struct vbo_save_vertex_list
*) data
;
194 struct vbo_save_context
*save
= &vbo_context(ctx
)->save
;
196 FLUSH_CURRENT(ctx
, 0);
198 if (node
->prim_count
> 0 && node
->count
> 0) {
200 if (ctx
->Driver
.CurrentExecPrimitive
!= PRIM_OUTSIDE_BEGIN_END
&&
201 node
->prim
[0].begin
) {
203 /* Degenerate case: list is called inside begin/end pair and
204 * includes operations such as glBegin or glDrawArrays.
207 _mesa_printf("displaylist recursive begin");
209 vbo_save_loopback_vertex_list( ctx
, node
);
212 else if (save
->replay_flags
) {
213 /* Various degnerate cases: translate into immediate mode
214 * calls rather than trying to execute in place.
216 vbo_save_loopback_vertex_list( ctx
, node
);
221 _mesa_update_state( ctx
);
223 /* XXX also need to check if shader enabled, but invalid */
224 if ((ctx
->VertexProgram
.Enabled
&& !ctx
->VertexProgram
._Enabled
) ||
225 (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
)) {
226 _mesa_error(ctx
, GL_INVALID_OPERATION
,
227 "glBegin (invalid vertex/fragment program)");
231 vbo_bind_vertex_list( ctx
, node
);
233 vbo_context(ctx
)->draw_prims( ctx
,
238 0, /* Node is a VBO, so this is ok */
244 _playback_copy_to_current( ctx
, node
);