Merge commit 'origin/gallium-master-merge'
[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 struct gl_uniform *
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 struct gl_uniform *uniform;
61 GLint index;
62
63 assert(target == GL_VERTEX_PROGRAM_ARB ||
64 target == GL_FRAGMENT_PROGRAM_ARB);
65
66 index = _mesa_lookup_uniform(list, name);
67 if (index < 0) {
68 /* not found - append to list */
69
70 if (oldNum + 1 > list->Size) {
71 /* Need to grow the list array (alloc some extra) */
72 list->Size += 4;
73
74 /* realloc arrays */
75 list->Uniforms = (struct gl_uniform *)
76 _mesa_realloc(list->Uniforms,
77 oldNum * sizeof(struct gl_uniform),
78 list->Size * sizeof(struct gl_uniform));
79 }
80
81 if (!list->Uniforms) {
82 /* out of memory */
83 list->NumUniforms = 0;
84 list->Size = 0;
85 return GL_FALSE;
86 }
87
88 uniform = list->Uniforms + oldNum;
89
90 uniform->Name = _mesa_strdup(name);
91 uniform->VertPos = -1;
92 uniform->FragPos = -1;
93 uniform->Initialized = GL_FALSE;
94
95 list->NumUniforms++;
96 }
97 else {
98 /* found */
99 uniform = list->Uniforms + index;
100 }
101
102 /* update position for the vertex or fragment program */
103 if (target == GL_VERTEX_PROGRAM_ARB) {
104 if (uniform->VertPos != -1) {
105 /* this uniform is already in the list - that shouldn't happen */
106 return GL_FALSE;
107 }
108 uniform->VertPos = progPos;
109 }
110 else {
111 if (uniform->FragPos != -1) {
112 /* this uniform is already in the list - that shouldn't happen */
113 return GL_FALSE;
114 }
115 uniform->FragPos = progPos;
116 }
117
118 return uniform;
119 }
120
121
122 /**
123 * Return the location/index of the named uniform in the uniform list,
124 * or -1 if not found.
125 */
126 GLint
127 _mesa_lookup_uniform(const struct gl_uniform_list *list, const char *name)
128 {
129 GLuint i;
130 for (i = 0; list && i < list->NumUniforms; i++) {
131 if (!_mesa_strcmp(list->Uniforms[i].Name, name)) {
132 return i;
133 }
134 }
135 return -1;
136 }
137
138
139 GLint
140 _mesa_longest_uniform_name(const struct gl_uniform_list *list)
141 {
142 GLint max = 0;
143 GLuint i;
144 for (i = 0; list && i < list->NumUniforms; i++) {
145 GLint len = (GLint)_mesa_strlen(list->Uniforms[i].Name);
146 if (len > max)
147 max = len;
148 }
149 return max;
150 }
151
152
153 void
154 _mesa_print_uniforms(const struct gl_uniform_list *list)
155 {
156 GLuint i;
157 printf("Uniform list %p:\n", (void *) list);
158 for (i = 0; i < list->NumUniforms; i++) {
159 printf("%d: %s %d %d\n",
160 i,
161 list->Uniforms[i].Name,
162 list->Uniforms[i].VertPos,
163 list->Uniforms[i].FragPos);
164 }
165 }