Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / shader / prog_uniform.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 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
25 /**
26 * \file prog_uniform.c
27 * Shader uniform functions.
28 * \author Brian Paul
29 */
30
31 #include "main/imports.h"
32 #include "main/mtypes.h"
33 #include "prog_uniform.h"
34
35
36 struct gl_uniform_list *
37 _mesa_new_uniform_list(void)
38 {
39 return CALLOC_STRUCT(gl_uniform_list);
40 }
41
42
43 void
44 _mesa_free_uniform_list(struct gl_uniform_list *list)
45 {
46 GLuint i;
47 for (i = 0; i < list->NumUniforms; i++) {
48 _mesa_free((void *) list->Uniforms[i].Name);
49 }
50 _mesa_free(list->Uniforms);
51 _mesa_free(list);
52 }
53
54
55 GLboolean
56 _mesa_append_uniform(struct gl_uniform_list *list,
57 const char *name, GLenum target, GLuint progPos)
58 {
59 const GLuint oldNum = list->NumUniforms;
60 GLint index;
61
62 assert(target == GL_VERTEX_PROGRAM_ARB ||
63 target == GL_FRAGMENT_PROGRAM_ARB);
64
65 index = _mesa_lookup_uniform(list, name);
66 if (index < 0) {
67 /* not found - append to list */
68
69 if (oldNum + 1 > list->Size) {
70 /* Need to grow the list array (alloc some extra) */
71 list->Size += 4;
72
73 /* realloc arrays */
74 list->Uniforms = (struct gl_uniform *)
75 _mesa_realloc(list->Uniforms,
76 oldNum * sizeof(struct gl_uniform),
77 list->Size * sizeof(struct gl_uniform));
78 }
79
80 if (!list->Uniforms) {
81 /* out of memory */
82 list->NumUniforms = 0;
83 list->Size = 0;
84 return GL_FALSE;
85 }
86
87 list->Uniforms[oldNum].Name = _mesa_strdup(name);
88 list->Uniforms[oldNum].VertPos = -1;
89 list->Uniforms[oldNum].FragPos = -1;
90 list->Uniforms[oldNum].Initialized = GL_FALSE;
91 index = oldNum;
92 list->NumUniforms++;
93 }
94
95 /* update position for the vertex or fragment program */
96 if (target == GL_VERTEX_PROGRAM_ARB) {
97 if (list->Uniforms[index].VertPos != -1) {
98 /* this uniform is already in the list - that shouldn't happen */
99 return GL_FALSE;
100 }
101 list->Uniforms[index].VertPos = progPos;
102 }
103 else {
104 if (list->Uniforms[index].FragPos != -1) {
105 /* this uniform is already in the list - that shouldn't happen */
106 return GL_FALSE;
107 }
108 list->Uniforms[index].FragPos = progPos;
109 }
110
111 return GL_TRUE;
112 }
113
114
115 /**
116 * Return the location/index of the named uniform in the uniform list,
117 * or -1 if not found.
118 */
119 GLint
120 _mesa_lookup_uniform(const struct gl_uniform_list *list, const char *name)
121 {
122 GLuint i;
123 for (i = 0; list && i < list->NumUniforms; i++) {
124 if (!_mesa_strcmp(list->Uniforms[i].Name, name)) {
125 return i;
126 }
127 }
128 return -1;
129 }
130
131
132 GLint
133 _mesa_longest_uniform_name(const struct gl_uniform_list *list)
134 {
135 GLint max = 0;
136 GLuint i;
137 for (i = 0; list && i < list->NumUniforms; i++) {
138 GLint len = (GLint)_mesa_strlen(list->Uniforms[i].Name);
139 if (len > max)
140 max = len;
141 }
142 return max;
143 }
144
145
146 void
147 _mesa_print_uniforms(const struct gl_uniform_list *list)
148 {
149 GLuint i;
150 printf("Uniform list %p:\n", (void *) list);
151 for (i = 0; i < list->NumUniforms; i++) {
152 printf("%d: %s %d %d\n",
153 i,
154 list->Uniforms[i].Name,
155 list->Uniforms[i].VertPos,
156 list->Uniforms[i].FragPos);
157 }
158 }