2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keithw@vmware.com>
30 #include "main/arrayobj.h"
31 #include "main/glheader.h"
32 #include "main/bufferobj.h"
33 #include "main/context.h"
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/macros.h"
37 #include "main/light.h"
38 #include "main/state.h"
39 #include "main/varray.h"
40 #include "util/bitscan.h"
42 #include "vbo_private.h"
46 * After playback, copy everything but the position from the
47 * last vertex to the saved state
50 playback_copy_to_current(struct gl_context
*ctx
,
51 const struct vbo_save_vertex_list
*node
)
53 struct vbo_context
*vbo
= vbo_context(ctx
);
54 fi_type vertex
[VBO_ATTRIB_MAX
* 4];
58 if (node
->current_size
== 0)
61 if (node
->current_data
) {
62 data
= node
->current_data
;
65 /* Position of last vertex */
66 const GLuint pos
= node
->vertex_count
> 0 ? node
->vertex_count
- 1 : 0;
67 /* Offset to last vertex in the vertex buffer */
68 const GLuint offset
= node
->buffer_offset
69 + pos
* node
->vertex_size
* sizeof(GLfloat
);
73 ctx
->Driver
.GetBufferSubData(ctx
, offset
,
74 node
->vertex_size
* sizeof(GLfloat
),
75 data
, node
->vertex_store
->bufferobj
);
77 data
+= node
->attrsz
[0]; /* skip vertex position */
80 mask
= node
->enabled
& (~BITFIELD64_BIT(VBO_ATTRIB_POS
));
82 const int i
= u_bit_scan64(&mask
);
83 fi_type
*current
= (fi_type
*)vbo
->currval
[i
].Ptr
;
85 assert(node
->attrsz
[i
]);
87 COPY_CLEAN_4V_TYPE_AS_UNION(tmp
,
92 if (node
->attrtype
[i
] != vbo
->currval
[i
].Type
||
93 memcmp(current
, tmp
, 4 * sizeof(GLfloat
)) != 0) {
94 memcpy(current
, tmp
, 4 * sizeof(GLfloat
));
96 vbo
->currval
[i
].Size
= node
->attrsz
[i
];
97 vbo
->currval
[i
]._ElementSize
= vbo
->currval
[i
].Size
* sizeof(GLfloat
);
98 vbo
->currval
[i
].Type
= node
->attrtype
[i
];
99 vbo
->currval
[i
].Integer
=
100 vbo_attrtype_to_integer_flag(node
->attrtype
[i
]);
102 if (i
>= VBO_ATTRIB_FIRST_MATERIAL
&&
103 i
<= VBO_ATTRIB_LAST_MATERIAL
)
104 ctx
->NewState
|= _NEW_LIGHT
;
106 ctx
->NewState
|= _NEW_CURRENT_ATTRIB
;
109 data
+= node
->attrsz
[i
];
112 /* Colormaterial -- this kindof sucks.
114 if (ctx
->Light
.ColorMaterialEnabled
) {
115 _mesa_update_color_material(ctx
, ctx
->Current
.Attrib
[VBO_ATTRIB_COLOR0
]);
118 /* CurrentExecPrimitive
120 if (node
->prim_count
) {
121 const struct _mesa_prim
*prim
= &node
->prims
[node
->prim_count
- 1];
123 ctx
->Driver
.CurrentExecPrimitive
= PRIM_OUTSIDE_BEGIN_END
;
125 ctx
->Driver
.CurrentExecPrimitive
= prim
->mode
;
132 * Set the appropriate VAO to draw.
135 bind_vertex_list(struct gl_context
*ctx
,
136 const struct vbo_save_vertex_list
*node
)
138 const gl_vertex_processing_mode mode
= ctx
->VertexProgram
._VPMode
;
139 _mesa_set_draw_vao(ctx
, node
->VAO
[mode
], _vbo_get_vao_filter(mode
));
144 loopback_vertex_list(struct gl_context
*ctx
,
145 const struct vbo_save_vertex_list
*list
)
147 struct gl_buffer_object
*bo
= list
->VAO
[0]->BufferBinding
[0].BufferObj
;
148 ctx
->Driver
.MapBufferRange(ctx
, 0, bo
->Size
, GL_MAP_READ_BIT
, /* ? */
151 /* Note that the range of referenced vertices must be mapped already */
152 _vbo_loopback_vertex_list(ctx
, list
);
154 ctx
->Driver
.UnmapBuffer(ctx
, bo
, MAP_INTERNAL
);
159 * Execute the buffer and save copied verts.
160 * This is called from the display list code when executing
164 vbo_save_playback_vertex_list(struct gl_context
*ctx
, void *data
)
166 const struct vbo_save_vertex_list
*node
=
167 (const struct vbo_save_vertex_list
*) data
;
168 struct vbo_context
*vbo
= vbo_context(ctx
);
169 struct vbo_save_context
*save
= &vbo
->save
;
170 GLboolean remap_vertex_store
= GL_FALSE
;
172 if (save
->vertex_store
&& save
->vertex_store
->buffer_map
) {
173 /* The vertex store is currently mapped but we're about to replay
174 * a display list. This can happen when a nested display list is
175 * being build with GL_COMPILE_AND_EXECUTE.
176 * We never want to have mapped vertex buffers when we're drawing.
177 * Unmap the vertex store, execute the list, then remap the vertex
180 vbo_save_unmap_vertex_store(ctx
, save
->vertex_store
);
181 remap_vertex_store
= GL_TRUE
;
184 FLUSH_CURRENT(ctx
, 0);
186 if (node
->prim_count
> 0) {
188 if (_mesa_inside_begin_end(ctx
) && node
->prims
[0].begin
) {
189 /* Error: we're about to begin a new primitive but we're already
190 * inside a glBegin/End pair.
192 _mesa_error(ctx
, GL_INVALID_OPERATION
,
193 "draw operation inside glBegin/End");
196 else if (save
->replay_flags
) {
197 /* Various degenerate cases: translate into immediate mode
198 * calls rather than trying to execute in place.
200 loopback_vertex_list(ctx
, node
);
205 bind_vertex_list(ctx
, node
);
207 /* Need that at least one time. */
209 _mesa_update_state(ctx
);
211 /* XXX also need to check if shader enabled, but invalid */
212 if ((ctx
->VertexProgram
.Enabled
&&
213 !_mesa_arb_vertex_program_enabled(ctx
)) ||
214 (ctx
->FragmentProgram
.Enabled
&&
215 !_mesa_arb_fragment_program_enabled(ctx
))) {
216 _mesa_error(ctx
, GL_INVALID_OPERATION
,
217 "glBegin (invalid vertex/fragment program)");
221 /* Finally update the inputs array */
222 _vbo_update_inputs(ctx
, &vbo
->draw_arrays
);
223 _mesa_set_drawing_arrays(ctx
, vbo
->draw_arrays
.inputs
);
225 assert(ctx
->NewState
== 0);
227 if (node
->vertex_count
> 0) {
228 GLuint min_index
= node
->start_vertex
;
229 GLuint max_index
= min_index
+ node
->vertex_count
- 1;
235 min_index
, max_index
,
242 playback_copy_to_current(ctx
, node
);
245 if (remap_vertex_store
) {
246 save
->buffer_ptr
= vbo_save_map_vertex_store(ctx
, save
->vertex_store
);