[MESA]
[reactos.git] / reactos / dll / opengl / mesa / vbo / vbo_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2005 Brian Paul 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 * 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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include <precomp.h>
29
30 #define NR_MAT_ATTRIBS 12
31
32 static GLuint check_size( const GLfloat *attr )
33 {
34 if (attr[3] != 1.0) return 4;
35 if (attr[2] != 0.0) return 3;
36 if (attr[1] != 0.0) return 2;
37 return 1;
38 }
39
40
41 static void init_legacy_currval(struct gl_context *ctx)
42 {
43 struct vbo_context *vbo = vbo_context(ctx);
44 struct gl_client_array *arrays = vbo->legacy_currval;
45 GLuint i;
46
47 memset(arrays, 0, sizeof(*arrays) * VERT_ATTRIB_MAX);
48
49 /* Set up a constant (StrideB == 0) array for each current
50 * attribute:
51 */
52 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
53 struct gl_client_array *cl = &arrays[i];
54
55 /* Size will have to be determined at runtime:
56 */
57 cl->Size = check_size(ctx->Current.Attrib[i]);
58 cl->Stride = 0;
59 cl->StrideB = 0;
60 cl->Enabled = 1;
61 cl->Type = GL_FLOAT;
62 cl->Ptr = (const void *)ctx->Current.Attrib[i];
63 cl->_ElementSize = cl->Size * sizeof(GLfloat);
64 _mesa_reference_buffer_object(ctx, &cl->BufferObj,
65 ctx->Shared->NullBufferObj);
66 }
67 }
68
69
70 static void init_mat_currval(struct gl_context *ctx)
71 {
72 struct vbo_context *vbo = vbo_context(ctx);
73 struct gl_client_array *arrays = vbo->mat_currval;
74 GLuint i;
75
76 ASSERT(NR_MAT_ATTRIBS == MAT_ATTRIB_MAX);
77
78 memset(arrays, 0, sizeof(*arrays) * NR_MAT_ATTRIBS);
79
80 /* Set up a constant (StrideB == 0) array for each current
81 * attribute:
82 */
83 for (i = 0; i < NR_MAT_ATTRIBS; i++) {
84 struct gl_client_array *cl = &arrays[i];
85
86 /* Size is fixed for the material attributes, for others will
87 * be determined at runtime:
88 */
89 switch (i) {
90 case MAT_ATTRIB_FRONT_SHININESS:
91 case MAT_ATTRIB_BACK_SHININESS:
92 cl->Size = 1;
93 break;
94 case MAT_ATTRIB_FRONT_INDEXES:
95 case MAT_ATTRIB_BACK_INDEXES:
96 cl->Size = 3;
97 break;
98 default:
99 cl->Size = 4;
100 break;
101 }
102
103 cl->Ptr = (const void*)ctx->Light.Material.Attrib[i];
104 cl->Type = GL_FLOAT;
105 cl->Stride = 0;
106 cl->StrideB = 0;
107 cl->Enabled = 1;
108 cl->_ElementSize = cl->Size * sizeof(GLfloat);
109 _mesa_reference_buffer_object(ctx, &cl->BufferObj,
110 ctx->Shared->NullBufferObj);
111 }
112 }
113
114
115 GLboolean _vbo_CreateContext( struct gl_context *ctx )
116 {
117 struct vbo_context *vbo = CALLOC_STRUCT(vbo_context);
118
119 ctx->swtnl_im = (void *)vbo;
120
121 /* Initialize the arrayelt helper
122 */
123 if (!ctx->aelt_context &&
124 !_ae_create_context( ctx )) {
125 return GL_FALSE;
126 }
127
128 /* TODO: remove these pointers.
129 */
130 vbo->legacy_currval = &vbo->currval[VBO_ATTRIB_POS];
131 vbo->mat_currval = &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT];
132
133 init_legacy_currval( ctx );
134 init_mat_currval( ctx );
135
136
137 /* Hook our functions into exec and compile dispatch tables. These
138 * will pretty much be permanently installed, which means that the
139 * vtxfmt mechanism can be removed now.
140 */
141 vbo_exec_init( ctx );
142 vbo_save_init( ctx );
143
144 _math_init_eval();
145
146 return GL_TRUE;
147 }
148
149
150 void _vbo_InvalidateState( struct gl_context *ctx, GLuint new_state )
151 {
152 vbo_exec_invalidate_state(ctx, new_state);
153 }
154
155
156 void _vbo_DestroyContext( struct gl_context *ctx )
157 {
158 struct vbo_context *vbo = vbo_context(ctx);
159
160 if (ctx->aelt_context) {
161 _ae_destroy_context( ctx );
162 ctx->aelt_context = NULL;
163 }
164
165 if (vbo) {
166 GLuint i;
167
168 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
169 _mesa_reference_buffer_object(ctx, &vbo->currval[i].BufferObj, NULL);
170 }
171
172 vbo_exec_destroy(ctx);
173 vbo_save_destroy(ctx);
174 FREE(vbo);
175 ctx->swtnl_im = NULL;
176 }
177 }
178
179
180 void vbo_set_draw_func(struct gl_context *ctx, vbo_draw_func func)
181 {
182 struct vbo_context *vbo = vbo_context(ctx);
183 vbo->draw_prims = func;
184 }
185