1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors:
25 * Keith Whitwell <[email protected]>
26 */
27
28 #include "main/errors.h"
29 #include "main/bufferobj.h"
30 #include "math/m_eval.h"
31 #include "main/api_arrayelt.h"
32 #include "main/arrayobj.h"
33 #include "main/varray.h"
34 #include "main/context.h"
35 #include "util/u_memory.h"
36 #include "vbo.h"
37 #include "vbo_private.h"
38
39
40 static GLuint
check_size(const GLfloat * attr)41 check_size(const GLfloat *attr)
42 {
43 if (attr[3] != 1.0F)
44 return 4;
45 if (attr[2] != 0.0F)
46 return 3;
47 if (attr[1] != 0.0F)
48 return 2;
49 return 1;
50 }
51
52
53 /**
54 * Helper for initializing a vertex array.
55 */
56 static void
init_array(struct gl_context * ctx,struct gl_array_attributes * attrib,unsigned size,const void * pointer)57 init_array(struct gl_context *ctx, struct gl_array_attributes *attrib,
58 unsigned size, const void *pointer)
59 {
60 memset(attrib, 0, sizeof(*attrib));
61
62 vbo_set_vertex_format(&attrib->Format, size, GL_FLOAT);
63 attrib->Stride = 0;
64 attrib->Ptr = pointer;
65 }
66
67
68 /**
69 * Set up the vbo->currval arrays to point at the context's current
70 * vertex attributes (with strides = 0).
71 */
72 static void
init_legacy_currval(struct gl_context * ctx)73 init_legacy_currval(struct gl_context *ctx)
74 {
75 struct vbo_context *vbo = vbo_context(ctx);
76
77 /* Set up a constant (Stride == 0) array for each current
78 * attribute:
79 */
80 for (int attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
81 if (VERT_BIT(attr) & VERT_BIT_GENERIC_ALL)
82 continue;
83
84 struct gl_array_attributes *attrib = &vbo->current[attr];
85
86 init_array(ctx, attrib, check_size(ctx->Current.Attrib[attr]),
87 ctx->Current.Attrib[attr]);
88 }
89 }
90
91
92 static void
init_generic_currval(struct gl_context * ctx)93 init_generic_currval(struct gl_context *ctx)
94 {
95 struct vbo_context *vbo = vbo_context(ctx);
96 GLuint i;
97
98 for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++) {
99 const unsigned attr = VBO_ATTRIB_GENERIC0 + i;
100 struct gl_array_attributes *attrib = &vbo->current[attr];
101
102 init_array(ctx, attrib, 1, ctx->Current.Attrib[attr]);
103 }
104 }
105
106
107 static void
init_mat_currval(struct gl_context * ctx)108 init_mat_currval(struct gl_context *ctx)
109 {
110 struct vbo_context *vbo = vbo_context(ctx);
111 GLuint i;
112
113 /* Set up a constant (StrideB == 0) array for each current
114 * attribute:
115 */
116 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
117 const unsigned attr = VBO_ATTRIB_MAT_FRONT_AMBIENT + i;
118 struct gl_array_attributes *attrib = &vbo->current[attr];
119 unsigned size;
120
121 /* Size is fixed for the material attributes, for others will
122 * be determined at runtime:
123 */
124 switch (i) {
125 case MAT_ATTRIB_FRONT_SHININESS:
126 case MAT_ATTRIB_BACK_SHININESS:
127 size = 1;
128 break;
129 case MAT_ATTRIB_FRONT_INDEXES:
130 case MAT_ATTRIB_BACK_INDEXES:
131 size = 3;
132 break;
133 default:
134 size = 4;
135 break;
136 }
137
138 init_array(ctx, attrib, size, ctx->Light.Material.Attrib[i]);
139 }
140 }
141
142
143 void
vbo_exec_update_eval_maps(struct gl_context * ctx)144 vbo_exec_update_eval_maps(struct gl_context *ctx)
145 {
146 struct vbo_context *vbo = vbo_context(ctx);
147
148 vbo->exec.eval.recalculate_maps = GL_TRUE;
149 }
150
151
152 GLboolean
_vbo_CreateContext(struct gl_context * ctx)153 _vbo_CreateContext(struct gl_context *ctx)
154 {
155 struct vbo_context *vbo = &ctx->vbo_context;
156
157 memset(vbo, 0, sizeof(*vbo));
158
159 init_legacy_currval(ctx);
160 init_generic_currval(ctx);
161 init_mat_currval(ctx);
162
163 /* make sure all VBO_ATTRIB_ values can fit in an unsigned byte */
164 STATIC_ASSERT(VBO_ATTRIB_MAX <= 255);
165
166 vbo_exec_init(ctx);
167 if (_mesa_is_desktop_gl_compat(ctx))
168 vbo_save_init(ctx);
169
170 vbo->VAO = _mesa_new_vao(ctx, ~((GLuint)0));
171 /* The exec VAO assumes to have all arributes bound to binding 0 */
172 for (unsigned i = 0; i < VERT_ATTRIB_MAX; ++i)
173 _mesa_vertex_attrib_binding(ctx, vbo->VAO, i, 0);
174
175 _math_init_eval();
176
177 return GL_TRUE;
178 }
179
180
181 void
_vbo_DestroyContext(struct gl_context * ctx)182 _vbo_DestroyContext(struct gl_context *ctx)
183 {
184 struct vbo_context *vbo = vbo_context(ctx);
185
186 if (vbo) {
187 vbo_exec_destroy(ctx);
188 if (_mesa_is_desktop_gl_compat(ctx))
189 vbo_save_destroy(ctx);
190 _mesa_reference_vao(ctx, &vbo->VAO, NULL);
191 }
192 }
193
194
195 const struct gl_array_attributes *
_vbo_current_attrib(const struct gl_context * ctx,gl_vert_attrib attr)196 _vbo_current_attrib(const struct gl_context *ctx, gl_vert_attrib attr)
197 {
198 const struct vbo_context *vbo = vbo_context_const(ctx);
199 const gl_vertex_processing_mode vmp = ctx->VertexProgram._VPMode;
200 return &vbo->current[_vbo_attribute_alias_map[vmp][attr]];
201 }
202