3ccbfac7e2856410d2b0fb118b1290ef87675578
[mesa.git] / src / mesa / vbo / vbo_save.h
1 /**************************************************************************
2
3 Copyright 2002 VMware, Inc.
4
5 All Rights Reserved.
6
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 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
16 Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keithw@vmware.com>
31 *
32 */
33
34 #ifndef VBO_SAVE_H
35 #define VBO_SAVE_H
36
37 #include "main/mtypes.h"
38 #include "vbo.h"
39 #include "vbo_attrib.h"
40
41
42 struct vbo_save_copied_vtx {
43 fi_type buffer[VBO_ATTRIB_MAX * 4 * VBO_MAX_COPIED_VERTS];
44 GLuint nr;
45 };
46
47
48 /* For display lists, this structure holds a run of vertices of the
49 * same format, and a strictly well-formed set of begin/end pairs,
50 * starting on the first vertex and ending at the last. Vertex
51 * copying on buffer breaks is precomputed according to these
52 * primitives, though there are situations where the copying will need
53 * correction at execute-time, perhaps by replaying the list as
54 * immediate mode commands.
55 *
56 * On executing this list, the 'current' values may be updated with
57 * the values of the final vertex, and often no fixup of the start of
58 * the vertex list is required.
59 *
60 * Eval and other commands that don't fit into these vertex lists are
61 * compiled using the fallback opcode mechanism provided by dlist.c.
62 */
63 struct vbo_save_vertex_list {
64 GLubyte attrsz[VBO_ATTRIB_MAX];
65 GLuint vertex_size; /**< size in GLfloats */
66 struct gl_vertex_array_object *VAO[VP_MODE_MAX];
67
68 /* Copy of the final vertex from node->vertex_store->bufferobj.
69 * Keep this in regular (non-VBO) memory to avoid repeated
70 * map/unmap of the VBO when updating GL current data.
71 */
72 fi_type *current_data;
73
74 GLuint buffer_offset; /**< in bytes */
75 GLuint start_vertex; /**< first vertex used by any primitive */
76 GLuint vertex_count; /**< number of vertices in this list */
77 GLuint wrap_count; /* number of copied vertices at start */
78
79 struct _mesa_prim *prims;
80 GLuint prim_count;
81
82 struct vbo_save_primitive_store *prim_store;
83 };
84
85
86 /**
87 * Is the vertex list's buffer offset an exact multiple of the
88 * vertex size (in bytes)? This is used to check for a vertex array /
89 * drawing optimization.
90 */
91 static inline bool
92 aligned_vertex_buffer_offset(const struct vbo_save_vertex_list *node)
93 {
94 unsigned vertex_size = node->vertex_size * sizeof(GLfloat); /* in bytes */
95 return vertex_size != 0 && node->buffer_offset % vertex_size == 0;
96 }
97
98
99 /**
100 * Return the stride in bytes of the display list node.
101 */
102 static inline GLsizei
103 _vbo_save_get_stride(const struct vbo_save_vertex_list *node)
104 {
105 return node->VAO[0]->BufferBinding[0].Stride;
106 }
107
108
109 /**
110 * Return the first referenced vertex index in the display list node.
111 */
112 static inline GLuint
113 _vbo_save_get_min_index(const struct vbo_save_vertex_list *node)
114 {
115 assert(node->prim_count > 0);
116 return node->prims[0].start;
117 }
118
119
120 /**
121 * Return the last referenced vertex index in the display list node.
122 */
123 static inline GLuint
124 _vbo_save_get_max_index(const struct vbo_save_vertex_list *node)
125 {
126 assert(node->prim_count > 0);
127 const struct _mesa_prim *last_prim = &node->prims[node->prim_count - 1];
128 return last_prim->start + last_prim->count - 1;
129 }
130
131
132 /**
133 * Return the vertex count in the display list node.
134 */
135 static inline GLuint
136 _vbo_save_get_vertex_count(const struct vbo_save_vertex_list *node)
137 {
138 assert(node->prim_count > 0);
139 const struct _mesa_prim *first_prim = &node->prims[0];
140 const struct _mesa_prim *last_prim = &node->prims[node->prim_count - 1];
141 return last_prim->start - first_prim->start + last_prim->count;
142 }
143
144
145 /* These buffers should be a reasonable size to support upload to
146 * hardware. Current vbo implementation will re-upload on any
147 * changes, so don't make too big or apps which dynamically create
148 * dlists and use only a few times will suffer.
149 *
150 * Consider stategy of uploading regions from the VBO on demand in the
151 * case of dynamic vbos. Then make the dlist code signal that
152 * likelyhood as it occurs. No reason we couldn't change usage
153 * internally even though this probably isn't allowed for client VBOs?
154 */
155 #define VBO_SAVE_BUFFER_SIZE (256*1024) /* dwords */
156 #define VBO_SAVE_PRIM_SIZE 128
157 #define VBO_SAVE_PRIM_MODE_MASK 0x3f
158 #define VBO_SAVE_PRIM_WEAK 0x40
159 #define VBO_SAVE_PRIM_NO_CURRENT_UPDATE 0x80
160
161 #define VBO_SAVE_FALLBACK 0x10000000
162
163 struct vbo_save_vertex_store {
164 struct gl_buffer_object *bufferobj;
165 fi_type *buffer_map;
166 GLuint used; /**< Number of 4-byte words used in buffer */
167 };
168
169 /* Storage to be shared among several vertex_lists.
170 */
171 struct vbo_save_primitive_store {
172 struct _mesa_prim prims[VBO_SAVE_PRIM_SIZE];
173 GLuint used;
174 GLuint refcount;
175 };
176
177
178 struct vbo_save_context {
179 struct gl_context *ctx;
180 GLvertexformat vtxfmt;
181 GLvertexformat vtxfmt_noop; /**< Used if out_of_memory is true */
182
183 GLbitfield64 enabled; /**< mask of enabled vbo arrays. */
184 GLubyte attrsz[VBO_ATTRIB_MAX]; /**< 1, 2, 3 or 4 */
185 GLenum16 attrtype[VBO_ATTRIB_MAX]; /**< GL_FLOAT, GL_INT, etc */
186 GLubyte active_sz[VBO_ATTRIB_MAX]; /**< 1, 2, 3 or 4 */
187 GLuint vertex_size; /**< size in GLfloats */
188 struct gl_vertex_array_object *VAO[VP_MODE_MAX];
189
190 GLboolean out_of_memory; /**< True if last VBO allocation failed */
191
192 GLbitfield replay_flags;
193
194 struct _mesa_prim *prims;
195 GLuint prim_count, prim_max;
196
197 struct vbo_save_vertex_store *vertex_store;
198 struct vbo_save_primitive_store *prim_store;
199
200 fi_type *buffer_map; /**< Mapping of vertex_store's buffer */
201 fi_type *buffer_ptr; /**< cursor, points into buffer_map */
202 fi_type vertex[VBO_ATTRIB_MAX*4]; /* current values */
203 fi_type *attrptr[VBO_ATTRIB_MAX];
204 GLuint vert_count;
205 GLuint max_vert;
206 GLboolean dangling_attr_ref;
207
208 GLuint opcode_vertex_list;
209
210 struct vbo_save_copied_vtx copied;
211
212 fi_type *current[VBO_ATTRIB_MAX]; /* points into ctx->ListState */
213 GLubyte *currentsz[VBO_ATTRIB_MAX];
214 };
215
216 void vbo_save_init(struct gl_context *ctx);
217 void vbo_save_destroy(struct gl_context *ctx);
218 void vbo_save_fallback(struct gl_context *ctx, GLboolean fallback);
219
220 /* save_loopback.c:
221 */
222 void _vbo_loopback_vertex_list(struct gl_context *ctx,
223 const struct vbo_save_vertex_list* node);
224
225 /* Callbacks:
226 */
227 void
228 vbo_save_playback_vertex_list(struct gl_context *ctx, void *data);
229
230 void
231 vbo_save_api_init(struct vbo_save_context *save);
232
233 fi_type *
234 vbo_save_map_vertex_store(struct gl_context *ctx,
235 struct vbo_save_vertex_store *vertex_store);
236
237 void
238 vbo_save_unmap_vertex_store(struct gl_context *ctx,
239 struct vbo_save_vertex_store *vertex_store);
240
241 #endif /* VBO_SAVE_H */