Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / mesa / vbo / vbo_context.c
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 <keithw@vmware.com>
26 */
27
28 #include "main/imports.h"
29 #include "main/mtypes.h"
30 #include "main/api_arrayelt.h"
31 #include "main/bufferobj.h"
32 #include "math/m_eval.h"
33 #include "vbo.h"
34 #include "vbo_context.h"
35
36
37 static GLuint check_size( const GLfloat *attr )
38 {
39 if (attr[3] != 1.0F) return 4;
40 if (attr[2] != 0.0F) return 3;
41 if (attr[1] != 0.0F) return 2;
42 return 1;
43 }
44
45
46 /**
47 * Helper for initializing a vertex array.
48 */
49 static void
50 init_array(struct gl_context *ctx, struct gl_client_array *cl,
51 unsigned size, const void *pointer)
52 {
53 memset(cl, 0, sizeof(*cl));
54
55 cl->Size = size;
56 cl->Type = GL_FLOAT;
57 cl->Format = GL_RGBA;
58 cl->Stride = 0;
59 cl->StrideB = 0;
60 cl->_ElementSize = cl->Size * sizeof(GLfloat);
61 cl->Ptr = pointer;
62 cl->Enabled = 1;
63
64 _mesa_reference_buffer_object(ctx, &cl->BufferObj,
65 ctx->Shared->NullBufferObj);
66 }
67
68
69 /**
70 * Set up the vbo->currval arrays to point at the context's current
71 * vertex attributes (with strides = 0).
72 */
73 static void init_legacy_currval(struct gl_context *ctx)
74 {
75 struct vbo_context *vbo = vbo_context(ctx);
76 GLuint i;
77
78 /* Set up a constant (StrideB == 0) array for each current
79 * attribute:
80 */
81 for (i = 0; i < VERT_ATTRIB_FF_MAX; i++) {
82 struct gl_client_array *cl = &vbo->currval[VERT_ATTRIB_FF(i)];
83
84 init_array(ctx, cl,
85 check_size(ctx->Current.Attrib[i]),
86 ctx->Current.Attrib[i]);
87 }
88 }
89
90
91 static void init_generic_currval(struct gl_context *ctx)
92 {
93 struct vbo_context *vbo = vbo_context(ctx);
94 GLuint i;
95
96 for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++) {
97 struct gl_client_array *cl = &vbo->currval[VBO_ATTRIB_GENERIC0 + i];
98
99 init_array(ctx, cl, 1, ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + i]);
100 }
101 }
102
103
104 static void init_mat_currval(struct gl_context *ctx)
105 {
106 struct vbo_context *vbo = vbo_context(ctx);
107 GLuint i;
108
109 /* Set up a constant (StrideB == 0) array for each current
110 * attribute:
111 */
112 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
113 struct gl_client_array *cl =
114 &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT + i];
115 unsigned size;
116
117 /* Size is fixed for the material attributes, for others will
118 * be determined at runtime:
119 */
120 switch (i) {
121 case MAT_ATTRIB_FRONT_SHININESS:
122 case MAT_ATTRIB_BACK_SHININESS:
123 size = 1;
124 break;
125 case MAT_ATTRIB_FRONT_INDEXES:
126 case MAT_ATTRIB_BACK_INDEXES:
127 size = 3;
128 break;
129 default:
130 size = 4;
131 break;
132 }
133
134 init_array(ctx, cl, size, ctx->Light.Material.Attrib[i]);
135 }
136 }
137
138
139 GLboolean _vbo_CreateContext( struct gl_context *ctx )
140 {
141 struct vbo_context *vbo = CALLOC_STRUCT(vbo_context);
142
143 ctx->vbo_context = vbo;
144
145 /* Initialize the arrayelt helper
146 */
147 if (!ctx->aelt_context &&
148 !_ae_create_context( ctx )) {
149 return GL_FALSE;
150 }
151
152 init_legacy_currval( ctx );
153 init_generic_currval( ctx );
154 init_mat_currval( ctx );
155
156 /* Build mappings from VERT_ATTRIB -> VBO_ATTRIB depending on type
157 * of vertex program active.
158 */
159 {
160 GLuint i;
161
162 /* identity mapping */
163 for (i = 0; i < ARRAY_SIZE(vbo->map_vp_none); i++)
164 vbo->map_vp_none[i] = i;
165 /* map material attribs to generic slots */
166 for (i = 0; i < MAT_ATTRIB_MAX; i++)
167 vbo->map_vp_none[VERT_ATTRIB_GENERIC(i)]
168 = VBO_ATTRIB_MAT_FRONT_AMBIENT + i;
169
170 for (i = 0; i < ARRAY_SIZE(vbo->map_vp_arb); i++)
171 vbo->map_vp_arb[i] = i;
172 }
173
174
175 /* Hook our functions into exec and compile dispatch tables. These
176 * will pretty much be permanently installed, which means that the
177 * vtxfmt mechanism can be removed now.
178 */
179 vbo_exec_init( ctx );
180 if (ctx->API == API_OPENGL_COMPAT)
181 vbo_save_init( ctx );
182
183 _math_init_eval();
184
185 return GL_TRUE;
186 }
187
188
189 void _vbo_InvalidateState( struct gl_context *ctx, GLuint new_state )
190 {
191 vbo_exec_invalidate_state(ctx, new_state);
192 }
193
194
195 void _vbo_DestroyContext( struct gl_context *ctx )
196 {
197 struct vbo_context *vbo = vbo_context(ctx);
198
199 if (ctx->aelt_context) {
200 _ae_destroy_context( ctx );
201 ctx->aelt_context = NULL;
202 }
203
204 if (vbo) {
205 GLuint i;
206
207 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
208 _mesa_reference_buffer_object(ctx, &vbo->currval[i].BufferObj, NULL);
209 }
210
211 vbo_exec_destroy(ctx);
212 if (ctx->API == API_OPENGL_COMPAT)
213 vbo_save_destroy(ctx);
214 free(vbo);
215 ctx->vbo_context = NULL;
216 }
217 }
218
219
220 void vbo_set_draw_func(struct gl_context *ctx, vbo_draw_func func)
221 {
222 struct vbo_context *vbo = vbo_context(ctx);
223 vbo->draw_prims = func;
224 }
225